2011-04-12 65 views
1

我得到在我的项目一个神秘的错误,即:预期的构造函数,析构函数或类型转换之前和令牌

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

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

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

+3

你有一个'使用的std :: ostream的;'或'一个使用空间std ;'(yuck)某处?如果不是这样,编译器不会知道'ostream'是什么。 – 2011-04-12 02:45:35

+0

有人可以挤上述代码的格式吗? – iammilind 2011-04-12 02:48:52

+0

解释您已定义模板类的位置。在同一个文件或不同的文件中。 – 2011-04-12 03:13:05

回答

0

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

希望它对你有所帮助。

+0

是的,你可以。但即使您不能,也不会导致此处报告的错误消息。 – 2011-04-12 03:47:31

2

你可能需要限定ostreamstd为:

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

而且你几乎应该通过const引用路过你的载体,而不是常量的值。

0

你应该让运营商< <函数接受一个恒定的参考MyVector而不仅仅是一个恒定的,因为你在做什么是创建载体的拷贝传递给函数。

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

预期构造,析构函数,或类型转换表明,它不能识别的ostream作为一种类型的(即,它没有被在该范围内声明)。使用std ::前缀并不足以解决问题;您还必须包含依赖文件。

#include <ostream> 
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; 
} 
相关问题