UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

  • Index
  • » C++
  • » Problem in using Classes

#1 2004-05-27 07:16 PM

hpdp
Member
From: Maryland
Registered: 2004-05-12
Posts: 11

Re: Problem in using Classes

I’m modifying other people’s code, and encounter this problem. Class A need to use class B, class B need to use class A.
In Class A.h:
#include B.h
Class A {
     ….
     Post_access(B *pb);
     ….
}

In Class B.h:
#include A.h
Class B {
    ….
    Convert(A *pa);         //line ##
    ….
}
The compiler gives error:
on line ##, type specifier committed for parameter, parse error  before “*” token.

How do I go around to let these two class access each other? Can this been done?
Thank you!

Offline

#2 2004-05-27 07:54 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: Problem in using Classes

I believe you can just pre-declare the class, without defining it...
Eg., in your "A.h", do:

class B;

And, in "B.h" do:

class A;

Rather than including the other header from each one...  (Which
would create infinite recursion, BTW, unless you did an #ifndef
trick at the top of your .h files...)

Offline

#3 2004-05-27 09:20 PM

hpdp
Member
From: Maryland
Registered: 2004-05-12
Posts: 11

Re: Problem in using Classes

Thanks a lot! It works! :)

Offline

  • Index
  • » C++
  • » Problem in using Classes

Board footer

Powered by FluxBB