2011-04-12 68 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; 
} 
相關問題