By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,422 Members | 1,336 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,422 IT Pros & Developers. It's quick & easy.

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

P: n/a
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;
}
Dec 9 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
ba********@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

Looking at the below makes me wonder, what is in TestA.h and testB.h?
//================================================== =========
// 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;
}
Dec 9 '07 #2

P: n/a
On Dec 9, 8:39 pm, "Daniel T." <danie...@earthlink.netwrote:
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

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
Dec 9 '07 #3

P: n/a
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
Dec 9 '07 #4

P: n/a
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
*/
Dec 9 '07 #5

P: n/a
ba********@yahoo.com wrote:
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
Dec 9 '07 #6

P: n/a
Thanks for your kindly help.

forward declaration ---works...:D

I appreciate once again.
Dec 9 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.