c++ - 错误 C++ 运算符重载

标签 c++

我正在编写一个获取 vector 并修改它们的程序 但它给了我错误: “变量‘Vector’不是类型名称”

我是 C++ 重载运算符的新手,所以它们可能有很多语法错误

这里有什么问题,是在函数声明中还是在实现中

另一件事,我什么时候使用带有参数的 const?

class Vector{
private:
    int n;
    int *vArray;
public:
    Vector();
    Vector(int);
    ~Vector();
    friend istream& operator>>(istream& is , Vector &v );
    friend ostream& operator>>(ostream& os  , const Vector &v);
    Vector& operator+=(const Vector& );
    Vector operator+(Vector& rhs);
    double operator*(Vector&);
    Vector operator-(Vector);

};

Vector::Vector(){
    n = 2;
    vArray = new int[n];

}


Vector::Vector(int num){
    n = num;
    vArray = new int[n];
}

Vector::~Vector(){
    delete [] vArray;


}


istream& operator>>(istream& is, Vector& v){
    for (int i = 0; i < v.n; i++)
        is >> v.vArray[i];
    return is;

}
ostream& operator>>(ostream& os, const Vector& v){
    for (int i = 0; i < v.n; i++)
        os >> v.vArray[i];
    return os;


}

Vector& :: Vector operator+=(const Vector& rhs){
    if(n == rhs.n){
        for (int i = 0; i < n; i++){
            this->vArray[i] += rhs.vArray[i];
        }
        return *this;
    }
    cout << "Error: size not match" << endl;
}

Vector:: Vector operator+( Vector& rhs){
    if (n == rhs.n){
        Vector z(n);
        for (int i = 0; i < n; i++)
            z.vArray[i] = this->vArray[i] + rhs.vArray[i];

       return z;
    }
    cout << "Error: size not match"<<endl;


}


double::Vector operator* (Vector& v){
    double count;
    if (n == v.n){
        for (int i = 0; i <n; i++){
            count += this->vArray[i] * v.vArray[i];
        }
        return count;

    }
    cout << "Error: size not match";

}


Vector::Vector operator-(Vector v){
    if (n == v.n){
        Vector z(n);
    for (int i = 0; i < n; i++){
        z.vArray[i]=this->vArray[i]-v.vArray[i];

    }
    return z;
}
    cout << "Error: size not match" << endl;

}

最佳答案

这里有一些错误,在以下几行:

Vector& :: Vector operator+=(const Vector& rhs){
Vector& :: Vector operator+=(const Vector& rhs){
Vector:: Vector operator+( Vector& rhs){
double::Vector operator* (Vector& v){
Vector::Vector operator-(Vector v){

这些应该是:

Vector& Vector::operator+=(const Vector& rhs){
Vector& Vector::operator+=(const Vector& rhs){
Vector Vector::operator+( Vector& rhs){
double Vector::operator* (Vector& v){
Vector Vector::operator-(Vector v){

not a typename 错误可能来自 Vector::Vector 错误,因为您应该返回一个类型而不是识别 Vector 的构造函数。

此外,您的 vector 类没有定义/删除复制构造函数或赋值运算符。如果您复制此类的实例,那么它将使用默认的复制/分配,这将简单地复制指针成员的值而不执行深复制——您最终会得到双重删除,这将使您的程序崩溃。

对于 const 参数问题.. 只要函数不修改传入的值,就应该使用 const 引用。此外,如果函数本身不修改类的对象,则成员函数也应该是 const。 例如,operator+ 应该接受一个 const 参数并且它本身也是 const:

Vector Vector::operator+(Vector const& rhs) const

最好尽可能使用 const - 放宽限制通常比追溯添加限制更容易。

看看这个: http://www.parashift.com/c++-faq/const-correctness.html

关于c++ - 错误 C++ 运算符重载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26074990/

相关文章:

python - C++ 和 cython - 寻求一种避免模板限制的设计模式

c++ - Visual Studio 6 何时捕获结构化异常?

c++ - 代码是C还是C++?

c++ - 如何正确删除从另一个 DLL 接收的派生对象?

c++ - 现在是将 QThread 与 QProcess 结合使用的合适时机吗?

c++ - 为 std::tuple 重载运算符 << - 可能的简化?

c++ - 创建一个临时传递给右值引用

c++ - 使用 map 将字符串与枚举连接起来

c++ - 重载运算符保持变量更新而不是保持原始变量

c++ - 带 API 的实时频谱分析仪