Sign in to follow this  

Basic classes help

This topic is 3317 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

class Normals
{
	public:
		vector calculateNormals(vector v[3]);

	protected:

	private:

		typedef struct
		{
			float x, y, z;
		}vector;
	
};

vector Normals::calculateNormals(vector v[])
{
    .....
}
My errors: error C2146: syntax error : missing ';' before identifier 'calculateNormals' error C4430: missing type specifier - int assumed. Note: C++ does not support default-int error C2061: syntax error : identifier 'vector' and the list goes on with the same type of errors What am I doing wrong? :S

Share this post


Link to post
Share on other sites
1. Make sure you arent including <vector> and using the std namespace anywhere. This would interfere with your custom vector struct.

2. Don't you have to declare the struct before you use it??

3. You might as well use the C++ way of defining a struct: struct vector {float x...};

Share this post


Link to post
Share on other sites
did you include the STL <vector> at any point? If so did you do "using std::vector" or "using namespace std"? If you did you will want to change your vector to something else, Vector would work (but be ambiguous).

Your errors look like it wants to do std::vector<int> but is failing.

Share this post


Link to post
Share on other sites
[edit] Dang, beat by ouraqt and SomeoneX. Yes, everything he said is something you should pay attention to, although it's clear you aren't putting std::vector in the global scope. Anyway, you try to use vector in the public scope, but you declare it as private. How are people supposed to create vector objects and pass them into the function if the type is private?

You need to declare vector before you use it and make it public if others are going to use it. In code it looks like:
class Normals
{
public:
// This is the C++ way of defining structs
struct vector
{
float x, y, z;
};

vector calculateNormals(vector v[3]);
};

vector Normals::calculateNormals(vector v[])
{
.....
}




Quote:

Your errors look like it wants to do std::vector<int> but is failing.


Actually it's not. When you try to create a variable with an undefined type, the compiler complains and assumes the undefined type is just an int (at least MSVC does). In other words, the compiler has no idea what a vector is, so it's going to give you an error and pretend it is an int. This means that there isn't an std::vector ambiguity, but an undefined data type.

Share this post


Link to post
Share on other sites
class Normals
{
public:
// This is the C++ way of defining structs
struct vector
{
float x, y, z;
};

vector calculateNormals(vector v[3]);
};

//Its now complaining on this:
vector Normals::calculateNormals(vector v[])
{
.....
}






Anyone know why? Thanks for the help.

EDIT: Aha! So I need to call it like this vector::Normals Normals::calculateNormals(vector v[]).

Thanks for the fast help!

EDIT2: How do I call the vector strut outside of the Class? Sorry for noob questions but Im new to OOP.

Share this post


Link to post
Share on other sites
Quote:
Original post by TutenStain
*** Source Snippet Removed ***

Anyone know why? Thanks for the help.

EDIT: Aha! So I need to call it like this vector::Normals Normals::calculateNormals(vector v[]).

Thanks for the fast help!


Don't you mean Normals::vector Normals::calculateNormals(vector v[])? [grin]

You have to do Normals::vector to access the vector struct. This tells the compiler "Hey, see the Normals class? Go inside of it. Now you see the vector struct? I want to use that one."

Share this post


Link to post
Share on other sites
Quote:
Original post by MikeTacular
Quote:
Original post by TutenStain
*** Source Snippet Removed ***

Anyone know why? Thanks for the help.

EDIT: Aha! So I need to call it like this vector::Normals Normals::calculateNormals(vector v[]).

Thanks for the fast help!


Don't you mean Normals::vector Normals::calculateNormals(vector v[])? [grin]


O snap, haha Thanks. But this is getting confusing. Why do I call the vector struct using this:

Normals::vector
and not
Normals test;
test.vector

Share this post


Link to post
Share on other sites
Quote:
Original post by TutenStain
O snap, haha Thanks. But this is getting confusing. Why do I call the vector struct using this:

Normals::vector
and not
Normals test;
test.vector


:: is the C++ Scope Resolution Operator. More info here. The period (.) is the Member Access Operator. I'll try to explain the difference.

When you define a struct inside of a class, a class inside of a namespace, etc, you change that class's (or struct's) scope. This explains what scope is. Basically it tells the compiler where something can be used. Since you created the struct inside of a class, it's scope is inside of that class. That means that it can be accessed only from inside of the class. If you are outside of the class and you want the struct, you have to tell the compiler to go inside of the class and get the struct for you.

First, the '.' operator is very different (kind of). It is used to refer to members of structs, classes, unions, etc. Members can be variables or functions, but not data types. vector is a struct, which is not a variable or function, so it is not a member, which means you can't access it through the '.' operator.

[edit]

Quote:
Original post by rip-off
To be honest I would make vector a separate type - rather than a nested type. They are common enough in game programming, I'm sure you'll find plenty of uses for them.


QFT. TutenStain, you may want to rethink your design here. Vectors are used for more than just normals. It doesn't make sense to stick vector inside of Normals since vector will probably be used in other situations where Normals is not, so it would be unintuitive to have to do Normals::vector just to get the vector struct when you aren't doing anything with Normals.

Share this post


Link to post
Share on other sites

This topic is 3317 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.

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