【问题标题】:ostream problems流问题
【发布时间】:2012-03-27 20:58:26
【问题描述】:

所以我在同一个文件中有两个类; 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 内部。

我收到以下错误

错误 1 ​​错误 C2995: 'std::ostream &operator

它让我发疯,不知道如何解决它。我已经完成了我的研究,但我仍然不知道该怎么做。我坚信这两个类可能与问题有关,尽管错误只是指出了一行。

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

【问题讨论】:

  • 您能否将问题简化为一个仍然会产生错误的小而完整的示例? ArrayLinkedList&lt;&gt;ArrayLinkedListRow&lt;&gt;之间是否存在某种继承关系?
  • 无继承关系。我将编辑问题,以便您可以看到完整的课程。

标签: c++ ostream


【解决方案1】:

尝试删除template&lt;class T&gt;部分:

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

这个工作的原因是here

  • 如果你声明了一个变量ArrayLinkedList&lt;int&gt;,那么只有这样operator &lt;&lt;才会使用模板参数TDT(未使用)创建。如果你编译这个,一切正常。
  • 如果添加ArrayLinkedList&lt;float&gt; 类型的变量,则会再次定义运算符,这会产生错误。

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

【讨论】:

  • 谢谢,我会的。希望它适用于主程序的其余部分。有趣的是ArrayLinkedListRow 可以正常工作,尽管它在结构上与ArrayLinkedList 基本相同。
  • 这是另一个 MSVC “功能”(错误)吗?
猜你喜欢
  • 2011-06-12
  • 2011-07-28
  • 2011-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-21
  • 2019-10-04
相关资源
最近更新 更多