On Dec 9, 2:14 pm, babakan...@yahoo.com wrote:
Hi to every body...:D
I'm a novice C++ programmer & I've a question,
I have the ClassA & in it's constructor, I instantiate ClassB, and I
want send "this" pointer """pointer to ClassA""" to the ClassB.
But I get this Error from the compiler: and it's in ClassB...
Error from the compiler:
error C2061: syntax error : identifier 'TestA'
error C2143: syntax error : missing ';' before '*'
error C2501: 'TestA' : missing storage-class or type specifiers
error C2501: 'tA' : missing storage-class or type specifiers
//================================================== =========
// TestA.cpp
#include"TestA.h"
#include"TestB.h"
class TestA
{
public:
TestA(){ tB = new TestB; }
~TestA(){ delete tB; }
void ShowSomething(){ cout << "Here is TestA..." << endl; }
private:
TestB * tB;
};
//================================================== =========
// TestB.cpp
#include"TestB.h"
#include"TestA.h"
class TestB
{
public:
TestB();
~TestB();
void DoIt( TestA* aP ){ aP->ShowSomething(); } // >>>>>Here is the
error
private:
TestA * tA;
};
//================================================== =========
// Main.cpp
#include"TestB.h"
#include"TestA.h"
int main (int argc, char * argv)
{
TestA * aP = new TestA;
aP->CallTestB();
delete aP;
return 0;
}
I think you are trying to bite off more than you can chew.
Declarations go into header files (*.h or *.hpp) and implementations
into source files (*.cpp)
We can anly guess at what "TestA.h" and "TestB.h" might have as far as
declarations in the case above.
By the way, this isn't the better way of doing this. But at least
you've got a plan.
Don't look for any new/delete allocations in the example below, always
use the stack whenever possible.
See if you can follow this example, note the include guards and
forward declarations in the headers.
Note the const qualifiers as well.
// a.h - declaration for class A
#ifndef A_H
#define A_H
#include "b.h"
class B;
class A
{
public:
A(const B& r_b);
~A();
void show() const;
private:
const B* const p_b;
};
#endif
// a.cpp - Implementation of class A
#include <iostream>
#include "a.h"
A::A(const B& r_b) : p_b(&r_b)
{
}
A::~A()
{
}
void A::show() const
{
std::cout << "p_b = " << p_b;
std::cout << "\n p_b->show(): ";
p_b->show();
}
// b.h - Declaration of class B
#ifndef B_H
#define B_H
#include "a.h"
class A;
class B
{
public:
B();
~B();
void doit(A& r_a);
void show() const;
private:
A* p_a;
};
#endif
// b.cpp - Implementation of class B
#include <iostream>
#include "b.h"
B::B() : p_a(0)
{
}
B::~B()
{
}
void B::doit(A& r_a)
{
p_a = &r_a;
}
void B::show() const
{
if(p_a != 0)
{
std::cout << "p_a = " << p_a;
}
else
{
std::cout << "p_a is null!";
}
std::cout << std::endl;
}
// test.cpp - main entry
#include <iostream>
#include "a.h"
int main()
{
B b; // b.p_a is set to null via ctor init list
b.show(); // displays that null pointer
A a(b); // a.p_b is set in ctor
a.show(); // b.p_a is still null
b.doit(a); // set b.p_a, finally
a.show(); // ok
}
/*
p_a is null!
p_b = 0x7fff5fb47180
p_b->show(): p_a is null!
p_b = 0x7fff5fb47180
p_b->show(): p_a = 0x7fff5fb47170
*/