2012-03-27 10 views
2

Also habe ich zwei Klassen in der gleichen Datei; ArrayLinkedList und ArrayLinkedListRow innerhalb des ersten erwähnten einen Iostream Probleme

template<class T> friend ostream& operator <<(ostream& s, ArrayLinkedList<T>& ll){ 
    //Extra code for giving s content 
    return s; 
} 

sowie mit

template<class T> friend ostream& operator <<(ostream& s, ArrayLinkedListRow<T>& ll){ 
     //Extra code for giving s content 
     return s; 
    } 

Innenseite ArrayLinkedListRow eine Methode haben.

bekomme ich folgende Fehler

Fehler 1 Fehler C2995: 'std :: ostream & Operator < < (std :: ostream &, ArrayLinkedList &)': Funktions-Template wurde bereits definiert

und es macht mich verrückt, nicht zu wissen, wie es zu beheben ist. Ich habe meine Nachforschungen gemacht, aber ich kann immer noch nicht herausfinden, was ich tun soll. Ich glaube fest daran, dass die beiden Klassen in dem Problem zusammenhängen könnten, obwohl der Fehler nur auf eine Zeile hinweist.

Extra Info: Dies ist die Klasse ArrayLinkedList Header für diejenigen, die mit meiner kurzen Erklärung verwirrt sind.

template<class DT> 
class ArrayLinkedList { 

private: 
    DT* _info[MAX_SIZE]; // store data 
    int _next[MAX_SIZE]; // next node 
    int _nextEmpty[MAX_SIZE]; //next empty slot 

    ArrayClass< ArrayLinkedListRow<DT> >* _rows; 
    int _head; // head of the list 
    int _firstEmpty; // first empty slot 
    int _size; 
    void copy(const ArrayLinkedList<DT>& ll);//copy from another list 
    // add a new node with next as it's next node and returns the index of new node 
    int newNode(DT& newObject, int next); 

public: 
    ArrayLinkedList(); // empty and copy constructors 
    ArrayLinkedList(const ArrayLinkedList<DT>& ll); 
    //copy constructors linked list object to an existing object. This is a deep copy. 
    ~ArrayLinkedList(); // destructor 

    ArrayLinkedList(DT& newObject); // Constructor that create a list with newObject as the head 
    ArrayLinkedList(int capacity); // Constructor with a give capacity 
    ArrayLinkedList(DT& newObject,int capacity);// Constructor with newObject as the head and capacity 

    bool isEmpty(); // is the list empty? 
    int size(); // return the number of nodes stored 

    void add(DT& newObject); // add an object to the tail 
    void insertAt(DT& newObject, int position); // insert an object at the position specified 

    DT remove(); // remove the head 
    DT removeAt(int position); // remove an object at the position specified 

    int find(DT key); // find the object that matches key, index of the object 

    void operator=(const ArrayLinkedList<DT>& ll); // = operator 
    // overloading [] operator, return a reference to object at the 
    // Add a new data element to the start of a linked list. 

    DT& operator[] (const int position);  // position in the linked list 

    // ostream operator 
    template<class T> friend ostream& operator <<(ostream& s, ArrayLinkedList<T>& ll){ 
     return s; 
    } 

    void displayRaw(); // display raw data of the data members 
}; 
+0

Können Sie das Problem auf ein kleines, vollständiges Beispiel reduzieren, das immer noch den Fehler verursacht? Gibt es eine Vererbungsbeziehung zwischen 'ArrayLinkedList <>' und 'ArrayLinkedListRow <>'? –

+0

Keine Vererbungsbeziehung. Ich werde die Frage bearbeiten, so dass Sie die komplette Klasse sehen können. – Yokhen

Antwort

1

Versuchen Sie, die template<class T> Teil zu entfernen:

friend ostream& operator <<(ostream& s, ArrayLinkedList& ll){ 
    //Extra code for giving s content 
    return s; 
} 
// and analogically with ArrayLinkedListRow 

Der Grund, warum das funktioniert angegeben here:

  • Wenn Sie eine Variable ArrayLinkedList<int> deklarieren, dann und nur dann die operator << ist erstellt mit dem Template-Parameter T und DT (die nicht verwendet wird). Wenn Sie das kompilieren, funktioniert alles gut.
  • Wenn Sie eine Variable vom Typ ArrayLinkedList<float> hinzufügen, wird der Operator ein zweites Mal definiert und dies erzeugt den Fehler.

Nur mit DT arbeiten funktioniert es wie erwartet.

+0

Danke, ich werde das tun. Hoffe, es funktioniert mit dem Rest des Hauptprogramms. Lustige Sache ist, dass 'ArrayLinkedListRow' funktioniert genauso gut wie es ist, obwohl es im Grunde identisch mit' ArrayLinkedList' in der Struktur ist. – Yokhen

+0

Ist das ein weiteres MSVC "Feature" (Bug)? – enobayram

Verwandte Themen