Thread: a class in a struct?

  1. #1
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787

    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...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    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?
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  3. #3
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    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...
    Last edited by major_small; 09-06-2003 at 09:23 PM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  4. #4
    Registered User Dohojar's Avatar
    Join Date
    Feb 2002
    Posts
    115
    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.
    Dohojar Moajbuj
    Time is the greatest teacher, too bad it kills all its students

  5. #5
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    okay, it's quite a bit of code that doesn't have anythign to do with this, but here it is:

    main.cpp
    Last edited by major_small; 09-07-2003 at 07:37 PM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  6. #6
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    games.h
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  7. #7
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    dice.h
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  8. #8
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    dice.cpp
    Last edited by major_small; 09-07-2003 at 07:39 PM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  9. #9
    Registered User Dohojar's Avatar
    Join Date
    Feb 2002
    Posts
    115
    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.
    Dohojar Moajbuj
    Time is the greatest teacher, too bad it kills all its students

  10. #10
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    >> 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
    };
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  11. #11
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    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.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  12. #12
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    I did, in main.cpp...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  13. #13
    Registered User Dohojar's Avatar
    Join Date
    Feb 2002
    Posts
    115
    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
    Dohojar Moajbuj
    Time is the greatest teacher, too bad it kills all its students

  14. #14
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    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...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  15. #15
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Sorry 'bout that.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  2. const elements of struct / class not accessible?
    By talz13 in forum C# Programming
    Replies: 2
    Last Post: 03-24-2006, 05:05 PM
  3. Function validation.
    By Fhl in forum C Programming
    Replies: 10
    Last Post: 02-22-2006, 08:18 AM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM