G ++ expected unqualified-id before '(token

I get this error when using stl_vector.h . I use 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 get this error, I searched a lot and found some β€œsimilar” problems, but I can not solve my problem.

EDIT: 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 

In the previous error log, you can see that the "vector" is called by the header "caction.h" as follows:

 //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 the / stl _vector.h bits as follows:

 #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 

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

Thanks in advance for your help.

+8
c ++ g ++
source share
3 answers

This may be caused by a preprocessor that corrupts your code, probably because you have a max macro. This can happen with the C library, because, as a rule, the C standard allows the standard functions of the C library to be actually macros (although I saw only such an error in MSVC).

To check you can

  • preprocess the source with gcc -E and search for the appropriate code. Check if it is damaged.
  • add the line #undef max to #include <vector> and see if it helps.
+9
source share

user977480, considering that you said: "I get this error when using stl_vector.h", I assume that your code uses 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. Instead, you need to use #include <vector> . stl_vector.h is an implementation detail, and it does not work on its own. The vector header file includes the / stl _vector.h bits after it has included some other detailed implementation files, including those that define std::max .

+2
source share

Never use identifiers starting with a double underscore, or an underscore followed by a capital letter, unless provided by the implementation.

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

It's not obvious that this is your problem, but see what happens if you change all of these identifiers.

EDIT : I was wrong, the code you submitted is part of the implementation, so using reserved identifiers is quite appropriate.

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

 #include <bits/stl_vector.h> 

I get 156 errors. The right way -

 #include <vector> 

but if you #define certain macros before #include <vector> , this may cause the problem you see.

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

+1
source share

All Articles