g ++期望在'('标记之前)的非限定id

use*_*480 8 c++ g++

使用时我收到此错误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;
}
Run Code Online (Sandbox Code Playgroud)

usr/include/c ++/4.5/bits/stl_vector.h:1143:40:错误:‘(’令牌之前的预期unqualified-id

我不知道为什么我会收到这个错误,我搜索了很多,发现了一些"相似"的问题,但我无法解决这个问题.

编辑:所以这是错误日志:

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
Run Code Online (Sandbox Code Playgroud)

您可以在上一个错误日志中看到"vector"被标题"caction.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"
Run Code Online (Sandbox Code Playgroud)

然后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
Run Code Online (Sandbox Code Playgroud)

只有来自vector的最后两个标题(stl_vector和stl_bvector)给出了完全相同的错误,其余的都没问题.有任何想法吗?

在此先感谢您的帮助.

jpa*_*cek 10

这可能是由于预处理器损坏了您的代码,可能是因为您已经max定义了宏.这可能发生在C库中,因为通常C标准允许C标准库函数实际上是宏(尽管我只在MSVC上看到过这样的错误).

要检查,你可以

  • 预处理源gcc -E并搜索输出以获得相应的代码.检查它是否完好无损.
  • #undef max之前添加一行#include <vector>,看看是否有帮助.

  • 谢谢,#undef max解决了这个问题:) (3认同)

Kei*_*son 1

切勿使用以双下划线或下划线后跟大写字母开头的标识符,除非实现提供了它们。

编译器和/或标准库可以以它们喜欢的任何方式使用__N__s__len等。

这并不明显是你的问题,但看看如果你更改所有这些标识符会发生什么。

编辑:我错了,您发布的代码是实现的一部分,因此它使用保留标识符是完全合适的。

/usr/include/c++/4.5/bits/stl_vector.h我的系统上包含相同的代码。最有可能的是您自己的代码中的某些内容导致了错误。例如,如果我这样做

#include <bits/stl_vector.h>
Run Code Online (Sandbox Code Playgroud)

我收到 156 个错误。正确的方法是

#include <vector>
Run Code Online (Sandbox Code Playgroud)

但如果您#define在之前使用某些宏,#include <vector>则可能会导致您所看到的问题。

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

  • 他正在展示编译器标准库附带的代码,因此他不能也不应该更改它。 (3认同)