为什么我的代码会产生语法错误?

时间:2012-06-11 16:37:54

标签: c++ visual-c++ syntax-error identifier

以下C ++代码生成此错误:

error C2061: syntax error : identifier 'IObject'

这是我的代码:

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 );
    };
}

文件: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;
    };
 }

文件:IIStreamable

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

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

在vc ++ 2010中编译此代码后出现此错误

错误C2061:语法错误:标识符'IObject'

在IIWriter.h文件和

错误C2061:语法错误:标识符'IIWriter'

IObject.h文件中的

错误C2061:语法错误:标识符'IIWriter'

在IIStreamale.h文件中。

我无法理解为什么会出现这种错误?

请帮帮我

感谢

2 个答案:

答案 0 :(得分:3)

使用piokuc建议的using指令仍然会给你一个循环包含引用的问题

最好将IObject.h更改为以下内容:

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并转发声明IIReader和IIWriter。事实上,你可以通过删除#include IObject.h来避免更多的混淆,并向前声明与上面相似......

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

答案 1 :(得分:2)

替换

#using namespace Object;

using namespace Object;