- #1

ChrisVer

Gold Member

- 3,373

- 459

Code:

```
#ifndef Vec2D_H
#define Vec2D_H
class Vec2D
{
private:
double fX;
double fY;
public:
Vec2D();
Vec2D(double x=0, double y=0);
~Vec2D() {};
double const X();
double const Y();
void SetX(double a);
void SetY(double b);
void print();
Vec2D addVector(const Vec2D& v2) const;
double scalarProduct(const Vec2D& v2) const;
};
#endif
```

Code:

```
#include "Vec2D.h"
#include <iostream>
Vec2D::Vec2D(){
fX= 0.;
fY= 0.;
}
Vec2D::Vec2D(double x, double y)
{
fX=x;
fY=y;
}
void Vec2D::SetY(double a){
fY=a;
}
void Vec2D::SetX(double b){
fX=b;
}
double const Vec2D::X(){
return fX;
}
double const Vec2D::Y(){
return fY;
}
void Vec2D::print(){
std::cout<<"("<<fX<<", "<<fY<<")";
}
Vec2D Vec2D::addVector(const Vec2D& v2) const
{
double v2_x,v2_y,v1_x,v1_y;
v2_x=v2.fX;
v2_y=v2.fY;
v1_x=fX;
v1_y=fY;
double x_new, y_new;
x_new= v1_x +v2_x;
y_new= v1_y +v2_y;
return Vec2D(x_new, y_new);
}
double Vec2D::scalarProduct(const Vec2D& v2) const
{
double v2_x, v2_y, v1_x, v1_y,prod;
v2_x= v2.fX;
v2_y= v2.fY;
v1_x=fX;
v1_y=fY;
prod=(v2_x*v1_x)+(v2_y*v1_y);
return prod;
}
```

"[linker error] undefined reference to Vec2D::addVector(Vec2D&)"

"[linker error] undefined reference to Vec2D::scalarProduct(Vec2D&)"

And this error started appearing when I tried to implement the keyword "const" in my arguments of those two methods (const Vec2D) and at the end of the methods' names METHOD(ARGS) const{ ... }.

Without that keyword my code runs smoothly...any help? Thanks.

ooh yes my main code also is:

Code:

```
#include <iostream>
#include "Vec2D.h"
int main(){
Vec2D a= Vec2D(1.,2.);
Vec2D b= Vec2D(3.,4.);
Vec2D c= Vec2D(2.,-1.);
Vec2D sum = a.addVector(b);
sum.print();
double p1= a.scalarProduct(b);
double p2= a.scalarProduct(c);
double p3= c.scalarProduct(b);
std::cout<<p1<<"\t"<<p2<<"\t"<<p3<<std::endl;
system("pause");
return 0;
}
```