How can I send "this" pointer to another Instantianted class...

B

babakandme

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;
}
 
D

Daniel T.

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

Looking at the below makes me wonder, what is in TestA.h and testB.h?
 
W

werasm

Looking at the below makes me wonder, what is in TestA.h and testB.h?

I think your question may be the key to the OPs answer. ;-). Also
consider forward declarations, whatever that might be.

Regards,

Werner
 
B

babakandme

Is there anybody?
I think that there is problem,
Because I see in the Discussions Page, there are 3 messages about
this article,
but in the article page, there is just my original ...

I appreciate for your help...:D
 
S

Salt_Peter

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
*/
 
K

Kai-Uwe Bux

Is there anybody?

Yes, plenty.
I think that there is problem,
Because I see in the Discussions Page, there are 3 messages about
this article,
but in the article page, there is just my original ...

There is no problem. In my news reader I see postings in the thread you
started just fine.

You seem to mistake this news group for some sort of chat room or other
online forum. Please familiarize yourself with usenet news groups and how
they work.


Best

Kai-Uwe Bux
 
B

babakandme

Thanks for your kindly help.

forward declaration ---> works...:D

I appreciate once again.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,262
Messages
2,571,045
Members
48,769
Latest member
Clifft

Latest Threads

Top