我试图弄清楚上面的错误,但无处可去。每次我编译时都会收到错误:
/home/duncan/Desktop/OOPS/dac80/json/parser.cpp: In function ‘Value* parseString(std::stringstream&)’:
/home/duncan/Desktop/OOPS/dac80/json/parser.cpp:149:19: error: expected type-specifier before ‘String’
Value* val = new String(name);
我已经验证我在源文件中包含了正确的头文件,以便编译器识别该文件。以下是有关错误的代码
Parser.cpp:
#include "object_model.h"
Value* parseString(std::stringstream& in)
{
std::string name("123");
Value* val = new String(name);
return val;
}
object_model.hpp:
#ifndef OBJECTMODEL_H
#define OBJECTMODEL_H
#include <string>
#include <sstream>
#include <map>
#include <vector>
enum ValueType { Object = 0, Array = 1, String = 2, Number = 3, True = 4, False = 5, Null = 6};
class Value
{
public:
Value() {}
virtual ValueType getType() = 0;
};
class String : public Value
{
public:
String(std::string content);
~String();
std::string content;
virtual ValueType getType();
};
#endif
object_model.cpp:
#include "object_model.h"
String::String(std::string content)
{
this->content = content;
}
String::~String()
{
}
ValueType String::getType()
{
return (ValueType)2;
}
我注意到的另一件事是,如果我将String更改为Text,那么代码将完全编译。不确定为什么但名称String会与std :: string类冲突吗?
答案 0 :(得分:2)
Chris说“不,它与你的其他字符串标识符冲突”的意思是你的'class String'与“enum ValueType {Object = 0,Array = 1,String = 2”中的标识符'String'冲突,Number = 3,True = 4,False = 5,Null = 6};“,编译器看到的内容
Value* val = new String(name);
是
Value* val = new 2(name);