1

I'm doing my project in visual studio 2015 community version.

I want to create two class.

class A {
    B* b;
    public:
    //several method... 
};
class B : public A {
    public
    //several method...
};

Is this possible?

I keep receive error code C2504. But I cannot translate this error message to English.

I'm sorry that I don't show this error message.


Two class are separated and each class .h file include others .h file For example, include "B.h" in A.h and vice versa.


I'm sorry I forgot adding semicolon.

arubirate
  • 87
  • 6
  • 1
    Check for [forward declaration](https://en.wikipedia.org/wiki/Forward_declaration). – 101010 Jun 16 '16 at 14:59
  • I also got a C2061, Syntax error of identifier A and B – arubirate Jun 16 '16 at 14:59
  • You need to both forward-declare `B`, and put semicolons after your class declarations. – md5i Jun 16 '16 at 15:00
  • You need `;` at the end of definition of each class – 101010 Jun 16 '16 at 15:01
  • If B needs to everything in A, which in turn depends on B for some things to work, why don't you create B to be independent of A, from what I'm seeing here it's like calling a function within itself- they call it recursion or? in that way, every function in A that you need becomes a local function to B's members – Clement Osei Tano Jun 16 '16 at 15:04
  • Strange design - what do you want to achieve? There might be better options... – Aconcagua Jun 16 '16 at 15:13
  • @Aconcagua A class is a Member of auction system. and B is a Seller of auction system. So, A system member can register products to sell by a Seller class. But if i login I only got a Member class. because of that, I create that structure.. – arubirate Jun 16 '16 at 16:38
  • @arubirate I have difficulties to follow you... Shouldn't you know at login time, if a member is a seller or a customer, and create the appropriate objects right then? It seems to me as if you'd rather want three classes: `Member`, `Customer : Member` and `Seller : Member`. Then Customer would have a pointer to the Seller he/she is associated to, whereas Seller does not inherit the pointer to another Seller. – Aconcagua Jun 16 '16 at 16:54

1 Answers1

1

Just write

class A {
    class B* b;
    ^^^^^
    public:
    //several method... 
};
 ^^
class B : public A {
    public
    //several method...
};
 ^^

Or

class B;
^^^^^^^^
class A {
    B* b;
    public:
    //several method... 
};
class B : public A {
    public
    //several method...
};

You should not include each header in other header. Otherwise the classes will be defined twice.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Wow it works, Thanks! – arubirate Jun 16 '16 at 15:05
  • 1
    If including the headers in each other caused a multiple definition, then those classes couldn't be depended on by more than one header. Include guards should be used to guard against that. That is not to say that OP shouldn't not do that however. To do so is completely redundant, and if one header *actually* depends on the other, then circular inclusion may cause them to be included in the wrong order. – eerorika Jun 16 '16 at 15:08