Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is my code generating a syntax error?

The following C++ code is generating 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 IObject.h file and

error C2061: syntax error : identifier 'IIWriter'

in the IIStreamale.h file.

i can not understand why this error occurs?

please help me

thanks

like image 504
Benyamin Jane Avatar asked Jun 11 '12 16:06

Benyamin Jane


2 Answers

Making use of a using directive as suggested by piokuc will still leave you the problem of a circular include reference

You would be best off changing the 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 drop the #include and forward declare IIReader and IIWriter. In fact you could avoid more confusion by dropping the #include IObject.h as well and forward declaring that similarly to above....

virtual void writeObject( Object::IObject* ) = 0;
like image 104
Goz Avatar answered Oct 18 '22 21:10

Goz


Replace

#using namespace Object;

with

using namespace Object;
like image 2
piokuc Avatar answered Oct 18 '22 22:10

piokuc