g++在'('令牌前期望非限定id

g++ expected unqualified-id before ‘(’ token

本文关键字:id 期望 令牌 g++      更新时间:2023-10-16

使用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某些宏,它可能会导致您看到的问题。

向我们展示您的代码,最好缩小到显示错误的最小源文件。