steveo225
steveo225

Reputation: 11882

Strange error when adding #include <string>

I have the following very simple application that compiles and runs fine:

EDIT: changed the example to be simpilar to end confusion of the real issue

int main() {
    return 0;
}

As soon as I add #include <string> (and not even reference std::string), it fails to compile and I get the following error:

/usr/include/c++/4.1.2/bits/allocator.h:82 error: expected template-name before '<' token

Along with about 456 other, similar errors.

Any ideas? Thanks!

UPDATE:

Line 82 of /usr/include/c++/4.1.2/bits/allocator.h references the template __glibcxx_base_allocator at the location of the error. That template is defined in bits/c++allocator.h. When I search the system for that file, I get 3 hits, but none of them are in /usr/include/c++/4.1.2/bits/ as one would expect.

I have version 3.1.6, 4.1.1, and 4.3.2, but not 4.1.2 as the rest of the includes I am using. I am not sure which one is being used (if any, however, I don't get any error for an unknown file), but it seems the problem may stem from this.

Upvotes: 0

Views: 2595

Answers (5)

steveo225
steveo225

Reputation: 11882

The problem appears to be the installed development packages are not correct or incomplete (not to be confused with corrupt). Forcing g++ to use different include versions corrects that:

g++ -nostdic++ hello.cc -o hello -I/usr/include/c++/3.4.6

All the alternative directories (4.1.1, 4.1.2 and 4.3.2) are incomplete causing inappropriate files to be included causing the unusually errors. For example:

/usr/include/c++/4.1.2/bits/allocator.h requires __glibcxx_base_allocator located in bits/c++allocator.h which is being included from either /usr/include/c++/4.1.1 or /usr/include/c++/4.3.2 and appear to be incompatible. Forcing the compiler to use the only complete set of includes rectifies this.

Upvotes: 1

dimba
dimba

Reputation: 27581

This could be error caused at preprocess stage. Just preprocess your cpp file by passing flag -E to gcc and Look at the place the compiler complains.

Upvotes: 0

Thomas Matthews
Thomas Matthews

Reputation: 57688

Check your include path. The paths can be specified as environment variables or specified on the command line. You could be using an include file from a different compiler or different version of the same compiler.

Also, try using <cstdio> rather than <stdio.h>.

Another suggestion: change <> to "".

Upvotes: 0

dnadlinger
dnadlinger

Reputation: 6130

Errors like this have been heard of to occur when the C++ standard library headers are corrupted/not fully installed – maybe there is even a message referring to a missing include among your 456 other errors.

In any case, make sure that libstdc++-devel, resp. the package containing the C++ standard library header files of your distribution, is properly installed.

Upvotes: 0

Mark B
Mark B

Reputation: 96241

Almost certainly g++ is detecting .cc as a C source file, not C++ and passes it through to gcc instead of compiling as C++. You can easily test by renaming your file to hello.C. There's also a language parameter to g++ you can use.

EDIT: This seems to work fine in g++ 4.2 with a .cc extension so that might not be it. Do you have any other headers included you aren't showing us? They could be interfering with <string>.

EDIT2: Alternatively your headers might not be set up right. Does this work:

#include <string>
int main()
{
    return 0;
}

Upvotes: 0

Related Questions