首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ostream问题

ostream问题
EN

Stack Overflow用户
提问于 2012-03-27 20:58:26
回答 1查看 369关注 0票数 2

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

代码语言:javascript
复制
template<class T> friend ostream& operator <<(ostream& s, ArrayLinkedList<T>& ll){
    //Extra code for giving s content
    return s;   
}

以及拥有

代码语言:javascript
复制
template<class T> friend ostream& operator <<(ostream& s, ArrayLinkedListRow<T>& ll){
        //Extra code for giving s content
        return s;   
    }

在ArrayLinkedListRow内部。

我得到以下错误

错误1错误C2995:'std::ostream &operator <<(std::ostream &,ArrayLinkedList &)‘:函数模板已经定义

这让我发疯了,我不知道怎么解决它。我已经做了我的研究,但我仍然不知道该做什么。我坚信这两个类在问题中可能是相关的,尽管错误只是指向一行。

额外信息:这是为那些对我的简短解释感到困惑的人的类ArrayLinkedList头。

代码语言:javascript
复制
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
};
EN

回答 1

Stack Overflow用户

发布于 2012-03-27 21:28:45

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

代码语言:javascript
复制
friend ostream& operator <<(ostream& s, ArrayLinkedList& ll){
   //Extra code for giving s content
   return s;   
}
// and analogically with  ArrayLinkedListRow

之所以这样做是为了说明here

  • --如果您声明变量ArrayLinkedList<int>,那么并且只有这样,才会使用模板参数TDT创建operator << (这是未使用的)。如果您编译它,所有操作都可以正常工作。
  • 如果您添加了一个类型为ArrayLinkedList<float>的变量,那么操作符将第二次被定义,这将创建错误.

只有使用DT才能使它像预期的那样工作。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9897606

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档