| | |
How to cross reference objects?
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Nov 2007
Posts: 4
Reputation:
Solved Threads: 0
Hello All,
I know that there is a similar thread about my problem, but I can't find it anymore.
Well basically I want 2 objects to know from each other. I know that in C++ I have to
use pointers to do that. I wrote some sample code to show you the problem:
======================================
The error I get is:
**** Build of configuration Linux GCC for project TestCrossPt ****
make all
g++ -c A.cpp
B.hpp:13: error: ISO C++ forbids declaration of ‘A’ with no type
B.hpp:13: error: expected ‘;’ before ‘*’ token
make: *** [A.o] Error 1
======================================
And this is the makefile I use:
all: test
test: A.o B.o TestCrossPt.o
g++ A.o B.o TestCrossPt.o -o test.exe -lpthread
A.o: A.cpp
g++ -c A.cpp
B.o: B.hpp
g++ -c B.cpp
TestCrossPt.o: TestCrossPt.cpp
g++ -c TestCrossPt.cpp
==========================================
Well, I am pretty new to C++ so please use easy language
many thanks
Rick
I know that there is a similar thread about my problem, but I can't find it anymore.
Well basically I want 2 objects to know from each other. I know that in C++ I have to
use pointers to do that. I wrote some sample code to show you the problem:
c++ Syntax (Toggle Plain Text)
#ifndef A_HPP_ #define A_HPP_ #include "B.hpp" class A { public: //B* bpt; A(); virtual ~A(); private: B* bpt; }; #endif /*A_HPP_*/ ========================================= #include "A.hpp" #include "B.hpp" A::A() { } A::~A() { } ======================================== #ifndef B_HPP_ #define B_HPP_ #include "A.hpp" class B { public: //A* apt; B(); virtual ~B(); private: A* apt; }; #endif /*B_HPP_*/ ======================================= #include "B.hpp" #include "A.hpp" B::B() { } B::~B() { }
The error I get is:
**** Build of configuration Linux GCC for project TestCrossPt ****
make all
g++ -c A.cpp
B.hpp:13: error: ISO C++ forbids declaration of ‘A’ with no type
B.hpp:13: error: expected ‘;’ before ‘*’ token
make: *** [A.o] Error 1
======================================
And this is the makefile I use:
all: test
test: A.o B.o TestCrossPt.o
g++ A.o B.o TestCrossPt.o -o test.exe -lpthread
A.o: A.cpp
g++ -c A.cpp
B.o: B.hpp
g++ -c B.cpp
TestCrossPt.o: TestCrossPt.cpp
g++ -c TestCrossPt.cpp
==========================================
Well, I am pretty new to C++ so please use easy language

many thanks
Rick
Last edited by Ancient Dragon; Nov 17th, 2007 at 5:28 pm. Reason: add code tags
The problem is that you are recursively including A.hpp and B.hpp. So solve the problem, just pre-declare the classes
#ifndef B_HPP_
#define B_HPP_
class A;
#include "A.hpp"
class B
{
public:
B();
virtual ~B();
private:
A* apt;
};
#endif /*B_HPP_*/ Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
![]() |
Similar Threads
- Cross Link Id Problem (Perl)
- Web hosting directories or Google (Networking Hardware Configuration)
- javascript to add select button (JavaScript / DHTML / AJAX)
- Visual Basic Shell Execute issue (Visual Basic 4 / 5 / 6)
- which language ? (Computer Science)
- C# : When exactly should a StringBuilder be used? (C#)
- Help with my Hijackthis log (Viruses, Spyware and other Nasties)
- Good advice when choosing an SEO service.... (Search Engine Optimization)
- Bruce Almighty Song (Geeks' Lounge)
- LCD troubles..works with monitor... (Monitors, Displays and Video Cards)
Other Threads in the C++ Forum
- Previous Thread: small problem in the string
- Next Thread: some errors with classes
| Thread Tools | Search this Thread |
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy directshow dll download dynamic dynamiccharacterarray email encryption error file format forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib library linkedlist linker list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings studio temperature template test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






