g++在'('令牌前期望非限定id
g++ expected unqualified-id before ‘(’ token
使用stl_vector.h
时出现此错误。我在Linux上用g++编译。
{
if (max_size() - size() < __n)
__throw_length_error(__N(__s));
const size_type __len = size() + std::max(size(), __n); //THE ERROR IS ON THIS LINE!
return (__len < size() || __len > max_size()) ? max_size() : __len;
}
usr/include/c++/4.5/bits/stl_vector.h:1143:40: error: expected unqualified-id before
‘(’
token
我不知道为什么我得到这个错误,我已经搜索了很多,发现一些"类似"的问题,但我不能解决我的。
编辑:这里是错误日志:
In file included from /usr/include/c++/4.5/vector:65:0,
from ../../RL_Toolbox/include/caction.h:34,
from ../../RL_Toolbox/include/cagent.h:35,
from shortestpathQLearning.cpp:42:
/usr/include/c++/4.5/bits/stl_vector.h:1143:40: error: expected unqualified-id before ‘(’ token
你可以在前面的错误日志中看到"vector"被头文件" action.h"像这样调用:
//THESE ARE THE INCLUDES IN "caction.h"
#ifndef CACTION_H
#define CACTION_H
#include <stdio.h>
#include <vector> //HERE IT CALLS <vector>
#include <list>
#include <map>
#include "cbaseobjects.h"
then Vector像这样调用bits/stl_vector.h:
#ifndef _GLIBCXX_VECTOR
#define _GLIBCXX_VECTOR 1
#pragma GCC system_header
#include <bits/stl_algobase.h>
#include <bits/allocator.h>
#include <bits/stl_construct.h>
#include <bits/stl_uninitialized.h>
#include <bits/stl_vector.h>//HERE IT CALLS stl_vector.h
#include <bits/stl_bvector.h> //Im actually getting the exact same error from stl_vector.h on this header
只是vector (stl_vector和stl_bvector)的最后2个头给了我完全相同的错误,其余的都可以。什么好主意吗?
提前感谢您的帮助。
这可能是由于预处理器损坏了您的代码,可能是因为您定义了宏max
。这可能发生在C库中,因为通常C标准允许C标准库函数实际上是宏(尽管我只在MSVC上看到过这样的不幸)。
查看,可以输入
- 用
gcc -E
预处理源代码,并在输出中搜索相应的代码。检查是否完好。 - 在
#include <vector>
之前添加#undef max
行,看看是否有帮助。
user977480,鉴于您说"我在使用stl_vector.h时得到此错误",我认为这意味着您的代码正在使用#include <bits/stl_vector.h>
或#include <c++/4.5/bits/stl_vector.h>
。
这个#include
语句是问题的原因。您需要使用#include <vector>
代替。Stl_vector.h是一个实现细节,它自己不能工作。vector
头文件在包含了一些其他实现细节文件(包括定义std::max
的文件)之后包含了bits/stl_vector.h。
永远不要使用以双下划线开头的标识符,或者下划线后跟大写字母的标识符,除非它们是由实现提供的。
编译器和/或标准库允许以任何方式使用
__N
、__s
、__len
等。
这不是你的问题,但看看如果你改变所有这些标识符会发生什么。
EDIT:我错了,你发布的代码是实现的一部分,所以它使用保留标识符是完全合适的。
在我的系统上 /usr/include/c++/4.5/bits/stl_vector.h
包含相同的代码。很可能是您自己的代码中的某些内容导致了错误。例如,如果我输入
#include <bits/stl_vector.h>
我得到156个错误。正确的方法是
#include <vector>
但是如果您在#include <vector>
之前#define
某些宏,它可能会导致您看到的问题。
向我们展示您的代码,最好缩小到显示错误的最小源文件。
- 从函数角度看ID到文件路径的内部与外部映射
- 通过组合不同的类型来创建唯一的id
- 我不断收到 [错误] ID 返回 1 退出状态错误,但看不到问题所在
- FFMPEG配置文件级别id大小无效
- 方法内部但循环仍得到预期的不合格id错误C++
- 从类型std::函数传递变量失败,尽管调用方期望的类型完全相同
- 如何获取 GLFW 窗口 ID?
- 当简单捕获中的标识符显示为参数的声明符 ID 时,没有编译器诊断
- 显示数组中的学生 ID 和最高分
- 一元*运算符的操作数是否期望一个 prvalue
- C++:"("令牌"之前有预期的非限定 id 指向类中成员函数的指针
- 在PostgreSQL中根据它们的ID选择大量行的最快方法是什么?
- Xcode 9.4.1 中的 Apple Mach-O 链接器 (id) 错误
- 错误:令牌 { '{' 之前应存在非限定 ID
- 断言"id < 0"在Qt ActiveX中失败
- C++部分概念 id:显式模板规范顺序/第一个参数的特殊状态的原因是什么?
- 如何根据两个因素组织向量:id 和数量?(C++)
- 在返回 0 之前应为非限定 ID
- g++在'('令牌前期望非限定id
- QT 5.2.1编译错误;在'delete'之前期望的非限定id