Creating a 2D Vector Using C++

In summary, the Vec2D class has a fX and fY field, a SetX and SetY methods, and a print method. The class also has addVector and scalarProduct methods. The Vec2D class can be used to create a vector that is the sum of two other vectors. When the Vec2D class is used with the const keyword, the compiler errors out.
  • #1
ChrisVer
Gold Member
3,378
464
I have the following class Vec2D:
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;  
}

My problem is when I am trying to do something with the Vec2D::scalarProduct() and Vec2D::addVector() methods I am getting an error of the following type:
"[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; 
}
 
Technology news on Phys.org
  • #2
Why do you need the 'const' keyword in there... from as much as I can tell it doesn't look like it's a constant at all... Why would you need a function that returns a constant?
 
  • #3
For example. const Vector2d::X returns fX, which isn't a constant, so it can't be evaluated at compile time, which is probably what the compiler is complaining about
 
  • #4
What is not constant?
the Vec2D::X() returns fX, but the return value is a constant... fX doesn't have to be... I don't think the compiler raises any error (at least I tried it now) with the assignment of non-constant objects to constants...
It raises an error if you try to change the const ones.

What I don't understand is why the code results to such an error after I inserted the const keywords on the addVector and scalarProduct methods.. From looking around for that error message I was left with the impression that there is a problem for the compiler when it tries to connect/match my cpp with my header file (and so the linker)... But in both those files I am using the same naming etc.
And let me elaborate with an example?
I have a vector A=(1,2)
and a vector B= (1,5)
I want the addition C=A+B = (2,7)
Now I can take the object A and using the method addVector add to its elements the vector's B ones? I can make sure that my B is not changed (read-only object) by passing it as a constant reference.
 
Last edited:
  • #5
Can you try omitting the const keyword from the cpp file and leaving it in the header file?

I'm no expert at this, but I've done this enough to know the frustration you can get from compiler errors!.. Maybe someone else will chime in.
 
  • #6
Rx7man said:
Can you try omitting the const keyword from the cpp file and leaving it in the header file?

Well I just did to make sure of my fear that it wouldn't work; omitting it of course results in not finding the method in the header file.
 
  • #7
I just understood that constants had to be evaluable at compile time, and an instance of your class will always return the same value, but each different instance will have a different return value, meaning it's not constant...
I'm going to fiddle a little with it myself :)
 
  • #8
ChrisVer said:
C:
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;
}
I could be wrong, but I think you really should remove the const attributes from your function return values, as also noted by Rx7man.

In addition, your definitions for the two functions I copied above are overly complicated. You should not have the declarations you show in the first line of each of these methods. In both methods the parameter is the vector that will be used to add to the vector on which the method is called, or to calculate the scalar product of the vector on which the method is called. One important fact that you're ignoring is that for methods of a class, the implied first argument is the this pointer, where this points to the instance of the class.

In the first method (addVector), this.fx and this.fy are the coordinates of the vector on which the method is called. The parameter, v2, has its own coordinates; namely, v2.fx and v2.fy.

Your addVector method could be rewritten like so:
C:
Vec2D Vec2D::addVector(const Vec2D& v2)
{
    Vec2D v_new(this->x, this->y);
    return v_new;
}

For this to work, though, you need a copy constructor, and to be able to use this method, you need to overload the assignment operator. Note that I have renamed your private class members from fX and fY to x and y.

The copy constructor looks like this:
C:
Vec2D::Vec2D(const Vec2D& v)
{
   this->x = v.x;
   this->y = v.y;
}

My overloaded assignment operator looks like this:
C:
Vec2D Vec2D::operator= (const Vec2D & v)
{
   this->x = v.x;
   this->y = v.y;
   return *this;
}
 
Last edited:
  • Like
Likes ChrisVer
  • #9
In addition to what I said above, my implementation of the scalarProduct() method has one line of code. Again, methods on classes always have an implied first argument, the this pointer, a pointer to (the address of) the object on which the method is called. You aren't using this in your method implementations.
 
  • #10
