Reputation: 63
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
Upvotes: 4
Views: 21865
Reputation: 6678
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.
Upvotes: 7
Reputation: 129434
Surely the correct thing is to have a Vector object INSIDE line, and not "inherit" from Vector? Generally inheriting from std::
containers is not a great data... I'm pretty sure a "Line" is not actually a vector, it's a "has a" vector. [The rule for "when you inherit" is "X is a Y", where you make a composite object when "X has a Y" - so there is a Y inside X.]
You will need to declare your constructor and destructor to get rid of your linking error.
I would also use const Line&
as your input to the math operations, as you neve alter the input.
Upvotes: 0
Reputation: 4519
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;
}
Upvotes: 2
Reputation: 726779
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
}
Upvotes: 1