| | |
Require help to solve!!!
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2007
Posts: 19
Reputation:
Solved Threads: 0
Hello,
I require a help on a basic issue.
I have 2 classes and I want to have an interaction among the classes.
Suppose my 2 classes are testA and testB. Now I want a handle of object pointer in each class to the other class. Like I want a variable in testA to be testB* t and a variable in testB to be testA* t.
How can I implement this situation???
I have included the code for easy understanding. This code doesn't work since it is not able to resolve the declarations.
Your help is highly appreciated!!!
I require a help on a basic issue.
I have 2 classes and I want to have an interaction among the classes.
Suppose my 2 classes are testA and testB. Now I want a handle of object pointer in each class to the other class. Like I want a variable in testA to be testB* t and a variable in testB to be testA* t.
How can I implement this situation???
I have included the code for easy understanding. This code doesn't work since it is not able to resolve the declarations.
Your help is highly appreciated!!!
C++ Syntax (Toggle Plain Text)
#include <stdio.h> class TestA { public: TestA(); int id; TestB* b; void testPrint(); void test(); void set(TestB* t); }; void TestA::testPrint() { printf("I am in class TestA\n"); } TestA::TestA() { id = 1; } void TestA::set(TestB* t) { b = t; } void TestB::test() { printf("My ID is: %d and the other ID is: %d\n", this->id, b->id); } class TestB { public: TestB(); TestA* a; int id; void testPrint(); void test(); void set(TestA* t); }; void TestB::testPrint() { printf("I am in class TestB\n"); } TestB::TestB() { id = 2; } void TestB::set(TestA* t) { a = t; } void TestB::test() { printf("My ID is: %d and the other ID is: %d\n", this->id, a->id); }
Last edited by abhishek2301; May 28th, 2009 at 12:21 pm. Reason: wrong code tags
•
•
Join Date: Nov 2007
Posts: 19
Reputation:
Solved Threads: 0
I don't think it will solve the problem.
Check the output:
testA.C:26: error: invalid use of undefined type ‘struct TestB’
testA.C:3: error: forward declaration of ‘struct TestB’
testA.C: In member function ‘void TestB::test()’:
testA.C:27: error: invalid use of undefined type ‘struct TestB’
testA.C:3: error: forward declaration of ‘struct TestB’
testA.C:27: error: ‘b’ was not declared in this scope
Check the output:
testA.C:26: error: invalid use of undefined type ‘struct TestB’
testA.C:3: error: forward declaration of ‘struct TestB’
testA.C: In member function ‘void TestB::test()’:
testA.C:27: error: invalid use of undefined type ‘struct TestB’
testA.C:3: error: forward declaration of ‘struct TestB’
testA.C:27: error: ‘b’ was not declared in this scope
Well it won't because it doesn't know what TestB is yet.
C++ Syntax (Toggle Plain Text)
class TestB; class TestA { // stuff }; class TestB { // stuff }; // methods
•
•
Join Date: Nov 2007
Posts: 19
Reputation:
Solved Threads: 0
Thanks very much!!!
I need a little more help.
If I want to place testA.C and testB.C in separate files then how to do it???
Like the following:
testA.C should look like:
testB.C should look like:
But the above method gives error for multiple declarations.
How to fix this issue???
I need a little more help.
If I want to place testA.C and testB.C in separate files then how to do it???
Like the following:
testA.C should look like:
C++ Syntax (Toggle Plain Text)
#include "testB.C" class testA { // stuff };
testB.C should look like:
C++ Syntax (Toggle Plain Text)
#include "testA.C" class testB { // stuff };
But the above method gives error for multiple declarations.
How to fix this issue???
Well the classes themselves go in
TestA.h and TestB.h
The actual implementations go in
TestA.cpp and TestB.cpp
Further, when you do something like
b->id
within an 'A' member function, you should really use something like
b->getID()
so that later on, when you make all the data private, your code will still work.
TestA.h and TestB.h
The actual implementations go in
TestA.cpp and TestB.cpp
Further, when you do something like
b->id
within an 'A' member function, you should really use something like
b->getID()
so that later on, when you make all the data private, your code will still work.
•
•
Join Date: Nov 2007
Posts: 19
Reputation:
Solved Threads: 0
Well,
My files are like this:
testA.h
testA.C
testB.h
testB.C
On compiling
g++ -c testA.C or g++ -c testB.C
giving many multiple declaration errors!!!
Please help me to fix this issue!!!
My files are like this:
testA.h
C++ Syntax (Toggle Plain Text)
#include <stdio.h> #include "testB.h" class TestA { public: TestA(); int id; TestB* b; void testPrint(); void test(); void set(TestB* t); };
C++ Syntax (Toggle Plain Text)
#include "testA.h" void TestA::testPrint() { printf("I am in class TestA\n"); } TestA::TestA() { id = 1; } void TestA::set(TestB* t) { b = t; } void TestA::test() { printf("My ID is: %d and the other ID is: %d\n", this->id, b->id); }
C++ Syntax (Toggle Plain Text)
#include <stdio.h> #include "testA.h" class TestB { public: TestB(); TestA* a; int id; void testPrint(); void test(); void set(TestA* t); };
C++ Syntax (Toggle Plain Text)
#include "testB.C" void TestB::testPrint() { printf("I am in class TestB\n"); } TestB::TestB() { id = 2; } void TestB::set(TestA* t) { a = t; } void TestB::test() { printf("My ID is: %d and the other ID is: %d\n", this->id, a->id); }
On compiling
g++ -c testA.C or g++ -c testB.C
giving many multiple declaration errors!!!
Please help me to fix this issue!!!
•
•
Join Date: Sep 2008
Posts: 36
Reputation:
Solved Threads: 1
put the declaration in the header file and the definition in the other
CPP Syntax (Toggle Plain Text)
//declaration class foo { void bar(); }; //definition void foo::bar() { }
All i've got is a slice of pi
![]() |
Similar Threads
- Send data on a serial port (C++)
- Win XP stop error (Windows NT / 2000 / XP)
- Internet Explorer & Windows Explorer (desktop shortcuts) Won't Load! (Viruses, Spyware and other Nasties)
- new window in Internet Explorer is always blank (Web Browsers)
- Obtain Remote Assistance by Sending an E-mail Message in Windows XP (Windows tips 'n' tweaks)
- Obtain Remote Assistance by Sending an E-mail Message in Windows XP (Windows tips 'n' tweaks)
- 35 processes, need to trim the fat (Viruses, Spyware and other Nasties)
- please help been at my pc for hours still cannot solve problem (Viruses, Spyware and other Nasties)
Other Threads in the C++ Forum
- Previous Thread: Please Hlpe me, lojec erooe
- Next Thread: image in picture control not displayed properly after moving
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






