473,396 Members | 1,812 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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
6 2301
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
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
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
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
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
Thanks for your kindly help.

forward declaration ---works...:D

I appreciate once again.
Dec 9 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Ernst Murnleitner | last post by:
Dear Readers, Is it possible to forbid conversion from this or use of this in general except where it is explicitly wanted? Reason: I changed my program from using normal pointers to...
32
by: Christopher Benson-Manica | last post by:
Is the following code legal, moral, and advisable? #include <iostream> class A { private: int a; public: A() : a(42) {}
9
by: aden | last post by:
I have read the years-old threads on this topic, but I wanted to confirm what they suggest. . . Can the this pointer EVER point to a type different from the class that contains the member...
8
by: solarin | last post by:
Hi all. I'm writting a logger class to write all the debug/info/warning/error messages in a file. Every time a class needs to send any message, should send a code (int) and a message (string)....
4
by: craig | last post by:
During construction of an object "parent", if you create a subobject that stores a pointer to the parent (through the "this" pointer), will that pointer be valid when the subobject is later called?...
10
by: Angel Tsankov | last post by:
Hello! Is the following code illformed or does it yield undefined behaviour: class a {}; class b {
3
by: arnuld | last post by:
this is from C++ Primer 4/e page 260: there is a class named Sales_item and same_isbn is a member function of that class. bool same_isbn(const sales_item &rhs) const { return isbn ==...
3
by: Martin Drautzburg | last post by:
For reasons related to python/swig and garbage collecting I want to remember every object in a linked list. I have a common base class RCObj which in turn has a static std::list<RCObj*memory. My...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.