Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > passing this object

Reply
Thread Tools

passing this object

 
 
Jung, William
Guest
Posts: n/a
 
      11-27-2008
On Thu, 27 Nov 2008 04:29:30 -0800 (PST), maverik
<(E-Mail Removed)> wrote:

>On Nov 27, 3:17?pm, maverik <(E-Mail Removed)> wrote:
>> On Nov 27, 2:35?pm, "Jung, William" <(E-Mail Removed)> wrote:
>>
>> > the whole error log is like this:http://www.oniva.com/upload/1356/log.txt

>>
>> 1>d:\snd\remote\remotedemo\app\virtualawear\sock.h (31) : error C2061:
>> syntax error : identifier 'CVirtualScreen'
>>
>> CVirtualScreen is unknown identifier in sock.h. From where sock.h
>> should know what the hell is CVirtualScreen?

>
>Ok. If you included "Sock.h" into the "VirtualScreen.h", you can't
>include
>"VirtualScreen.h" into "Sock.h" because of circular dependence.
>
>So you can try to write something like:
>
>extern class VirtualScreen;
>
>in Sock.h.


Thanks so much your help.

OK, the errors goes away if I use forward class declaration or extern.

but when I added the line (sock.cpp)
Vscreen->PushMessage(std::string((const
char*)m_bReceiveBuffer + j));

where Vscreen is like this
CVirtualScreen *Vscreen; (sock.h)

http://www.oniva.com/upload/1356/e2.jpg


 
Reply With Quote
 
 
 
 
Thomas J. Gritzan
Guest
Posts: n/a
 
      11-27-2008
Jung, William write:
> On Thu, 27 Nov 2008 04:29:30 -0800 (PST), maverik
> <(E-Mail Removed)> wrote:
>
>> On Nov 27, 3:17?pm, maverik <(E-Mail Removed)> wrote:
>>> On Nov 27, 2:35?pm, "Jung, William" <(E-Mail Removed)> wrote:
>>>
>>>> the whole error log is like this:http://www.oniva.com/upload/1356/log.txt
>>> 1>d:\snd\remote\remotedemo\app\virtualawear\sock.h (31) : error C2061:
>>> syntax error : identifier 'CVirtualScreen'
>>>
>>> CVirtualScreen is unknown identifier in sock.h. From where sock.h
>>> should know what the hell is CVirtualScreen?

>> Ok. If you included "Sock.h" into the "VirtualScreen.h", you can't
>> include
>> "VirtualScreen.h" into "Sock.h" because of circular dependence.
>>
>> So you can try to write something like:
>>
>> extern class VirtualScreen;
>>
>> in Sock.h.

>
> Thanks so much your help.
>
> OK, the errors goes away if I use forward class declaration or extern.
>
> but when I added the line (sock.cpp)
> Vscreen->PushMessage(std::string((const
> char*)m_bReceiveBuffer + j));
>
> where Vscreen is like this
> CVirtualScreen *Vscreen; (sock.h)


You can't call member function on classes that are only forward
declared. Once you call member functions or create objects of a class,
you need the full declarationn of the class.

So make sure that you include the header for CVirtualScreen in sock.cpp.
Also, don't forget to use "include guards" in your headers.

There are some issues in the FAQ about that point:
http://www.parashift.com/c++-faq-lit...html#faq-39.11
and the two following.

Also, please post the error message you get. You can copy&paste it
directly from the output window. Most people won't click on wild links
in postings.

--
Thomas
 
Reply With Quote
 
 
 
 
Thomas J. Gritzan
Guest
Posts: n/a
 
      11-27-2008
Jung, William wrote:
> ===============
> Sock::Sock()
> {
> StartServer();
> }
>
> void Sock::Create1(CVirtualScreen *screen)
> {
> Vscreen = screen;
> }
>
> Sock::~Sock()
> {
> StopComm();
> }
> ===============
> class Sock : public CSocketComm
> {
> public:
> Sock();
> ~Sock();
> void Create1(CVirtualScreen *Vscreen);
> CVirtualScreen *Vscreen;
>
> };


