• Advertisement
Sign in to follow this  

Simple Class Errors

This topic is 2820 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
Post those two files in entirety: objects.h and whatever file has "Quad Billy" in it.

Share this post


Link to post
Share on other sites
Objects.h:

class Quad {
protected:
int X;
int Y;
public:
void SetValues(int x, int y) { X, Y = x, y; };
};


Objects.cpp:

#include "Objects.h"

Quad Billy;
Billy.SetValues(5,5);

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
This is it. I have other files, but none of them use these files because I just created them to test classes. I really have no clue how these errors are occurring.

Share this post


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

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
Sign in to follow this  

  • Advertisement