-
a class in a struct?
okay, I'm not sure this is legal, but here it goes...
I'm trying to have a struct with a class in it...
main.cpp
Code:
...
#include "foo.h"
...
fooStruct foo1;
foo1.bar1.hello();
...
foo.h
Code:
...
#include "bar.h"
...
struct fooStruct
{
barClass bar1;
}
...
bar.h
Code:
class barClass
{
public:
void hello();
...
it gives me errors in main... I mainly want to know
1) is this legal
2) if so, what am i doing wrong?
thanks in advance...
-
It is legal. I can't tell what the problem is from the code you posted though. Can you post a bit more, along with the errors?
-
here's some literal code:
ERRORS: (they get cut off where they are by the compiler :( )
271 C:\TCWIN45\BIN\dev c++\battle system\version 3\main.cpp
request for member `rules' in `game.gamesStruct::dice()', which
272 C:\TCWIN45\BIN\dev c++\battle system\version 3\main.cpp
request for member `run' in `game.gamesStruct::dice()', which is
Main.cpp
Code:
...
#ifndef _GAMES_H
#define _GAMES_H
#include "games.h" //gamesStruct for Merchant Alley
#endif
...
int main(){
...
gamesStruct game;
...
if(ans==1)
{
game.dice.rules();
game.dice.run();
}
...
ALL of games.h
Code:
#ifndef _DICE_H
#define _DICE_H
#include "dice.h"
#endif
struct gamesStruct
{
diceClass dice(); //create instance of dice class
};
ALL of dice.h
Code:
class diceClass
{
public:
diceClass(); //initializer
void run(char*); //starts with username
void rules(); //prints rules
private:
void setstats(char*); //sets stats
void savestats(); //saves changed stats
int rolldice(); //returns random number between 1..6
char username[20]; //holds username
char password[20]; //holds password
int hp,atk,def,lvl,exp,mon,pts; //stats
};
and below can be found dice.cpp... it's too big to post...
-
Ok I downloaded your file and opened it up and this was all that was in it
Code:
/*
games.cpp
gamesStruct definition/prototype
John Shao
*/
struct gamesStruct
{
diceClass dice(); //create instance of dice class
};
Can you post all your code? From what I can see, the first thing that is wrong is this you are forgetting to include "games.h" in your .cpp file.
next thing is this statment:
diceClass dice(); //create instance of dice class
it should be just this:
diceClass dice
when you declare a stuct or class within anouther class, you don't need the (). Basically what you are doing is calling the constructor for dice and you don't need to.
Code:
int main()
{
gamesStruct game()
return(0);
}
diceClass will call its constructor on its own when game is created
also, in the code that you posted here, it looks like you declared gamesStruct in the games.h file but it is also declared in the games .cpp file I downloaded. if you declared it twice then you will get a lot of errors. The complete code you have so far will show us what you actually have going on so if you can post it I will try to help you.
-
okay, it's quite a bit of code that doesn't have anythign to do with this, but here it is:
main.cpp
-
-
-
-
hmm ... where should I start. Like I said in my previous post, You have this
Code:
struct gamesStruct
{
diceClass dice(); //create instance of dice class
};
declared twice. Once in the games.h file and once in the game.cpp file. And you don't need ( ) after you declare a diceClass variable inside a stuct or a class.
I cant compile the code because of missing header files but I do know you don't need all those #ifndef #define statements in your .cpp file. It only has to be done in the header files like so:
Code:
/******** dice.h header file **********/
#ifndef _DICE_H
#define _DICE_H
// header file stuff here
#endif
then just #include header normally in your dice.cpp file. or whereever you need to include that header file. You also don't have to check the standard heads the way you are either. They are already defined the first time your program hits the first #include <iostream> it finds. I know you know this from your comments in your code but I am just letting you that you don't have to do this:
Code:
#ifndef _iostream_h
#define _iostream_h
#include <iostream>
#endif
that is all I found just having a quick look. Like I said, I couldn't get it to compile because of the missing header file so I can't really help you any more than what I have.
-
>> I do know you don't need all those #ifndef #define statements in your .cpp file. It only has to be done in the header files like so... then just #include header normally in your dice.cpp file.
I know, but I just kinda like this way better... I don't remember why I started putting all those unnecessary checks in, but it just kinda became habit for me... idk...
also, there is no longer any games.cpp file.. it's only games.h, which is #included in main.cpp... and only contains this:
Code:
#include "dice.h"
struct gamesStruct
{
diceClass dice(); //create instance of dice class
};
-
Code:
#include "dice.h"
struct gamesStruct
{
diceClass dice; //declare instance of dice class
};
The gameStruct constructor will create the instance of diceClass, you just need to declare it.
-
-
yea you did declare it in main.cpp but this is what your games.h file contains(well the one I downloaded anyways):
Code:
struct gamesStruct
{
diceClass dice() ; //create instance of dice class
};
notice the red part.
I am not sure if you are seeing what is wrong with this stuct. It should look like this:
Code:
struct gamesStruct
{
diceClass dice; //create instance of dice class
};
Notice the lack of red.
maybe you just missed it and didnt notice that you have added parentheses after you delcare dice in your gamesStruct
-
oic... thanks alot... I completely missed that in an earlier post... it was just the way that Zach L. worded it that threw me off...
-