Structure in a class HELP ME!!!

i am having a lot of trouble! I am knew to c++!

1
2
3
4
5
6
7
8
9
10
11
12
13
class farm_animals
{
	public:
	struct cow
	{
		int value;
	};
};

farm_animals farm_object;
farm_object.cow animal; //this line dosnt work

int main(){random stuff here not yet needed}


i cant seem to use the cow structure, it says this:

||=== Build: Debug in HelloWorld (compiler: Microsoft Visual C++ 2010) ===|
main.cpp|14|error C2143: syntax error : missing ';' before '.'|
main.cpp|14|error C4430: missing type specifier - int assumed. Note: C++ does not support default-int|
main.cpp|14|error C2371: 'object' : redefinition; different basic types|
main.cpp|14|error C2653: 'cow' : is not a class or namespace name|
||=== Build failed: 4 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|


how do i access the structure?
Last edited on
If you want an instance external to your farm_animals class, you'll need something like:
farm_animals::cow myCow;

To create an internal instance in the farm_animals class, you'd do:
1
2
3
4
5
6
7
8
9
10
class farm_animals
{
    public:
    struct cow
    {
        int value;
    };

    cow myCow;
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class farm_animals
{
    public:

        struct cow { int value = 0 ; }; // type cow

        cow my_cow ; // member variable  (object of type cow)
};

namespace wild_animals
{
    struct tiger { int value = 0 ; };
}

int main()
{
    farm_animals::cow c ;

    wild_animals::tiger t ;

    farm_animals my_animals ;
    my_animals.my_cow.value += 100 ;
}
okay that works! but how to I change the int value of the myCow:

myCow.value = 10; i tried this, but it comes up with errors
but it comes up with errors

Fairly useless statement :)
What errors? what's your code look like now?

edit:
remember.. you need to do this:

animals.myCow.value = 10;

OR something like this if you want to hide your cow from the outside world:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class farm_animals
{
private:
	struct cow
	{
		int value;
	};
	cow myCow;

public:
	void setCowValue(int val)
	{
		myCow.value = val;
	}
};

int main()
{
	farm_animals animals;
	animals.setCowValue(10);

	return 0;
}

Last edited on
come up with this:

1>c:\users\host\documents\visual studio 2010\projects\game1\game1\main.cpp(14): error C2143: syntax error : missing ';' before '.'

1>c:\users\host\documents\visual studio 2010\projects\game1\game1\main.cpp(14): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

1>c:\users\host\documents\visual studio 2010\projects\game1\game1\main.cpp(14): error C2371: 'myCow' : redefinition; different basic types

the "myCow" is outside the class so i thought i wouldnt have to to the "farm_animals." before "myCow". but then again i am still learning c++
i've updated my post with an example.

the "myCow" is outside the class so i thought i wouldnt have to to the "farm_animals." before "myCow"

Nearly. you have to create a farm_animals object first and use this. dont use the class itself.
Last edited on
hmmmm. still pretty rusty...this here isnt working:

1
2
3
4
5
6
7
8
9
10
11
class farm_animals
{
	public:
	struct cow
	{
		int value;
	};
};
farm_animals farm_object;
farm_object::cow myCow;
farm_object.myCow.value = 10;


it comes up with:

'farm_object' : left of '::' must be a class/struct/union
1>c:\users\host\documents\visual studio 2010\projects\game1\game1\main.cpp(13): error C2146: syntax error : missing ';' before identifier 'myCow'

1>c:\users\host\documents\visual studio 2010\projects\game1\game1\main.cpp(13): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

1>c:\users\host\documents\visual studio 2010\projects\game1\game1\main.cpp(13): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

1>c:\users\host\documents\visual studio 2010\projects\game1\game1\main.cpp(14): error C2143: syntax error : missing ';' before '.'

1>c:\users\host\documents\visual studio 2010\projects\game1\game1\main.cpp(14): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

1>c:\users\host\documents\visual studio 2010\projects\game1\game1\main.cpp(14): error C2371: 'farm_object' : redefinition; different basic types



and in particular this error dosnt make too much sense to me:
syntax error : missing ';' before '.'
yours:
1
2
3
4
5
6
7
8
class farm_animals
{
	public:
	struct cow
	{
		int value;
	};
};


mine:
1
2
3
4
5
6
7
8
9
class farm_animals
{
public:
	struct cow
	{
		int value;
	};
	cow myCow;
};


You no longer have a cow object in your farm_animals class. Yes you have a cow structure, but you dont have an instance of a cow.
This is the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;

class farm_animals
{
	public:
	struct cow
	{
		int value;
	};
	cow myCow;
};
farm_animals farm_object;
farm_object.myCow.value = 10;    //this line dosnt work

int main(){
	cin.ignore();
	return 0;
}



whenever i try to change the value of lets say myCow.value it comes up with the same errors ive had before, it dosnt matter if i do this:
1
2
3
4
5
6
7
8
9
class farm_animals
{
public:
	struct cow
	{
		int value;
	};
	cow myCow;
};

and create myCow inside the class it comes up with same errors, maybe its becasue of the way i change the myCow.value. I dont know what to type, should i type:
1
2
3
farm_animals farm_object;     //create object for class
farm_object::cow myCow;      //create instance of the cow structure called myCow
farm_object.myCow.value = 10;        //change the myCow instance's "int value" to 10 

that dosnt work.
or should i create cow instance inside the class, becasue nothing i do works, it always comes up with: syntax error : missing ';' before '.'

i have been trying to fix it for 2 days now and im getting sick of the same problem, I had a whole game that had 3 different files and it didnt work, so i made a more simple verson so i could see directly what the problem was, but no matter what i do the only way the program will run is if I dont change the value, everything else works.

This is what I think the problem is.... I think i dont correctly know the right syntax for changing the value, i thought it was this.
 
farm_object.myCow.value = 10;             

the farm_object for the class object, myCow for the cow structure instance and value for the "int value;" in the structure for cow.

Maybe the syntax is more complicated, but its very specific so there is very little information about this online, I just need C++ experts who have been doing C++ for long enough to understand what im doing wrong.
> farm_object::cow myCow;
> farm_object.myCow.value = 10;
> that dosnt work.

Use the name of the class farm_animals as the scope containing the type cow.
Not farm_object::cow myCow;
But farm_animals::cow myCow;

We cant have a discarded-value expression (such as an assignment; an expression which is only there for its side effects; the result of the evaluation is discarded) at namespace scope. Move the assignment expression to within the block scope of main().

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class farm_animals
{
public:
	struct cow
	{
		int value;
	};
	cow myCow;
};

farm_animals farm_object;     //create object of class
/*farm_object*/ farm_animals::cow myCow;      //create instance of the cow structure called myCow

// farm_object.myCow.value = 10; // **** error: can't have this here

int main()
{
    farm_object.myCow.value = 10; // assignment here is fine
}
oh my god it works, thank you, you know your stuff!
Topic archived. No new replies allowed.