Ren
Ren

Reputation: 4683

ostream problems

So I have two classes inside the same file; ArrayLinkedList and ArrayLinkedListRow Inside the first mentioned one I have a method

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

as well as having

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

inside of ArrayLinkedListRow.

I get the following error

Error 1 error C2995: 'std::ostream &operator <<(std::ostream &,ArrayLinkedList &)' : function template has already been defined

and it's driving me nuts not knowing how to fix it. I have done my research, but still I cannot figure out what to do. I strongly believe that the two classes might be related in the problem, despite of the error just pointing out to one line.

Extra Info: This is the class ArrayLinkedList header for those who feel confused with my short explanation.

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
};

Upvotes: 2

Views: 378

Answers (1)

ipc
ipc

Reputation: 8143

Try to remove the template<class T> part:

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

The reason why this works is stated here:

  • If you declare a variable ArrayLinkedList<int>, then and only then the operator << is created with the template-parameter T and DT (which is unused). If you compile this, everything works fine.
  • If you add a variable of type ArrayLinkedList<float>, then the operator gets defined a second time and this creates the error.

Working only with DT makes it work as expected.

Upvotes: 1

Related Questions