Why is my code generating a syntax error?

The following C ++ code generates this error:

error C2061: syntax error : identifier 'IObject'

Here is my code:

file: IObject.h

#include "IIStreamable.h"
using namespace Serialization;
namespace Object
{
    class IObject : public IIStreamable
    {
        virtual void AcceptReader( IIReader* reader ); 
        virtual void AcceptWriter( IIWriter* writer );
    };
}

file: IIWriter

#include "IObject.h"
#using namespace Object;
namespace Serialization
{
    class ICORE_API IIWriter
 {
public:
    // primitive "built in" value types
    virtual void writeChar(const char) =0;
    virtual void writeUChar(unsigned char) =0;
    virtual void writeCharPtr(const char*) =0;
    virtual void writeUCharPtr(const unsigned char*) =0;
    virtual void writeLong(long) =0;
    virtual void writeULong(unsigned long) =0;
    virtual void writeShort(short) =0;
    virtual void writeUShort(unsigned short) =0;
    virtual void writeInt(int) =0;
    virtual void writeUInt(unsigned int) =0;
    virtual void writeFloat(float) =0;
    virtual void writeDouble(double) =0;
    virtual void writeBool(bool) =0;
    virtual void writeObject(IObject*) =0;
    };
 }

file: IIStreamable

#include "IIReader.h"
#include "IIWriter.h"
namespace Serialization
{

class ICORE_API IIStreamable
    {
    public:
    virtual void AcceptReader(IIReader*) = 0;
    virtual void AcceptWriter(IIWriter*) = 0;
    };
 }

after compiling this code in vC ++ 2010 I got this error

error C2061: syntax error: identifier 'IObject'

in the IIWriter.h file and

error C2061: syntax error: identifier 'IIWriter'

in the file IObject.h and

error C2061: syntax error: identifier 'IIWriter'

in the IIStreamale.h file.

I can not understand why this error occurs?

Please help me

thank

+5
source share
2 answers

Using the using directive suggested by piokuc will still leave you a circular reference problem

You better change IObject.h to the following:

namespace Serialization
{
     class ICORE_API IIWriter;
     class ICORE_API IIReader;
}
namespace Object
{
    class IObject : public IIStreamable
    {
        virtual void AcceptReader( Serialization::IIReader* reader ); 
        virtual void AcceptWriter( Serialization::IIWriter* writer );
    };
}

IE #include forward declare IIReader IIWriter. , #include IObject.h , , , ....

virtual void writeObject( Object::IObject* ) = 0;
+3

#using namespace Object;

using namespace Object;
+2

All Articles