Thread: Creating class - linker problems

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    24

    Creating class - linker problems

    Should this work?

    Code:
    /* .h */
    
    class cPlayer	 		//declaration
    {
    	public:
    		float x, y, z;
    
    		cPlayer(); 	//constructor
    
    };
    
    cPlayer::cPlayer(); 		//construct
    
    void do_something(cPlayer);
    
    /* .cpp */
    
    cPlayer player; 		//instance
    
    player.x=10;			//assignment
    
    main()
    {
    	do_something(player)	//pass
    }
    
    /* other.cpp - includes same .h */
    
    
    void do_something(cPlayer &player)
    {
    	player->y=20;		//use by reference
    
    }
    Last edited by alanb; 09-03-2009 at 07:26 AM. Reason: Change call to pass instance

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    The linker can't find the constructor because you've only declared, but not defined, it.

    Also, main returns an int.

    EDIT:

    One more thing. Your declaration for do_something doesn't match it's definition, either.
    Last edited by Sebastiani; 09-03-2009 at 07:31 AM.

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    24
    Quote Originally Posted by Sebastiani View Post
    The linker can't find the constructor because you've only declared, but not defined, it.
    I tried duplicating the declaration just after it and removed the constructor and gave the floats a value. The compiler says - 'cPlayer' : 'class' type redefinition.
    Also, main returns an int.
    I was paraphrasing, sorry.
    One more thing. Your declaration for do_something doesn't match it's definition, either.
    Should the declaration indicate a pointer to cPlayer rather than to a type cPlayer?

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> I tried duplicating the declaration just after it and removed the constructor and gave the floats a value. The compiler says - 'cPlayer' : 'class' type redefinition.

    You don't need to duplicate or remove anything - simply define a body for the constructor *somewhere*.

    >> Should the declaration indicate a pointer to cPlayer rather than to a type cPlayer?

    The declaration indicates that the parameter is to be a temporary (eg: copy of the value passed in), whereas the definition specifies a reference. You need to pick one and stick with it (probably the latter, since you seem to need to make changes to the object).

  5. #5
    Registered User
    Join Date
    Aug 2009
    Posts
    24
    Quote Originally Posted by Sebastiani View Post
    The declaration indicates that the parameter is to be a temporary (eg: copy of the value passed in), whereas the definition specifies a reference. You need to pick one and stick with it (probably the latter, since you seem to need to make changes to the object).
    void do_something(cPlayer &player); //declaration

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    That's it.

    I'm assuming you fixed the undefined reference to the constructor, as well, then?

  7. #7
    Registered User
    Join Date
    Aug 2009
    Posts
    24
    I began by removing it until I could compile cleanly, then I took the assignments collecting in the main .cpp and put them back into a constructor and all seems well.

  8. #8
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    /* .cpp */
    
    cPlayer player; 		//instance
    
    player.x=10;			//assignment
    
    main()
    {
    	do_something(player)	//pass
    }
    Does that assignment work for you? It shouldn't... or is this more "paraphrasing"?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  9. #9
    Registered User
    Join Date
    Aug 2009
    Posts
    24
    No, I wasn't, and I've moved that line (and others like it) to a constructor within the declaration, and I removed the instance names in the process. I can now access the members in that way from within the functions that the class is passed to by reference.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Problems with my custom string class
    By cunnus88 in forum C++ Programming
    Replies: 15
    Last Post: 11-15-2005, 08:21 PM
  3. Replies: 3
    Last Post: 10-31-2005, 12:05 PM
  4. base class pointer problems
    By ... in forum C++ Programming
    Replies: 3
    Last Post: 11-16-2003, 11:27 PM
  5. Creating a string class
    By incognito in forum C++ Programming
    Replies: 2
    Last Post: 01-19-2002, 05:40 PM