Abdul jalil
Abdul jalil

Reputation: 124

GCC problem: in template

i have redhat with gcc 4.1.1 i have compile as "gcc test.c" and give the following error

Error : expected '=' ,',' , ';' , ásm' or '__ attribute__' before '<' token

the code in "test.c" is as follow

template <typename T> class A {
public:
    T foo;
};

Upvotes: 1

Views: 162

Answers (3)

Michael Kristofik
Michael Kristofik

Reputation: 35198

From the GCC Manual, compiling a file with the .c extension will compile your code as though it were C, not C++. The easiest solution is to compile your code with g++ instead. The g++ command sets the default language to C++ and automatically links your code against the C++ standard library. You can do both of those with gcc but you have to do it by hand. Exactly how you do that is left as an exercise. :-)

Upvotes: 2

H&#229;vard S
H&#229;vard S

Reputation: 23886

This is C++ code, not C. You need to use g++, i.e. g++ test.c. Also, to avoid confusion, you should rename your file to end with .cpp or .cxx.

Upvotes: 2

kennytm
kennytm

Reputation: 523634

Compile with g++ and/or rename your file to test.cpp.

If you compile with gcc test.c then your file will be assumed as a C file. There's no templates in C.

Upvotes: 6

Related Questions