im new to programming and i am learning by trail and error as well as reading many books

im making a game engine and i have a class.i am wondering how to set certain pokemons base hp and attack stats like in a struct


//create pokemon
class pokemon
{
public:
int basehp;
int baseatk;
int basedef;
int basesatk;
int basesdef;
int basespe;



} bulbasaur, ivysaur, venasaur, charmander, charmeleon, charizard, squirtle, wartortle, blastoise;

i know in a struct i can just do this.

bulbasaur.basehp=49
bulbasaur.baseatk=49
bulbasaur.basedef=49
bulbasaur.basesatk=65
bulbasaur.basesdef=65
bulbasaur.basespe=45

but i get an error every time i do this in my compiler. i am using code::blocks in windows 7. does anybody know what i am doing wrong?
1
2
3
4
5
6
bulbasaur.basehp=49
bulbasaur.baseatk=49
bulbasaur.basedef=49
bulbasaur.basesatk=65
bulbasaur.basesdef=65
bulbasaur.basespe=45


None of these lines have a semi-colon at the end. Do they have one in your actual code?

If you tell us the compiler error, we can tell you how to fix it.
they do have semi-colons the error is "ERROR: bulbasaur does not name a type
That indicates that the compile, at that point in your code, has no idea what the object named bulbasaur is.

This code compiles and runs without problem:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>

int main()
{

//create pokemon
class pokemon
{
public:
int basehp;
int baseatk;
int basedef;
int basesatk;
int basesdef;
int basespe;
} bulbasaur, ivysaur, venasaur, charmander, charmeleon, charizard, squirtle, wartortle, blastoise;


 bulbasaur.basehp=49;
 bulbasaur.baseatk=49;
 bulbasaur.basedef=49;
 bulbasaur.basesatk=65;
 bulbasaur.basesdef=65;
 bulbasaur.basespe=45;

 std::cout << "All done";

 return 0;
}


Are you creating the object named bulbasaur before you try to use it?
i figured out a little more about what is going on here

i get two errors with this code below.

#include <iostream>
using namespace std;

//create pokemon
class pokemon
{
public:
int basehp;
int baseatk;
int basedef;
int basesatk;
int basesdef;
int basespe;
} bulbasaur, ivysaur, venasaur, charmander, charmeleon, charizard, squirtle, wartortle, blastoise, caterpie, metapod, butterfree, weedle, kakuna, beedrill, pidgey, pidgeotto, pidgeot, ratatta, raticate, spearow, fearow,;



bulbasaur.basehp=49;
bulbasaur.baseatk=49;
bulbasaur.basedef=49;
bulbasaur.basesatk=65;
bulbasaur.basesdef=65;
bulbasaur.basespe=45;
ivysaur.basehp=60;
ivysaur.baseatk=62;
ivysaur.basedef=63;
ivysaur.basesatk=80;
ivysaur.basesdef=80;
ivysaur.basespe=60;
venasaur.basehp=80;
venasaur.baseatk=82;
venasaur.basedef=83;
venasaur.basesatk=100;
venasaur.basesdef=100;
venasaur.basespe=80;


the first error is;

error:unexpected unqualified-id before ';' token


it is talking about the line of code where i name all of my objects. however i do not understand what is wrong with this line.

I SOLVED THIS IT WAS THE ',' BEFORE THE SEMI-COLON

the second error is;

error:'bulbasaur' does not name a type


and it is referring to every line of code where i set a value to a member such as

bulbasaur.basehp=49;
Last edited on
You have to put those lines of code inside main() or some function, you can't just have them floating there.
oh ok i did not know that thank you for clarification

may i put the function inside of a classes members?
Last edited on
If you did not have a main in your code, you need to stop, go back to the beginning, and start learning C++.

http://www.cplusplus.com/doc/tutorial/
i do have a main() it was just a question. it just seems more benifitial to encapsulate as much functionality that has to do with the class from the main as much as possible to me
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class pokemon
{
 public:
   int basehp;
   int baseatk;
   int basedef;
   int basesatk;
   int basesdef;
   int basespe;
}
   bulbasaur = {49, 49, 49, 65, 65, 45},
   ivysaur = {60, 62, 63, 80, 80, 60},
   venasaur = {80, 82, 83, 100, 100, 80},
   charmander,
   charmeleon,
   charizard,
   squirtle,
   wartortle,
   blastoise;

//... 
You can put functions inside your class, but main() must go outside of any class.

If you want to encapsulate the initialization of a class inside the class, you should be using constructors.

You might find this site's classes tutorials informative.
http://www.cplusplus.com/doc/tutorial/classes/
http://www.cplusplus.com/doc/tutorial/classes2/
Topic archived. No new replies allowed.