Sign in to follow this  
Zorro1267

C++ Class Question

Recommended Posts

What am I doing wrong in this code? I am trying to write my own class just for practice.
#include <iostream>
using namespace std;

class vector2d
{
public:
      int x,y;
public:
       void coordinates (int a, int b)
       {
            x = a;
            y = b;
       };
       
       int dot (vector2d a, vector2d b)
       {
           return (a.x * b.x + a.y * b.y);
       };
};

int main ()
{
    char c;
    vector2d  v1,v2;
    
    v1.coordinates(3,4);
    v2.coordinates(5,6);
    cout << "The dot product is:" << vector2d.dot(v1,v2); Compiler Error Here

    cin >> c;
    return (0);
}

The compiler error says: expected primary-expression before '.' token I am not sure what that means

Share this post


Link to post
Share on other sites
vector2d.dot(v1,v2) should be v1.dot(v1,v2) or v2.dot(v1,v2). This is kind of pointless though since you're passing the class to itself so you may want to change it to:


int dot ( vector2d v )
{
return ( ( x * v.x ) + ( y * v.y ) );
};


or make it a static method and call it as vector2d::dot(v1,v2)

Share this post


Link to post
Share on other sites
static int dot (vector2d a, vector2d b)...

...vector2d::dot(v1,v2);
There are a couple of other issues that could be addressed here, but I'll go ahead and post this (I've probably been beaten already :).

Share this post


Link to post
Share on other sites
Quote:
Original post by Scet
vector2d.dot(v1,v2) should be v1.dot(v1,v2) or v2.dot(v1,v2). This is kind of pointless though since you're passing the class to itself so you may want to change it to:


int dot ( vector2d v )
{
return ( ( x * v.x ) + ( y * v.y ) );
};


or make it a static method and call it as vector2d::dot(v1,v2)




Duh, of course. Feeling kind of dumb now. Anyways, what I was hoping to do is have a function called dot, that is part of the class, so that I can simply write

dot (v1,v2)

Share this post


Link to post
Share on other sites
Quote:
Original post by Zorro1267
Anyways, what I was hoping to do is have a function called dot, that is part of the class, so that I can simply write

dot (v1,v2)
Just make it a free function. The 'free' version would look just like your current version, but would live outside the class rather than being a member function.

Share this post


Link to post
Share on other sites
A couple more minor notes on your code (not comprehensive, and not compiled or tested):

#include <iostream>

// The 'using' directive is fairly harmless in this case, but
// we don't really need it here (and it's best not to just throw
// it in out of habit).
//using namespace std;

class vector2d
{
public:

int x, y;

public:

// You seemed to be using the 'coordinates' function as a constructor,
// more or less, in your original example, so let's create a
// proper constructor (or pair of constructors) instead:
vector2d() {}
vector2d(int x, int y) : x(x), y(y) {} // Note use of initializer list
};

// Passing arguments by constant reference rather than
// by value can be more efficient for non-trivial types:
int dot(const vector2d& a, const vector2d& b)
{
return a.x * b.x + a.y * b.y;
};

int main()
{
vector2d v1(3,4);
vector2d v2(5,6);

std::cout << "The dot product is:" << dot(v1,v2);

char c;
cin >> c;

// Not necessary - main() returns 0 by default.
//return (0);
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this