Mark44 said:
In addition to what I said above, my implementation of the scalarProduct() method has one line of code. Again, methods on classes always have an implied first argument, the this pointer, a pointer to (the address of) the object on which the method is called. You aren't using this in your method implementations.

Is the this keyword the C++ analogue of self in python? Although python doesn't have pointers for the ->.
 
  • #11
Yes - or as close as you can get. But you don't have to declare it in the method definition. It's just there automagically.
 
  • #12
ChrisVer said:
Is the this keyword the C++ analogue of self in python? Although python doesn't have pointers for the ->.
They're similar, with self in Python representing the object, and this being a pointer to the object.
 
  • #13
I don't know how it works in C++, but in VB, typically when you call the default constructor, you pass default default values to the non-default constructor...

so it would look something like this, and would reduce the locations of code to debug

Code:
Vec2D::Vec2D(){
     Vec2D(0,0);           //call the non default constructor with default values (syntax may not be right)
}

Vec2D::Vec2D(double x, double y)
{
    fX=x;   //do the initialization for both cases here in the non-default contructor.. and you may need "this" here as well
    fY=y;                   
}
 
  • #14
Rx7man said:
I don't know how it works in C++, but in VB, typically when you call the default constructor, you pass default default values to the non-default constructor...

so it would look something like this, and would reduce the locations of code to debug

Code:
Vec2D::Vec2D(){
     Vec2D(0,0);           //call the non default constructor with default values (syntax may not be right)
}

Vec2D::Vec2D(double x, double y)
{
    fX=x;   //do the initialization for both cases here in the non-default contructor.. and you may need "this" here as well
    fY=y;                  
}
In the code that @ChrisVer posted at the start of this thread, the header file contains these declarations:
C:
Vec2D();
Vec2D(double x=0, double y=0);
The first constructor can be removed; the second constructor passes default values of 0 and 0 if a Vec2D object is created with no initializers. If a Vec2D object is created with initializer values, those values are used to set the x and y members.
 
  • Like
Likes Rx7man

1. How can I create a 2D vector using C++?

To create a 2D vector using C++, you can use the vector class provided by the standard template library (STL). First, you need to include the vector header file in your code. Then, you can declare a 2D vector using the syntax vector>. For example, to create a 2D vector of integers, you can use vector>.

2. How do I initialize the elements in a 2D vector?

To initialize the elements in a 2D vector, you can use nested for loops. The outer loop will iterate through the rows of the vector, and the inner loop will iterate through the columns. You can use the push_back() function to add elements to the vector. For example, to initialize a 2D vector of size 3x3 with all elements as 0, you can use the following code:

vector> vec;for (int i = 0; i < 3; i++) {  vector row;  for (int j = 0; j < 3; j++) {    row.push_back(0);  }  vec.push_back(row);}

3. How can I access and modify elements in a 2D vector?

You can access and modify elements in a 2D vector using the [][] operator. The first set of brackets represents the row index and the second set represents the column index. For example, to access the element at row 2 and column 3 in a vector named vec, you can use vec[2][3]. To modify this element, you can simply assign a new value to it, like vec[2][3] = 5.

4. How can I resize a 2D vector?

To resize a 2D vector, you can use the resize() function. It takes two parameters: the new number of rows and the new number of columns. This function will automatically adjust the size of the vector and add or remove elements as needed. For example, to resize a 2D vector named vec to have 5 rows and 4 columns, you can use vec.resize(5, 4).

5. Can I use a 2D vector in a function as a parameter?

Yes, you can use a 2D vector as a parameter in a function. You can either pass it by value or by reference. Passing by value will create a copy of the vector, while passing by reference will allow the function to directly modify the original vector. To pass a 2D vector by reference, you can use the & symbol in the function declaration, like void myFunction(vector>& vec).

Similar threads

  • Programming and Computer Science
Replies
23
Views
1K
  • Programming and Computer Science
Replies
1
Views
649
  • Programming and Computer Science
2
Replies
35
Views
2K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
7
Views
1K
  • Programming and Computer Science
Replies
20
Views
1K
  • Programming and Computer Science
Replies
6
Views
2K
Replies
63
Views
3K
  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
15
Views
2K
Back
Top