错误:“数字”之前的预期类型说明符
error: expected type-specifier before ‘Number’
我试图找出上面的错误,但一无所获。 每次我编译器时都会收到错误:
/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);
我已经验证了我在源文件中包含了正确的头文件,以便编译器识别该文件。 以下是有关错误的代码
解析器.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;
}
我注意到的另一件事是,如果我将字符串更改为文本,那么代码就会完全编译。 不知道为什么,但字符串这个名字会与 std::string 类冲突吗?
当 Chris 说"不,它与您的其他字符串标识符冲突"时,您的"类字符串"与来自"枚举 ValueType { Object = 0, Array = 1, String = 2, Number = 3, True = 4, False = 5, Null = 6};"中的标识符"字符串"冲突,所以编译器看到的内容
Value* val = new String(name);
是
Value* val = new 2(name);
相关文章:
- 缺少类型说明符,显式类型为"缺少错误"
- 与 tesseract::TessBaseApi() 相关的 Tesseract-OCR 出错(预期的类型说明符)
- 为什么转换函数声明不需要至少一个定义类型说明符
- C++需要所有声明的类型说明符和预期的非限定 id(需要调试帮助)
- printf 数据类型说明符复杂问题
- C++错误:"class name"之前的预期类型说明符
- 缺少类型说明符和另一个问题
- 缺少类型说明符 - 假定为 int.注意 c++ 不支持 default-int
- 错误:C4430:缺少类型说明符-假定为int.注意:C++不支持default-int
- 在C++中出现错误之前,无效使用了不完整的类型和所需的类型说明符
- 缺少类型说明符 - int 假定 声明静态成员后出错
- 不能在类型说明符中定义联合
- 没有带有字符串数组的存储类或类型说明符
- 警告:函数使用不带尾随返回类型的'auto'类型说明符
- 仅为某些模板专用化定义转换运算符:预期类型/预期类型说明符
- 在 C++ 中出现unique_ptr错误之前的预期类型说明符
- 有没有办法将类型说明符存储在变量中
- C 期望类对象的类型说明符
- 是无符号的int"简单类型说明符"
- 在 vscode 上编译错误.类型说明符是 C++11 扩展