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>
#inc开发者_如何学运维lude <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.
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.
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
.
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.
精彩评论