在 & 令牌之前进行预期的构造函数、析构函数或类型转换
expected constructor, destructor, or type conversion before & token
我在我的项目中得到一个神秘的错误,说:
期望的构造函数、析构函数或类型转换
它不允许我使用过载的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;
}
我看了其他问题,但似乎没有一个与我的问题相符。非常感谢你的帮助。谢谢!
您可能需要将ostream
与std
限定为:
std::ostream& operator<<(std::ostream&, const MyVector<T>);
几乎可以肯定的是,你应该通过const引用传递vector,而不是const value。
你不能在一个文件中声明模板,然后在另一个文件中定义它。你只能定义它
希望对你有帮助。
您应该使操作符<<函数接受对MyVector
的常量引用,而不仅仅是常量引用,因为您所做的是创建vector的副本并将其传递给函数。
template <class T> std::ostream& operator<<(ostream& o, const MyVector<T>& V)
预期构造函数、析构函数或类型转换表明它不将ostream识别为类型(即,它没有在此范围内声明)。前缀std::并不足以解决问题;还必须包含依赖文件。
#include <ostream>
不需要声明 this:
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;
}
相关文章:
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- new[] / delete[] 并在C++中抛出构造函数/析构函数
- 创建/向容器添加对象时如何使用构造函数/析构函数
- LNK2019构造函数/析构函数使用 C++ Dll
- 有人可以解释一下这里发生了什么(类和构造函数/析构函数)吗?
- 带智能ptr的Pimpl-为什么需要构造函数/析构函数
- 需要构造函数/析构函数/类型转换错误
- Boost共享指针构造函数/析构函数
- 通过值或引用调用时未调用构造函数/析构函数.不管怎样
- C++构造函数析构函数中的奇怪行为
- 构造函数/析构函数链接错误
- 在模板化函数中调用具有特征的显式构造函数/析构函数
- c++构造函数/析构函数
- 为什么赋值的成本不能保证小于构造函数-析构函数对
- 重写默认构造函数/析构函数而不实现它们
- 使用自定义分配器调用对象构造函数/析构函数
- 涉及类和结构的构造函数/析构函数
- 在抽象构造函数/析构函数中调用纯虚拟函数安全吗
- 如果在没有限定的情况下从构造函数/析构函数调用虚拟函数,则是否会发生虚拟调度
- C++基于堆栈的构造函数/析构函数无法按预期工作