• 9
• 9
• 9
• 10
• 10

Simple Class Errors

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

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 on other sites
This is in Visual Studio 2008, BTW.

Share on other sites
Post those two files in entirety: objects.h and whatever file has "Quad Billy" in it.

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 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 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 on other sites
Oh, that works. Why does it do that?

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

Share on other sites
Quote:
 Original post by Matthew ShockleyOh, 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.