gpt4 book ai didi

c++ - &token 之前预期的构造函数、析构函数或类型转换

转载 作者:行者123 更新时间:2023-11-30 04:35:05 26 4
gpt4 key购买 nike

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

expected constructor, destructor, or type conversion

它不允许我使用重载的 operator<< .在我将我的类(class)( 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

我在一个单独的文件中有运算符(operator)的代码:

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

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

最佳答案

您可能需要符合条件 ostreamstd作为:

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

而且几乎可以肯定,您应该通过 const 引用而不是 const 值来传递 vector 。

关于c++ - &token 之前预期的构造函数、析构函数或类型转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5629819/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com