Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

overloading *, +, -'operators for vector<double> class

I'm writing a Line class to make numerical methods and I want these operators (*, +, -) to make my code more readable and easier to understand.

        #include <vector>

        using namespace std;

        typedef vector<double> Vector;

        class Line : public Vector
        {
        public:
            Line();
            ~Line();

            Line operator+(Line);
            Line operator-(Line);
            Line operator*(double);
        };


        Line Line::operator*(double alfa)
        {
            Line temp;
            int n = size();
            temp.resize(n);
            for (int i = 0; i < n; i++)
            {
                temp.at(i) = this->at(i)*alfa;
            }
            return temp;
        }

        Line Line::operator+(Line line)
        {
            int n = size();
            Line temp;
            temp.resize(n);
            for (int i = 0; i < n; i++)
            {
                temp.at(i) = this->at(i) + line[i];
            }
            return temp;
        }

        Line Line::operator-(Line line)
        {
            int n = size();
            Line temp;
            temp.resize(n);
            for (int i = 0; i < n; i++)
            {
                temp.at(i) = this->at(i) - line[i];
            }
            return temp;
        }


        int main()
        {
            return 0;
        }

Is it possible to overload such operators from Vector class? should I just make functions (or methods) instead of operators? any other suggestions?

ps1: I'm using Visual Studio 11 as compiler.

ps2: I have not started the project as 'win32 project', it's console application.

I'm geting the following errors:

Error   1   error LNK2019: unresolved external symbol "public: __thiscall Line::Line(void)" (??0Line@@QAE@XZ) referenced in function "public: class Line __thiscall Line::operator*(double)" (??DLine@@QAE?AV0@N@Z) C:\Users\Lucas\Documents\Visual Studio 11\Projects\test\test\test.obj   test


Error   2   error LNK2019: unresolved external symbol "public: __thiscall Line::~Line(void)" (??1Line@@QAE@XZ) referenced in function "public: class Line __thiscall Line::operator*(double)" (??DLine@@QAE?AV0@N@Z)    C:\Users\Lucas\Documents\Visual Studio 11\Projects\test\test\test.obj   test
like image 690
Lucas Da Rocha Souza Avatar asked Feb 18 '23 19:02

Lucas Da Rocha Souza


2 Answers

You have to overload the operators at global scope:

vector<double> operator*(const vector<double>& v, double alfa)
{
    ...
}

vector<double> operator+(const vector<double>& v1, const vector<double>& v2)
{
    ...
}

vector<double> operator-(const vector<double>& v1, const vector<double>& v2)
{
    ...
}

As for the linker errors, it just looks like you didn't implement the Line constructor and destructor.

like image 106
user1610015 Avatar answered Feb 27 '23 02:02

user1610015


You should never inherit from std-classes which are not meant for inheritance. Inheriting from classes which do not have a virtual destructor is very dangerous.

I'd suggest you use aggregation: Make your Line class contain a member of vector type, named myVector_ for example, and implement the desired operators in a way that they use this member variable.

So you replace all calls to size() to myVector.size() etc:

Line Line::operator*(double alfa)
{
    Vector temp;
    int n = myVector_.size();
    temp.resize(n);
    for (int i = 0; i < n; i++)
    {
        temp.at(i) = myVector_.at(i)*alfa;
    }
    return temp;
}
like image 41
lethal-guitar Avatar answered Feb 27 '23 03:02

lethal-guitar