Sign in to follow this  
Matthew Shockley

Simple Class Errors

Recommended Posts

Can someone please explain to me what I am doing wrong? I am trying to make a simple class:
class Quad {
protected:
	int X;
	int Y;
public:
	void SetValues(int x, int y) { X, Y = x, y; };
};
And when I make a new Quad:
Quad Billy;
Billy.SetValues(5,5);
But every time I try to compile, I get these errors:
1>c:\users\matt\documents\visual studio 2008\projects\ustest\ustest\objects.h(10) : error C2143: syntax error : missing ';' before '.'
1>c:\users\matt\documents\visual studio 2008\projects\ustest\ustest\objects.h(10) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\matt\documents\visual studio 2008\projects\ustest\ustest\objects.h(10) : error C2371: 'Billy' : redefinition; different basic types
1>        c:\users\matt\documents\visual studio 2008\projects\ustest\ustest\objects.h(9) : see declaration of 'Billy'

Share this post


Link to post
Share on other sites
X, Y = x, y; does not do what you think it does. Try X = x; Y = y; instead.

EDIT: I hadn't seen your latest post. Anyway, you can't just call a function like that. You'll have to do that inside another function.

Share this post


Link to post
Share on other sites
Quote:
Original post by Matthew Shockley
Oh, that works. Why does it do that?

That's just the proper way of saying what you're trying to say in C++. Assignment is a statement. Statements end in semicolons. To assign a sequence of values to a sequence of variables, you'll have to explicitly put them in lists. For example, you could do:

class Quad
{
protected:
vector<int> mValues;

public:
void SetValues(const vector<int>& values) { mValues = values; }
};


Not that I would recommend this in such a simple case.

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