简体   繁体   English

错误:“数字”之前的预期类型说明符

[英]error: expected type-specifier before ‘Number’

I have attempted to figure out the error above but have come nowhere. 我试图找出以上错误,但无济于事。 Every time I compiler I get the error: 每次编译时,我都会收到错误消息:

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

I have verified that I am including the correct header file in the source files so that the compiler recognizes the file. 我已经验证我在源文件中包括了正确的头文件,以便编译器可以识别该文件。 Below is the code concerning the error 以下是有关错误的代码

Parser.cpp: 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: 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: object_model.cpp:

 #include "object_model.h" String::String(std::string content) { this->content = content; } String::~String() { } ValueType String::getType() { return (ValueType)2; } 

Another thing that I have noticed if that I change String to Text then the code compiles completely. 我注意到的另一件事是,如果我将String更改为Text,则代码将完全编译。 Not sure why but would the name String ever conflict with the std::string class? 不知道为什么,但是名称String是否会与std :: string类冲突?

What Chris means when he says "No, it conflicts with your other String identifier" is that your 'class String' clashes with the identifier 'String' from "enum ValueType { Object = 0, Array = 1, String = 2, Number = 3, True = 4, False = 5, Null = 6};", so what the compiler sees for 克里斯说“不,它与您的其他字符串标识符冲突”的意思是,您的“类字符串”与“枚举ValueType {对象= 0,数组= 1,字符串= 2,数字= 3,True = 4,False = 5,Null = 6};“,因此编译器会看到

Value* val = new String(name);

is

Value* val = new 2(name);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM