overloading *, +, -'operators for vector<double> class
Solution 1
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.
Solution 2
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;
}
Solution 3
The linker error tells you that your code is missing definitions of two member functions that you declared - the constructor and the destructor:
Line::Line() {
// Code of the constructor goes here
}
Line::~Line() {
// Code of the destructor goes here
}
Lucas Da Rocha Souza
Updated on June 04, 2022Comments
-
Lucas Da Rocha Souza almost 2 years
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