Thread: object members

  1. #1
    Registered User blight2c's Avatar
    Join Date
    Mar 2002
    Posts
    266

    object members

    hey all,

    i'd like to use objects of one class in other classes but i am still having trouble. this is what i have so far:
    Code:
    //-------------------------------------------------lineup.h--------------------
    
    
    #ifndef lineup_h
    #define lineup_h
    
    #include "player.h"//Player is the other class 
    
    
    
    class Lineup
    {
    public:
    
    Lineup();	
    void mf_Lineup_loadArray(Player *p_h1, Player *p_h2);
    												
    private:
    
    Player home_lineup[2];//this is an array of pointers
    		//to objects of class Player
    
    
    };
    
    #endif
    
    //-------------------------------------lineup.cpp--------------------------------
    //Lineup.cpp
    
    #include "lineup.h"
    #include "player.h"
    
    class Player;
    
    Lineup::Lineup() //what's wrong with my constructor?
    {}
    
    void Lineup::mf_Lineup_loadArray(Player *p_h1, Player *p_h2)//this comes from main
    {
    	 home_lineup[0]=&p_h1;
    	 home_lineup[1]=&p_h2;
    }
    i get this error:
    C:\mprog\sandlot\Lineup.cpp(9) : error C2512: 'Player' : no appropriate default constructor available
    C:\mprog\sandlot\Lineup.cpp(13) : error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'class Player ** ' (or there is no acceptable conversion)
    C:\mprog\sandlot\Lineup.cpp(14) : error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'class Player ** ' (or there is no acceptable conversion)


    thanks in advance

  2. #2
    Registered User
    Join Date
    May 2002
    Posts
    317
    Ok well you can either use the friend keyword (which would work for how you set up your code):

    Class Lineup
    {

    friend class Player;
    // You're code here
    };

    or you can make one derive from the other, which I don't think you want to do.

    Then just remove that class Player statement from the cpp file which is I believe what is giving you the errors.
    Last edited by Traveller; 05-06-2002 at 06:00 AM.

  3. #3
    Registered User blight2c's Avatar
    Join Date
    Mar 2002
    Posts
    266
    thanks for helping

    i tried as you suggested but I'm getting this error instead:

    main.obj : error LNK2001: unresolved external symbol "public: __thiscall Lineup::Lineup(void)" (??0Lineup@@QAE@XZ)
    Debug/sandlot.exe : fatal error LNK1120: 1 unresolved externals


    here's my function call, am i doing it right:
    Code:
    Lineup l;
    l.mf_Lineup_loadArray(h1, h2);//Player h1 and h2
    here's the classes:
    Code:
    //-----------------------------------lineup.h---------------------------------
    
    
    #ifndef lineup_h
    
    #define lineup_h
    
    #include "player.h"//Player is the other class 
    
    class Lineup
    {
    
    	
    friend class Lineup;
    	
    public:
    
    Lineup();	
    void mf_Lineup_loadArray(Player h1, Player h2);
    														
    private:
    
    Player home_lineup[2];//this is an array of pointers
    		//to objects of class Player
    
    
    };
    
    #endif
    
    //----------------------------------------Lineup.cpp---------------------------
    
    #include "lineup.h"
    #include "player.h"
    #include <iostream.h>
    
    
    
    
    
    void Lineup::mf_Lineup_loadArray(Player h1, Player h2)//this comes from main
    {
    	 home_lineup[0]=h1;
    	 home_lineup[1]=h2;
    	 cout<<"gotit";
    }

  4. #4
    Registered User
    Join Date
    May 2002
    Posts
    317
    What I see is this:
    Code:
    class Lineup
    {
          friend class Lineup; 
          //ect.
    };
    I believe this should be : friend class Player

  5. #5
    Registered User blight2c's Avatar
    Join Date
    Mar 2002
    Posts
    266
    ya, i'm sorry--typo. same problem though. here's the revised code:

    Code:
    //lineup.h
    
    
    #ifndef lineup_h
    #define lineup_h
    
    #include "player.h"//Player is the other class 
    
    
    
    class Lineup
    {
    
    	
    friend class Player;
    	
    public:
    
    	Lineup();	
    	void mf_Lineup_loadArray(Player h1, Player h2);
    														
    private:
    
    	Player home_lineup[2];//this is an array of pointers
    			//to objects of class Player
    
    
    };
    
    #endif
    am i making the function call right? christ, i just can't figure out what i'm doing wrong

  6. #6
    Registered User
    Join Date
    May 2002
    Posts
    317
    Alright I believe you are declaring you're array wrong:

    Player home_lineup[2];

    should be: Player *home_lineup[2];

    And then try this:

    void mf_Lineup_loadArray(Player &h1, Player &h2);

    And lastly:
    Code:
    void Lineup::mf_Lineup_loadArray(Player &h1, Player &h2)//this comes from main
    {
    	 home_lineup[0]=&h1;
    	 home_lineup[1]=&h2;
    	 cout<<"gotit";
    }

  7. #7
    Registered User blight2c's Avatar
    Join Date
    Mar 2002
    Posts
    266
    i pluged in variations of * and & (i even tried passing the entire object). but still the same error:

    main.obj : error LNK2001: unresolved external symbol "public: __thiscall Lineup::Lineup(void)" (??0Lineup@@QAE@XZ)
    Debug/sandlot.exe : fatal error LNK1120: 1 unresolved externals



    thanks a lot for your help and i'm sorry to take up your time. if you still have any patience left here's all my code.

  8. #8
    Registered User
    Join Date
    May 2002
    Posts
    317
    Ok, in you main.cpp file where you define your objects:
    Code:
    Player h1(12,3,3,0,0,4,2,3,"Alby Abe", 1), *p_h1=&h1;
    Try doing this instead:
    Code:
    	
           Player h1(12,3,3,0,0,4,2,3,"Alby Abe", 1); //define object
           Player *p_h1; //create pointer of Player type
           p_h1 = &h1; //assign address of h1 to p_h1

  9. #9
    Registered User blight2c's Avatar
    Join Date
    Mar 2002
    Posts
    266
    same error:
    main.obj : error LNK2001: unresolved external symbol "public: __thiscall Lineup::Lineup(void)" (??0Lineup@@QAE@XZ)
    this is what i put in:
    Code:
    Player h1(12,3,3,0,0,4,2,3,"Alby Abe", 1); 
    Player *p_h1;
    p_h1=&h1;
    Player h2(14,1,1,1,1,1,1,1,"Boss",2);
    Player *p_h2;
    p_h2=&h2;
    as far as i can tell, it should be the same difference. am i trying to do some weird here? what are my other options? i was able to compile everything that i wanted in procedural style code, but i wanted to learn something new and make my code look nice like everyone else's. ignoring the symantics, what design flaws are there? thanks again.

  10. #10
    Registered User
    Join Date
    May 2002
    Posts
    317
    Well, I think we were looking at th wrong thing. The error calls for unresolved external symbol ...Lineup::Lineup() so you gave a prototype for a constructor but never defined it:
    Code:
    class Lineup
    {
    
    friend class Player;
    	
    public:
    
    	Lineup();	<- write here
    	void mf_Lineup_loadArray(Player &h1, Player &h2);
    private:
    
    	Player *home_lineup[2];//this is an array of pointers
    						//to objects of class Player
    
    };
    So in Lineup.cpp add:
    Code:
    Lineup::Lineup()
    {};

  11. #11
    Registered User blight2c's Avatar
    Join Date
    Mar 2002
    Posts
    266
    : plants a big wet kiss on traveller's cheek:

    gfe2e

    would you believe i messed around with that last night but not in conjunction the freind declaration. thanks a lot you made my week.

    but . . . why is lineup.cpp not printing the cout?

  12. #12
    Registered User blight2c's Avatar
    Join Date
    Mar 2002
    Posts
    266
    this is funny. it won't print if i compile this:
    Code:
    void Lineup::mf_Lineup_loadArray(Player *p_h1, Player *p_h2)//this comes from main
    {
    	 home_lineup[0]=p_h1;
    	 home_lineup[1]=p_h2;
    	 cout<<"testing";
    }
    but it will if i do something like this:
    Code:
    void Lineup::mf_Lineup_loadArray(Player *p_h1, Player *p_h2)//this comes from main
    {
    	 home_lineup[0]=p_h1;
    	 home_lineup[1]=p_h2;
    	 cout<<"testing";
    	 int b;
    	 cin>>b;
    }

  13. #13
    Registered User
    Join Date
    May 2002
    Posts
    317
    I think its because you're clearing the screen right after you call the function( well after a 3 second wait anyway). That's why if you put a cin statement, you have time to see the message.

  14. #14
    Registered User blight2c's Avatar
    Join Date
    Mar 2002
    Posts
    266
    that's what i thought, so i deleted the clrscr()'s and still nothing. even when i scroll up through the console window to the beginning i still don't see it. but i do see the cursor blinky during the f_wait(). i'm stumped again.

  15. #15
    Registered User blight2c's Avatar
    Join Date
    Mar 2002
    Posts
    266
    wait, problem solved. i must of missed a clrscr(). . . .

    gosh, what'll we talk about now if i don't come up with another idiotic question for you to help me with

    thanks a lot for your help, i won't be forgetting it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Telling a shared_ptr not to delete object?
    By TriKri in forum C++ Programming
    Replies: 5
    Last Post: 08-16-2008, 04:26 AM
  2. Replies: 60
    Last Post: 12-20-2005, 11:36 PM
  3. Another Dynamic Memory Question
    By SirCrono6 in forum C++ Programming
    Replies: 6
    Last Post: 03-02-2005, 12:10 PM
  4. Creating object of type HWND from a dll
    By Unregistered in forum Windows Programming
    Replies: 2
    Last Post: 03-13-2002, 12:40 AM