g++预期在 ‘(’ 令牌之前有不合格的 id

发布于 2024-12-07 14:24:02 字数 1714 浏览 0 评论 0原文

我在使用 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: 错误:'(' 标记之前预期有非限定 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

您可以在前面的错误日志中看到“向量”被标头调用。像这样的“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"

然后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

只有向量的最后2个头(stl_vector和stl_bvector)给了我完全相同的错误,其余的都可以,

提前感谢您 。你的帮助。

I'm getting this error when using stl_vector.h. I'm on Linux using g++ to compile.

{
  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

I'm not sure why I'm getting this error, I have searched a lot and found some "similar" problems but I can't solve mine.

EDIT: so here's the error log:

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

You can see in the previous error log that "vector" gets called by the header "caction.h" like this:

//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 calls bits/stl_vector.h like this:

#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

just the last 2 headers from vector (stl_vector and stl_bvector) give me the exact same error, the rest are ok. Any ideas?

Thanks in advance for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

遥远的绿洲 2024-12-14 14:24:02

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

要进行检查,您可以

  • 使用 gcc -E 预处理源代码并在输出中搜索相应的代码。检查是否完好。
  • #include 之前添加一个 #undef max 行,看看是否有帮助。

This may be caused by the preprocessor damaging your code, probably because you have macro max defined. This can happen with the C library, because generally the C standard allows C standard library functions to be actually macros (although I've only seen such a mishap on MSVC).

To check, you can

  • preprocess the source with gcc -E and search the output for the corresponding code. Check if it is undamaged.
  • add an #undef max line before #include <vector> and see if it helps.
星軌x 2024-12-14 14:24:02

user977480,鉴于您说“我在使用 stl_vector.h 时遇到此错误”,我认为这意味着您的代码正在使用类似 #include>#include

#include 语句是导致您出现问题的原因。您需要使用 #include 代替。 stl_vector.h 是一个实现细节,它本身不能工作。 vector 头文件在包含了一些其他实现细节文件(包括定义 std::max 的文件)之后,还包含了 bits/stl_vector.h。

user977480, given that you said "I'm getting this error when using stl_vector.h", I assume that means your code is using something like #include <bits/stl_vector.h> or #include <c++/4.5/bits/stl_vector.h>.

This #include statement is the cause of your problem. You need to use #include <vector> instead. stl_vector.h is an implementation detail and it does not work by itself. The vector header file includes bits/stl_vector.h after it has included some other implementation detail files, including the one that defines std::max.

花心好男孩 2024-12-14 14:24:02

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

编译器和/或标准库允许使用 < code>__N、__s__len 等等,以任何他们喜欢的方式。

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

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

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

#include <bits/stl_vector.h>

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

#include <vector>

但如果您在 #include 之前 #define 某些宏,则可能会导致您遇到的问题。

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

Never use identifiers starting with either a double underscore, or an underscore followed by an uppercase letter, unless they're provided by the implementation.

The compiler and/or standard library are permitted to use __N, __s, __len, and so forth, in any way they like.

It's not obvious that that's your problem, but see what happens if you change all those identifiers.

EDIT: I was wrong, the code you posted is part of the implementation, so its use of reserved identifiers is perfectly appropriate.

/usr/include/c++/4.5/bits/stl_vector.h on my system contains the same code. Most likely something in your own code is causing the error. For example, if I do

#include <bits/stl_vector.h>

I get 156 errors. The correct way is

#include <vector>

but if you #define certain macros before the #include <vector> it might cause the problem you're seeing.

Show us your code, preferably narrowed down to the smallest source file that exhibits the error.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文