0

I am trying to create a CBuffer member in every CClient class like this :

#ifndef CCLI_INC
#define CCLI_INC
#include "CBuffer.h"
#include "main.h"

    class CClient
    {
    private:

        CBuffer *m_buffer;


    public:
        CClient();



    };

but this code gives me

 error C2143: syntax error : missing ';' before '*'
 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

Although I have included cbuffer.h header, it gives this weird error..

deniz
  • 2,427
  • 4
  • 27
  • 38

1 Answers1

2

Forward declare CBuffer and lose the include.

#ifndef CCLI_INC
#define CCLI_INC
#include "main.h"
class CBuffer;
class CClient
{
private:

    CBuffer *m_buffer;
public:
    CClient();
};
Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • yeah thanks it didn't give any errors. can you please explain why should i re-define class ? – deniz May 15 '12 at 13:46
  • @deniz you're not re-defining the class, only declaring it. When you only use it as a pointer, or parameter, or return type, you don't need a full definition, but only to tell the compiler that a class with that name exists. And forward declarations should be prefered to inclusions when possible. – Luchian Grigore May 15 '12 at 13:48
  • should i declare it on each file i will use ? by the way main.h includes all of the header files with #pragma once. – deniz May 15 '12 at 13:51
  • @deniz if you can, yes. Always prefer forward-declaration instead of an include. Refer to this answer for reasons why you should do so - http://stackoverflow.com/questions/9906402/should-one-use-forward-declarations-instead-of-includes-wherever-possible/9906429#9906429 – Luchian Grigore May 15 '12 at 14:06