Why did you remove the CVirtualScreen* from the constructor? If this
class needs a CVirtualScreen for its work, you should pass the pointer
in at the time of creation:

class Sock : public CSocketComm
{
public:
Sock(CVirtualScreen *Vscreen);
~Sock();

private:
CVirtualScreen *Vscreen;
};

Sock::Sock(CVirtualScreen *Vscreen)
: Vscreen(Vscreen) // <-- initializer list
{
StartServer();
}

Note also the use of the initializer list. In general, it's preferred in
C++ to initialize variables instead of assignment, when possible.

--
Thomas
 
Reply With Quote
 
Jung, William
Guest
Posts: n/a
 
      11-28-2008
On Thu, 27 Nov 2008 14:07:33 +0100, "Thomas J. Gritzan"
<(E-Mail Removed)> wrote:

>Jung, William write:
>> On Thu, 27 Nov 2008 04:29:30 -0800 (PST), maverik
>> <(E-Mail Removed)> wrote:
>>
>>> On Nov 27, 3:17?pm, maverik <(E-Mail Removed)> wrote:
>>>> On Nov 27, 2:35?pm, "Jung, William" <(E-Mail Removed)> wrote:
>>>>
>>>>> the whole error log is like this:http://www.oniva.com/upload/1356/log.txt
>>>> 1>d:\snd\remote\remotedemo\app\virtualawear\sock.h (31) : error C2061:
>>>> syntax error : identifier 'CVirtualScreen'
>>>>
>>>> CVirtualScreen is unknown identifier in sock.h. From where sock.h
>>>> should know what the hell is CVirtualScreen?
>>> Ok. If you included "Sock.h" into the "VirtualScreen.h", you can't
>>> include
>>> "VirtualScreen.h" into "Sock.h" because of circular dependence.
>>>
>>> So you can try to write something like:
>>>
>>> extern class VirtualScreen;
>>>
>>> in Sock.h.

>>
>> Thanks so much your help.
>>
>> OK, the errors goes away if I use forward class declaration or extern.
>>
>> but when I added the line (sock.cpp)
>> Vscreen->PushMessage(std::string((const
>> char*)m_bReceiveBuffer + j));
>>
>> where Vscreen is like this
>> CVirtualScreen *Vscreen; (sock.h)

>
>You can't call member function on classes that are only forward
>declared. Once you call member functions or create objects of a class,
>you need the full declarationn of the class.
>
>So make sure that you include the header for CVirtualScreen in sock.cpp.
>Also, don't forget to use "include guards" in your headers.


What is "include guards" in my headers? any example?

>
>There are some issues in the FAQ about that point:
>http://www.parashift.com/c++-faq-lit...html#faq-39.11
>and the two following.
>
>Also, please post the error message you get. You can copy&paste it
>directly from the output window. Most people won't click on wild links
>in postings.

 
Reply With Quote
 
Thomas J. Gritzan
Guest
Posts: n/a
 
      11-28-2008
Jung, William wrote:
>> So make sure that you include the header for CVirtualScreen in sock.cpp.
>> Also, don't forget to use "include guards" in your headers.

>
> What is "include guards" in my headers? any example?


First hit in your favorite search engine might find this:
http://en.wikipedia.org/wiki/Include_guard

In short, an include guard avoids multiple inclusion of a header file.
Example:

#ifndef SOCK_H_INCLUDED
#define SOCK_H_INCLUDED

class Sock
{
// ...
};

#endif // SOCK_H_INCLUDED

--
Thomas
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing an Object to Another Object Hal Vaughan C++ 12 03-28-2008 05:53 AM
Passing a reference to an object from inside the object's member functions? stefven blonqhern C++ 3 07-05-2007 06:49 PM
Passing Bound Object value to another Bound Object Phillip Vong ASP .Net 0 07-27-2006 10:54 PM
Object creation - Do we really need to create a parent for a derieved object - can't the base object just point to an already created base object jon wayne C++ 9 09-22-2005 02:06 AM
Passing derived class object array in place of base class object array justanotherguy63@yahoo.com C++ 9 12-03-2004 10:57 PM



Advertisments