0

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.

13
  • Additionally: I just use g++ hello.cc -o hello to compile, nothing fancy Commented Mar 8, 2011 at 20:11
  • Can you post the entire file that fails? Commented Mar 8, 2011 at 20:13
  • 1
    What is the output of g++ --version? Commented Mar 8, 2011 at 20:13
  • 1
    Where in the file are you adding #include <string>? Somewhere near the top? Are you putting it on a line of its own? Commented Mar 8, 2011 at 20:14
  • 2
    Do you get the same error if you put the #include <string> statement on the 1st line? Commented Mar 8, 2011 at 20:20

5 Answers 5

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for all the help everybody. After many days of working towards an answer, I finally figured it out and thought I would share my findings.
There is a typo its -nostdinc++ not -nostdic++
0

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;
}

3 Comments

I doubt it, as it's throwing an error about expecting a template name. Also, .cc is a common c++ extension.
I also doubt it. This isn't my actual application shock but my real one uses classes and inheritance with no problem, just seems to be STL
No, no other includes, and your example failed with the same errors as well.
0

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.

1 Comment

I did recently make sure the correct version was installed, then reinstalled it, not saying the package on disc isn't corrupted somehow. Also, I checked the entire build log, no complaining of missing files, and most of the errors were invalid use of undefined type 'struct std::allocator<char>'
0

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 "".

3 Comments

The problem isn't with stdio.h, that works fine, the problem is with any STL include, like string, vector, iostream, etc. g++ --version reported 4.1.2 which is also the version folder where include files are found, so I am pretty sure that is matching up.
@steveo255: Are you sure that is not where the problem is? Have you tried using this C++ header rather than the old C one?
The problem is not with printing, I changed the example to reflect this (no more stdio.h or printf). The problem is when I include any STL header, it won't compile and generates the same error.
0

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.

3 Comments

I tried that, and got a large file output, especially since the application does nothing. I didn't see anywhere in the output where the pre-processor complained and nothing was sent to stderr, but I do admit I am unfamiliar with that particular output.
I don't mean preprocessor error, but rather error introduced after preprocessor did it's job. Just consider what effect after preprocess will be on code if you have such a stupid macro as "#define public private". Your code probably won't compile.
Just look in preprocedd file line 82 of /usr/include/c++/4.1.2/bits/allocator.h. If the line looks different, than you have somewhere macro that altered the line

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.