在 & 令牌之前进行预期的构造函数、析构函数或类型转换

expected constructor, destructor, or type conversion before & token

本文关键字:构造函数 析构函数 类型转换 令牌      更新时间:2023-10-16

我在我的项目中得到一个神秘的错误,说:

期望的构造函数、析构函数或类型转换

它不允许我使用过载的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;
}

我看了其他问题,但似乎没有一个与我的问题相符。非常感谢你的帮助。谢谢!

您可能需要将ostreamstd限定为:

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