error: 'Vector Vector::operator*(float, const Vector&) const' must have either 0 or one argument

问题描述 投票:0回答:0
#include <iostream>
#include <cstdlib>
#include <ctime>

class Vector
{
private:
    int length;
    float * elements;
    bool is_column;//column-vector = single column, multiple rows.
                   //row-vector = singl row, multiple columns.
public:
    Vector()
    {
        this->length = 0;
        elements = NULL;
        is_column = true;
    }
    void set_length(int length)
    {
        this->length = length;
        elements = new float[length];
    }
    void set_row_vector(bool is_row)
    {
        is_column = !is_row;
    }
    float get_length() const
    {
        return length;
    }
    // Copy constructor
    Vector(const Vector& other)
    {
        this->length = other.length;
        this->elements = new float[length];
        for (int i = 0; i < length; i++) 
        {
            this->elements[i] = other.elements[i];
        }
    }
    // Destructor
    ~Vector()
    {
        this->length = 0;
        if (elements != NULL) 
        {
            delete[] elements;
        }
    }

    // Overloaded assignment operator
    Vector& operator=(const Vector& other)
    {
        if (this != &other) 
        {
            this->length = other.length;
            delete[] this->elements;
            this->elements = new float[length];
            for (int i = 0; i < length; i++) 
            {
                this->elements[i] = other.elements[i];
            }
        }
        return *this;
    }
    float& operator[](int index)//will modify the state of the object 
                                //when assigned a value
    {
        return elements[index];
    }
    bool operator==(const Vector& other) const//this function will not modify the state 
                                                //of the object on which it is called
    {
        if (this->length != other.length) 
        {
            return false;
        }
        for (int i = 0; i < this->length; i++) 
        {
            if (this->elements[i] != other.elements[i]) 
            {
                return false;
            }
        }
        return true;
    }   
    void scalar_product(float scalar)
    {
        for(int i=0 ; i<length ; i++)
        {
            this->elements[i] = this->elements[i] * scalar;
        }
    }
    Vector operator*(float scalar, const Vector& vec) const
    {
        Vector result(vec);
        result.scalar_product(scalar);
        return result;
    }
    void display() const
    {
        if (length == 0) 
        {
            std::cout << "[]" << std::endl;
            return;
        }
        
        if (is_column)
        {
            std::cout << "[";
            for (int i = 0; i < length - 1; i++)
            {
                std::cout << elements[i] << std::endl;
            }
            std::cout << elements[length - 1] << "]" << std::endl;
        }
        else
        {
            std::cout << "[";
            for (int i = 0; i < length - 1; i++)
            {
                std::cout << elements[i] << " ";
            }
            std::cout << elements[length - 1] << "]" << std::endl;
        }       
    }

    bool is_column_vector() const
    {
        return is_column;
    }
    
    float dot_product(const Vector& other) const
    {
        if (!this->is_column || !other.is_column_vector())
        {
            std::cout << "Error: At least one vector must be a row vector" << std::endl;
            return;
        }
        
        if (this->length != other.length) 
        {
            std::cout << "Error: Vectors must have the same length to perform Dot product" << std::endl;
            return 0.0;
        }
        float result = 0.0;
        for (int i = 0; i < this->length; i++) 
        {
            result += this->elements[i] * other.elements[i];
        }
        return result;
    }
    void hadamard_product(const Vector& other)
    {
        if (this->is_column || !other.is_column_vector())
        {
            std::cout << "Error: rhs must be a row-vector" << std::endl;
            return;
        }
        
        if (this->length != other.length) 
        {
            std::cout << "Error: Vectors must have the same length to perform Hadamard product" << std::endl;
            return;
        }
        for (int i = 0; i < this->length; i++) 
        {
            this->elements[i] = this->elements[i] * other.elements[i];
        }
    }
};

int main()
{
    
    float array[] = {1.0, 2.0, 3.0};
    
    Vector v;
    v.set_length(3);
    v[0] = array[0];
    v[1] = array[1];
    v[2] = array[2];
    
    v.display();
    
    v.scalar_product(2.0);
    
    v.display();
}

输出

PS C:\git\Matrix> g++ matrix3x3.cpp -o matrix3x3.exe
matrix3x3.cpp:95:16: error: 'Vector Vector::operator*(float, const Vector&) const' must have either zero or one argument
   95 |         Vector operator*(float scalar, const Vector& vec) const
      |                ^~~~~~~~
matrix3x3.cpp: In member function 'float Vector::dot_product(const Vector&) const':
matrix3x3.cpp:139:25: error: return-statement with no value, in function returning 'float' [-fpermissive]
  139 |                         return;
      |                         ^~~~~~
PS C:\git\Matrix>
  • 重载的运算符导致第一个错误。该运算符对操作数右侧的向量进行运算。所以,我不明白为什么这是一个错误。

我该如何解决这个问题?

c++ operator-overloading
© www.soinside.com 2019 - 2024. All rights reserved.