expected constructor, destructor, or type conversion before & token
I'm getting a mysterious error in my project that says:
expected 开发者_如何学Goconstructor, destructor, or type conversion
It's not allowing me to use my overloaded operator<<
. This previously worked before I made my class (myVector
) into a 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
I have the code for the operator in a seperate file:
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've looked at other questions, but none seem to match mine. Help would be greatly appreciated. Thanks!
You probably need to qualify ostream
with std
as:
std::ostream& operator<<(std::ostream&, const MyVector<T>);
And you should almost certainly be passing your vector by const reference, rather than const value.
You can't declare a template in a file and then define it into another file. You can only define it.
Hope it helped you.
You should make the operator<< function accept a constant reference to the MyVector
rather than just a constant, since what you are doing is to create a copy of the vector to pass into the function.
template <class T> std::ostream& operator<<(ostream& o, const MyVector<T>& V)
Expected constructor, destructor, or type conversion suggests that it does not recognize ostream as a type (ie, it has not been declared in this scope). Prefixing with std:: is not alone sufficient to resolve the issue; you must also include the dependent file.
#include <ostream>
No need to Declare this:
template <class T>
ostream& operator<<(ostream&, const MyVector<T>);
You can follow this
#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;
}
精彩评论