所以我在同一个文件中有两个类。 ArrayLinkedListArrayLinkedListRow在第一个提到的里面,我有一个方法

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

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

在ArrayLinkedListRow内部。

我收到以下错误



而且让我发疯,不知道如何解决它。我已经完成了研究,但是仍然无法弄清楚该怎么做。我坚信,尽管错误仅指出了一行,但这两个类可能在问题中相关。

额外信息:
对于那些对我的简短解释感到困惑的人来说,这是类ArrayLinkedList头。
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
};

最佳答案

尝试删除template<class T>部分:

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

起作用的原因是here:
  • 如果您声明变量ArrayLinkedList<int>,那么并且只有这样,才会使用模板参数operator <<T(未使用)创建DT。如果您对此进行编译,则一切正常。
  • 如果添加类型为ArrayLinkedList<float>的变量,则将再次定义运算符,这将产生错误。

  • 仅使用DT可使其按预期工作。

    关于c++ - 即时问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9897606/

    10-09 22:46