Я получаю загадочную ошибку в моем проекте, которая говорит:
ожидаемый конструктор, деструктор или преобразование типов
Это не позволяет мне использовать мой перегруженный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;
}
I 'Мы смотрели на другие вопросы, но, похоже, ни один из них не соответствует моим.Помощь будет принята с благодарностью.Спасибо!