【问题标题】:expected constructor, destructor, or type conversion before & token& 标记之前的预期构造函数、析构函数或类型转换
【发布时间】:2011-04-12 02:42:06
【问题描述】:

我的项目中出现了一个神秘错误:

预期的构造函数、析构函数或类型转换

它不允许我使用我重载的operator<<。在我将我的课程 (myVector) 变成 template 之前,这以前有效。

//MyVector class
//An implementation of a vector of integers.
template <class T>
class MyVector{
public:
    //Purpose: Initialize an object of type MyVector
    //Parameters: none.
    //Returns: nothing.
    MyVector();

    //------------------------------------------------
    //Purpose: Initialize an object of type MyVector
    //Parameters: an integer.
    //Returns: nothing.
    //------------------------------------------------
    MyVector(int);

    //Purpose: Destroys objects of type MyVector
    //Parameters: none.
    //Returns: nothing
    //------------------------------------------------
    ~MyVector();

    //Purpose: Returns the current size of the MyVector.
    //Parameters: none.
    //Returns: the size.
    int size() const;

    //------------------------------------------------
    //Purpose: Returns the capacity of the MyVector.
    //Parameters: none.
    //Returns: int.
    int capacity() const;

    //------------------------------------------------
    //Purpose: Removes the entries of MyVector.
    //Parameters: none.
    //Returns: nothing.
    void clear();

    //------------------------------------------------
    //Purpose: Appends a given integer to the vector.
    //Parameters: an integer.
    //Returns: nothing.
    void push_back(T);

    //------------------------------------------------
    //Purpose: Shows what's at a given position.
    //Parameters: an integer index.
    //Returns: an integer.
    T at(int) const;

    MyVector(const MyVector& b);
    const MyVector& operator=(const MyVector&);

    //------------------------------------------------

private:
    int _size;
    int _capacity;
    int* head;

    //Purpose: Increases the capacity of a MyVector when it's 
    // capacity is equal to it's size. Called by push_back(int)
    //Parameters/Returns: nothing.
    void increase();

    //Purpose: Copies the given vector reference. 
    //Param: MyVector reference.
    //Returns: nothing.
    void copy(const MyVector&);

    //Purpose: Frees MyVector up for an assignment.
    void free();
};

template <class T>
ostream& operator<<(ostream&, const MyVector<T>);
//This line is giving me the error.

#endif

我在一个单独的文件中有操作员的代码:

template <class T>
ostream& operator<<(ostream& os, const MyVector<T> V){
    int N = V.size();
    os << endl;
    for(int i = 0; i<N; i++){
        os << V.at(i)<<endl;
    }
    return os;
}

我查看了其他问题,但似乎没有一个与我的匹配。帮助将不胜感激。谢谢!

【问题讨论】:

  • 你在某处有using std::ostream;using namespace std;(恶心)吗?如果没有,编译器就不会知道ostream 是什么。
  • 有人可以压缩一下上面代码的格式吗?
  • 解释你在哪里定义了模板类。在同一个文件或不同的文件中。
  • 评论应该告诉我们什么是不明显的。您不需要我们告诉默认 ctor 的用途是什么,或者它的参数,或者它的返回值。 increase 的目的评论很好。最糟糕的是,当您将其制作为模板但忘记更新 cmets 时,所有 cmets 都已过时;这些现在很混乱,

标签: c++ templates compiler-errors


【解决方案1】:

您可能需要将ostreamstd 限定为:

std::ostream&amp; operator&lt;&lt;(std::ostream&amp;, const MyVector&lt;T&gt;);

你几乎肯定应该通过 const 引用而不是 const 值来传递你的向量。

【讨论】:

    【解决方案2】:

    您不能在一个文件中声明一个模板,然后将它定义到另一个文件中。你只能定义它。

    希望对你有所帮助。

    【讨论】:

    • 是的,你可以。但是即使不能,也不会导致这里报错。
    【解决方案3】:

    您应该让 operatorMyVector 的常量引用,而不仅仅是一个常量,因为您所做的是创建向量的副本以传递给函数。

    template <class T> std::ostream& operator<<(ostream& o, const MyVector<T>& V)
    

    【讨论】:

      【解决方案4】:

      预期的构造函数、析构函数或类型转换表明它不将 ostream 识别为类型(即,它尚未在此范围内声明)。仅使用 std:: 前缀不足以解决问题;您还必须包含依赖文件。

      #include <ostream> 
      

      【讨论】:

        【解决方案5】:

        无需声明

        template <class T>
        ostream& operator<<(ostream&, const MyVector<T>);
        

        你可以关注这个

        #include <iostream>
        using namespace std;
        template <class T>
        class A
        {
        public:
        A();
        int _size;
        };
        template <class T>
        ostream& operator<<(ostream&, const A<T> p)
        {
        cout<<"in ostream"<<endl;
        }
        template <class T>
        A<T>::A()
        {
        _size = 90;
        cout<<_size<<endl;
        }
        int main()
        {
        A<int> ob;
        cout<<ob;
        return 0;
        }
        

        【讨论】:

          猜你喜欢
          • 2011-12-04
          • 2010-12-12
          • 2011-06-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多