Overloading *, +, -operators for vector class <double>
I am writing a Line class for creating numeric methods, and I want these operators (*, +, -) to make my code clearer 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 the Vector class? should i just execute functions (or methods) instead of operators? any other suggestions?
ps1: I am using Visual Studio 11 as the compiler.
ps2: I didn't run the project as a "win32 project", this is a console application.
I am getting 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
source to share
You need to overload the operators in the 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 far as linker errors go, it looks like you haven't implemented the Line constructor and destructor.
source to share
You should never inherit from std
-classes that are not intended to inherit. Inheriting classes that do not have a virtual destructor is very dangerous.
I suggest you use aggregation. Create a class Line
that contains a member of a type vector
named myVector_
, for example, and implement the required operators so that they use that member variable.
So, you replace all calls with size()
with 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;
}
source to share
Surely the right thing to do is to have the line of an INSIDE object and not "inherit" from a vector? As a rule, inheritance from containers is std::
not great data ... I am sure that "Line" is not actually a vector, but a vector. [The rule for "when you inherit" is "X is Y" where you create a composite object, when "X has Y" it means X is inside.]
You will need to declare your constructor and destructor to get rid of your binding error.
I would also use const Line&
as your input for math operations since you are not modifying the input.
source to share