Having two classes reference each other.

This is a discussion on Having two classes reference each other. within the C++ Programming forums, part of the General Programming Boards category; Hello! I'm new to the forums and C++ as well. I've been following through a few books but I haven't ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Location
    IL
    Posts
    23

    Thumbs down Having two classes reference each other.

    Hello! I'm new to the forums and C++ as well. I've been following through a few books but I haven't found how to do this correctly yet.

    I'm programming a console application using Visual C++.

    What I am aiming to do is to allow one class to reference another. This is for a game that me and couple of friends are programming.

    I found a thread similar to this before I joined. However following through it I haven't produced any reliable results. So, here is my code, split up into 5 files.

    The code compiles fine. However, in the linking stage, I recieve several errors:

    Code:
    Linking...
    a.obj : error LNK2005: "public: __thiscall A::A(void)" (??0A@@QAE@XZ) already defined in cppsample.obj
    a.obj : error LNK2005: "public: __thiscall A::~A(void)" (??1A@@QAE@XZ) already defined in cppsample.obj
    a.obj : error LNK2005: "public: int __thiscall A::getBV(void)" (?getBV@A@@QAEHXZ) already defined in cppsample.obj
    b.obj : error LNK2005: "public: __thiscall B::B(void)" (??0B@@QAE@XZ) already defined in cppsample.obj
    b.obj : error LNK2005: "public: __thiscall B::~B(void)" (??1B@@QAE@XZ) already defined in cppsample.obj
    b.obj : error LNK2005: "public: int __thiscall B::getAV(void)" (?getAV@B@@QAEHXZ) already defined in cppsample.obj
    Release/cppsample.exe : fatal error LNK1169: one or more multiply defined symbols found
    cppsample.cpp
    Code:
    //This is the main file.
    //Includes (Unused)
    /*
    These include files are not included but may be included later.
    #include <limits>				//Limits
    #include <fstream>				//File stream for dealing with files.
    */
    
    //Includes (Standard)
    #include <cstdio>				//C Standard Input/Output
    #include <cstdlib>				//C Standard Library
    #include <iostream>				//Input/Output Stream
    #include <string>				//C++ Strings
    using namespace std;			//Use Standard Namespace
    
    //Includes (custom)
    #include "a.cpp"
    #include "b.cpp"
    
    //Defines
    #define CURRENT_EXPERIMENT "This program will try to get two classes to reference each other."
    
    //Type Definitions
    typedef short unsigned int sui;	//Shorthand form of numbers from 0 to 65,535.
    typedef short int si;			//Shorthand form of numbers from -32,768 to 32,767.
    
    int main(int iNumberOfArgs, char* psArgs[])
    {
    	cout << CURRENT_EXPERIMENT << "\n";
    	system("PAUSE");
    	A hi;
    	cout << hi.iOfA << "\n";
    	system("PAUSE");
    	return 0;
    }
    a.cpp
    Code:
    #include "a.h"
    #include "b.h"
    A::A()
    {
    	iOfA=1;
    	pToB=0;
    }
    A::~A()
    {
    }
    int A::getBV()
    {
    	return pToB->iOfB;
    }
    b.cpp
    Code:
    #include "a.h"
    #include "b.h"
    
    B::B()
    {
    	iOfB=2;
    	pToA=0;
    }
    B::~B()
    {
    }
    int B::getAV()
    {
    	return pToA->iOfA;
    }
    a.h
    Code:
    #ifndef A_ADDED
    #define A_ADDED
    #ifndef B_ADDED
    class B;
    #endif
    class A
    {
    public:
    	A();
    	~A();
    	int getBV();
    	int iOfA;
    private:
    	B* pToB;
    };
    #endif
    b.h
    Code:
    #ifndef B_ADDED
    #define B_ADDED
    #ifndef A_ADDED
    class A;
    #endif
    class B
    {
    public:
    	B();
    	~B();
    	int getAV();
    	int iOfB;
    private:
    	A* pToA;
    };
    #endif

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    wow... you need to not use templates... or at least not that one... here's what you can cut out of your main program:
    Code:
    //This is the main file.
    
    //Includes (Standard)
    //here you included c headers for I/O, which aren't needed if you're using c++
    #include <iostream>				//Input/Output Stream
    #include <string>				//C++ Strings
    using namespace std;			//Use Standard Namespace
    
    //Includes (custom)
    #include "a.cpp"
    #include "b.cpp"
    
    //no need to make new int size definitions... IMO, short int is a whole lot
    //clearer to most people than si or sui for short unsigned int...
    
    int main()
    {
    	cout << CURRENT_EXPERIMENT << "\n";
    	cin.get();	//pause removed - creates possible security holes
    	A hi;
    	cout << hi.iOfA << "\n";
    	cin.get();	//see above comment
    	return 0;
    }
    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

  3. #3
    Registered User
    Join Date
    Mar 2006
    Location
    IL
    Posts
    23
    Thank you for the tips, I've adapted the main .cpp for that, but there are still linker errors.

    If anyone else can help me solve this I'd be grateful.
    Last edited by theJ89; 03-25-2006 at 10:28 PM.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    >> #include "a.cpp"

    You don't include cpp files (unless you are doing template programming, which you aren't). That's your problem. You should be including the header files (a.h and b.h).

    >> #ifndef B_ADDED

    That is not necessary in your a.h file, and the same is true for the #ifndef A_ADDED in your b.h file. Just use one include guard specific to that particular file.

  5. #5
    Registered User
    Join Date
    Mar 2006
    Location
    IL
    Posts
    23
    Thank you, I have solved my problem now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. A general quaetion on portability
    By kris.c in forum C Programming
    Replies: 7
    Last Post: 07-20-2006, 03:48 AM
  4. problem with the library
    By kris.c in forum C Programming
    Replies: 21
    Last Post: 07-10-2006, 08:29 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21