预期构造函数,析构函数或类型转换之前&代币

时间:2011-04-12 02:42:07

标签: c++ templates compiler-errors

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

  

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

它不允许我使用我的重载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;
}

我看过其他问题,但似乎没有一个问题与我的相符。非常感谢帮助。谢谢!

5 个答案:

答案 0 :(得分:2)

您可能需要将ostreamstd限定为:

std::ostream& operator<<(std::ostream&, const MyVector<T>);

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

答案 1 :(得分:0)

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

希望它对你有所帮助。

答案 2 :(得分:0)

你应该让运营商&lt;&lt;函数接受对MyVector的常量引用而不仅仅是一个常量,因为你正在做的是创建一个向量的副本以传递给函数。

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

答案 3 :(得分:0)

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

#include <ostream> 

答案 4 :(得分:0)

无需声明

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