Thread: Cant solve this error... please help...

  1. #1
    ------------
    Join Date
    Jun 2005
    Posts
    79

    Cant solve this error... please help...

    Ok, i switched from coding a MUD to a regular text based game... I decided to use a structure to hold the player information, and anything that you can fight (trying to keep organised, and easier to change) but i keep getting this error:

    [montez@localhost codes]$ c++ game.cpp -o game -O2 -Wall
    game.cpp: In function `void name()':
    game.cpp:37: error: request for member `player' in `name', which is of non-class type `void ()()'

    when i try to compile it... this is my souce so far:

    Code:
    #include <iostream>
    
    using namespace std;
    
    struct stats { //declairs a structure that holds the player information
            string name; //players name
            string weapon; //players weapon
            int life; //players life
    };
    
    void intro();
    void name();
    
    void intro() {
            cout<<"--------------------------------------------- "<<endl;
            cout<<"----                                     ---- "<<endl;
            cout<<"----                                     ---- "<<endl;
            cout<<"----          -My    Game-                ---- "<<endl;
            cout<<"----                                     ---- "<<endl;
            cout<<"----                                     ---- "<<endl;
            cout<<"----          -By: Goosie-               ---- "<<endl;
            cout<<"----                                     ---- "<<endl;
            cout<<"----                                     ---- "<<endl;
            cout<<"--------------------------------------------- "<<endl;
            name();
    }
    
    int main() {
            stats player;
            player.life = 100;
    
            intro();
    }
    
    void name() {
            cout<<"Enter your name: ";
            cin>>name.player;
    }
    any help would be appreciated
    Last edited by Goosie; 07-08-2005 at 04:21 PM. Reason: removing a ton of extra spaces...
    CAUTION: Newbie at programming!
    -------------------------------------------------
    Thanks everyone who helped me with all the questions, and utter lost-ness i happen to have... it is VERY VERY much appreciated

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    It should be player.name, not name.player and you have to pass the player variable to the function.

  3. #3
    ------------
    Join Date
    Jun 2005
    Posts
    79
    how do i pass the player variable to the function? sorry for sounding dumb, im new to coding... could i make a pointer to "stats" called S and make it:
    Code:
    void name(&S)
    <code>
    ?
    CAUTION: Newbie at programming!
    -------------------------------------------------
    Thanks everyone who helped me with all the questions, and utter lost-ness i happen to have... it is VERY VERY much appreciated

  4. #4
    ------------
    Join Date
    Jun 2005
    Posts
    79
    hehe nevermind... i wasnt thinking, sorry... just add stats player; to the void and it works... thanks for the help
    CAUTION: Newbie at programming!
    -------------------------------------------------
    Thanks everyone who helped me with all the questions, and utter lost-ness i happen to have... it is VERY VERY much appreciated

  5. #5
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    No you were right to pass the reference of the struct.

    Because youre creating an object of the struct, not sharing the same one, the level and other variables in that struct wont be attached to the appropriate name.

    struct #1:
    level = 5,
    name = *blank*;
    life = 100;

    struct #2:
    level = *blank*
    name = Goosie;
    life = *blank*;
    You could return a string this way,

    Code:
    void intro();
    string name();
    
    void intro() {
            cout<<"--------------------------------------------- "<<endl;
            cout<<"----                                     ---- "<<endl;
            cout<<"----                                     ---- "<<endl;
            cout<<"----          -My    Game-                ---- "<<endl;
            cout<<"----                                     ---- "<<endl;
            cout<<"----                                     ---- "<<endl;
            cout<<"----          -By: Goosie-               ---- "<<endl;
            cout<<"----                                     ---- "<<endl;
            cout<<"----                                     ---- "<<endl;
            cout<<"--------------------------------------------- "<<endl;
    }
    
    int main() {
            stats player;
            player.life = 100;
            player.name = name();
            intro();
    }
    
    string name() {
            string argPlayer;
    
            cout<<"Enter your name: ";
            cin>>argPlayer;
    
            return argPlayer;
    }
    or pass the reference to the right struct this way (the way I would do it):

    Code:
    void intro();
    void name(stats*);
    
    void intro() {
            cout<<"--------------------------------------------- "<<endl;
            cout<<"----                                     ---- "<<endl;
            cout<<"----                                     ---- "<<endl;
            cout<<"----          -My    Game-               ---- "<<endl;
            cout<<"----                                     ---- "<<endl;
            cout<<"----                                     ---- "<<endl;
            cout<<"----          -By: Goosie-               ---- "<<endl;
            cout<<"----                                     ---- "<<endl;
            cout<<"----                                     ---- "<<endl;
            cout<<"--------------------------------------------- "<<endl;
    }
    
    int main() {
            stats player;
            player.life = 100;
    
            intro();
            name(&player);
    }
    
    void name(stats* copyStats) {
            cout<<"Enter your name: ";
            cin>>copyStats->name;
    }
    So by passing the memory location you're saving yourself space/process speed from not having to create another object of stats, and you're also giving the right name to the right struct.

    BTW I seperated Intro() and Name() in that, you could pass the reference to Intro() if you wanted.. I just thought I made more sence to have them seperate.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  6. #6
    ------------
    Join Date
    Jun 2005
    Posts
    79
    Its only single player, so i did want it all to be in the same structure. Like i want to define the default stuff in the int main() (money, weapon, ect...) and be able to change it through diffrent functions in the game... would a pointer do that better?
    CAUTION: Newbie at programming!
    -------------------------------------------------
    Thanks everyone who helped me with all the questions, and utter lost-ness i happen to have... it is VERY VERY much appreciated

  7. #7
    *this
    Join Date
    Mar 2005
    Posts
    498
    Code:
    int main() {
            stats player;
            player.life = 100;
    
            intro();
            name(player);
    }
    
    void name(stats &copyStats) {
            cout<<"Enter your name: ";
            cin>>copyStats.name;
    }

  8. #8
    *this
    Join Date
    Mar 2005
    Posts
    498
    Just pass the reference to name like the code I changed above and it will do what you want without pointers.

  9. #9
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by JoshR
    Just pass the reference to name like the code I changed above and it will do what you want without pointers.
    You're passing the reference either way, its just how you recieve it, with a pointer or a reference.. it does not matter.

    Yes Goosie, using either my way or JoshR's modified way will allow you to keep the same stats for your 1 struct by using references OR pointers or visa versa.


    PS. Why recently do you have a quote in 1 post and the text to reply in the next post.. whats up with that?
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  10. #10
    ------------
    Join Date
    Jun 2005
    Posts
    79
    Joshr, thanks for telling me that, im trying to make it work throughout my program (added a LOT more...). Seems like it'll save me a lot of time...
    CAUTION: Newbie at programming!
    -------------------------------------------------
    Thanks everyone who helped me with all the questions, and utter lost-ness i happen to have... it is VERY VERY much appreciated

  11. #11
    ------------
    Join Date
    Jun 2005
    Posts
    79
    im getting this now when i try to put it throughout the code...
    Code:
    [montez@localhost codes]$ c++ game.cpp -o game -O2 -Wall
    game.cpp: In function `void intro(stats&)':
    game.cpp:29: error: syntax error before `&' token
    game.cpp: In function `int main(stats&)':
    game.cpp:38: error: syntax error before `&' token
    game.cpp: In function `void name(stats&)':
    game.cpp:45: error: syntax error before `&' token
    game.cpp: In function `void station(stats&)':
    game.cpp:62: error: syntax error before `&' token
    game.cpp:66: error: syntax error before `&' token
    and my code is:
    Code:
    #include <iostream>
    
    using namespace std;
    
    struct stats { //declairs a structure that holds the player information
    	string name; //players name
    	string weapon; //players weapon
    	int life; //players life
    	int debt; //players debt to loanshark
    };
    
    void intro(stats &S);
    void name(stats &S);
    void station(stats &S);
    void atlanta(stats &S);
    void loanshark(stats &S);
    
    void intro(stats &S) {
    	cout<<"--------------------------------------------- "<<endl;
    	cout<<"----                                     ---- "<<endl;
    	cout<<"----                                     ---- "<<endl;
    	cout<<"----          -Drug Wars-                ---- "<<endl;
    	cout<<"----                                     ---- "<<endl;
    	cout<<"----                                     ---- "<<endl;
    	cout<<"----          -By: Goosie-               ---- "<<endl;
    	cout<<"----                                     ---- "<<endl;
    	cout<<"----                                     ---- "<<endl;
    	cout<<"--------------------------------------------- "<<endl;
    	name(stats &S);
    	
    }
    
    int main(stats &S) {
    	stats player;
    	S.life = 100;
    	S.debt = 500;
    
    	intro(stats &S);
    }
    
    void name(stats &S) {
    
    	cout<<"Enter your name: ";
    	cin>>S.name;
    	station(stats &S);
    }
    
    void station(stats &S) {
    
    	int destination;
    	
    	cout<<"Train Station... "<<endl;
    	cout<<"Pick a destination: "<<endl;
    	cout<<"--------------------------- "<<endl;
    	cout<<"1. Loan Shark "<<endl;
    	cout<<"2. Atlanta "<<endl;
    	cout<<"Where do you want to go? ";
    	cin>>destination;
    	switch ( destination ) {
    	case 1:
    		cout<<"You catch the Greyhound headed towards your loan shark. "<<endl<<endl;
    		loanshark(stats &S);
    		break;
    	case 2:
    		cout<<"You catch the MARTA headed towards Atlanta. "<<endl<<endl;
    		atlanta(stats &S);
    		break;
    		
    	default:
    		cout<<"Invalid destination... ";
    		break;
    	}
    	cin.get();
    }
    
    void atlanta(stats &S) {
    	
    	cout<<"Atlanta is incomplete right now, sorry "<<endl;
    }
    
    void loanshark(stats &S) {
    
    	cout<<"You are at the loan sharks office, and "<<endl;
    	cout<<"currently owe him $"<<S.debt<<". "<<endl;
    	cout<<"How much do you want to repay the loan "<<endl;
    	cout<<"shark now?"<<endl;
    }
    i decided to turn it into a drugwars clone... ive always loved that game
    CAUTION: Newbie at programming!
    -------------------------------------------------
    Thanks everyone who helped me with all the questions, and utter lost-ness i happen to have... it is VERY VERY much appreciated

  12. #12
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    You've got a problem because you're missing a few concepts..

    No idea why you're saying int main should take the arguement 'stats &S', not only is S not define there, but int main will not allow that. Then when youre passing S, you dont have to define it as 'stats' again. Look over your code, it should be obvious.

    Look over these changes: you pass 'S', not 'stats &S'. You are passing the memory location of 'S', not making a whole new 'stats' (stats &S) object thats only a pointer and passing that for some odd reason.

    Code:
    #include <iostream>
    
    using namespace std;
    
    struct stats { //declairs a structure that holds the player information
    	string name; //players name
    	string weapon; //players weapon
    	int life; //players life
    	int debt; //players debt to loanshark
    };
    
    void intro(stats &S);
    void name(stats &S);
    void station(stats &S);
    void atlanta(stats &S);
    void loanshark(stats &S);
    
    void intro(stats &S) {
    	cout<<"--------------------------------------------- "<<endl;
    	cout<<"----                                     ---- "<<endl;
    	cout<<"----                                     ---- "<<endl;
    	cout<<"----          -Drug Wars-                ---- "<<endl;
    	cout<<"----                                     ---- "<<endl;
    	cout<<"----                                     ---- "<<endl;
    	cout<<"----          -By: Goosie-               ---- "<<endl;
    	cout<<"----                                     ---- "<<endl;
    	cout<<"----                                     ---- "<<endl;
    	cout<<"--------------------------------------------- "<<endl;
    	name(S);
    	
    }
    
    int main() {
    	stats player;
    	player.life = 100;
    	player.debt = 500;
    
    	intro(player);
    }
    
    void name(stats &S) {
    
    	cout<<"Enter your name: ";
    	cin>>S.name;
    	station(S);
    }
    
    void station(stats &S) {
    
    	int destination;
    	
    	cout<<"Train Station... "<<endl;
    	cout<<"Pick a destination: "<<endl;
    	cout<<"--------------------------- "<<endl;
    	cout<<"1. Loan Shark "<<endl;
    	cout<<"2. Atlanta "<<endl;
    	cout<<"Where do you want to go? ";
    	cin>>destination;
    	switch ( destination ) {
    	case 1:
    		cout<<"You catch the Greyhound headed towards your loan shark. "<<endl<<endl;
    		loanshark(S);
    		break;
    	case 2:
    		cout<<"You catch the MARTA headed towards Atlanta. "<<endl<<endl;
    		atlanta(S);
    		break;
    		
    	default:
    		cout<<"Invalid destination... ";
    		break;
    	}
    	cin.get();
    }
    
    void atlanta(stats &S) {
    	
    	cout<<"Atlanta is incomplete right now, sorry "<<endl;
    }
    
    void loanshark(stats &S) {
    
    	cout<<"You are at the loan sharks office, and "<<endl;
    	cout<<"currently owe him $"<<S.debt<<". "<<endl;
    	cout<<"How much do you want to repay the loan "<<endl;
    	cout<<"shark now?"<<endl;
    }
    Also note you're passing the object 'player' around (from int main).
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  13. #13
    ------------
    Join Date
    Jun 2005
    Posts
    79
    ooohhh... that makes sense now... thanks for the help so i only need to put a 'Stats &S' in functions that use it? for some reason i was thinking they all needed it oops...
    CAUTION: Newbie at programming!
    -------------------------------------------------
    Thanks everyone who helped me with all the questions, and utter lost-ness i happen to have... it is VERY VERY much appreciated

  14. #14
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by Goosie
    ooohhh... that makes sense now... thanks for the help so i only need to put a 'Stats &S' in functions that use it? for some reason i was thinking they all needed it oops...
    In functions that you PASS IT TO, not any function that you use it in. This is from the basics of functions, its no different when you are passing around a struct.

    Code:
    void aFunction(int x) { /*this function takes the parameter of 1 int, therefor 
    when using the function you must give it the value of an int */
    
    //do something with x
    
    }
    
    int main() 
    {
      anInt = 10;
    
      aFunction(anInt);
    
    }
    If you wanted to pass the MEMORY ADDRESS, you change the parameter from int x, to int& x, to tell it to take the memory address instead of the value, which means it will update anInt from main() too, because its the same memory address.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  15. #15
    *this
    Join Date
    Mar 2005
    Posts
    498
    Quote Originally Posted by Dae
    You're passing the reference either way, its just how you recieve it, with a pointer or a reference.. it does not matter.

    Yes Goosie, using either my way or JoshR's modified way will allow you to keep the same stats for your 1 struct by using references OR pointers or visa versa.


    PS. Why recently do you have a quote in 1 post and the text to reply in the next post.. whats up with that?
    That is just a sign of my lazy posting. lol.

    Ya I know what your saying about pointers it works either way, but sometimes to beginers its easier if you dont have to call a function using a reference you know? Its just more stuff to think about.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 04-25-2005, 11:59 AM
  2. help solve linking problem, thanks
    By Raison in forum Windows Programming
    Replies: 8
    Last Post: 05-29-2004, 11:14 AM
  3. ^^ help me solve the error
    By skwei81 in forum C++ Programming
    Replies: 2
    Last Post: 04-03-2003, 09:04 AM
  4. Help to solve this problem
    By Romashka in forum C++ Programming
    Replies: 3
    Last Post: 04-16-2002, 09:32 AM