gcc模板错误

时间:2010-07-11 00:50:44

标签: c++ generics gcc templates metaprogramming

为什么在这段代码的第一行:

template <typename VectorType>
std::string repr_vector_dynamic(const VectorType* vect)
{
    std::stringstream strs;
    strs << "(";
    for (int i = 0; i < vect->size(); ++i) {
        if (0 != i) {
            strs << ", ";
        }
        strs << (*vect)[i];
    }
    strs << ")";
    return strs.str();
}

我总是在第一行遇到错误:( MinGW gcc 4.5)

  

预期';'在“模板”之前

     

'template'之前的预期primary-expression

THX

稍后编辑:这是我从eclipse获得的命令行/日志:

g++ -II:\proj\bp\PyCML -IC:\PF\Python26\include -II:\proj\bp/include -O3 -Wall -c -fmessage-length=0 -oPyCML\cml.o ..\PyCML\cml.cpp
In file included from I:\proj\bp/include/boost/python/object/make_instance.hpp:9:0,
             from I:\proj\bp/include/boost/python/object/make_ptr_instance.hpp:8,
             from I:\proj\bp/include/boost/python/to_python_indirect.hpp:11,
             from I:\proj\bp/include/boost/python/converter/arg_to_python.hpp:10,
             from I:\proj\bp/include/boost/python/call.hpp:15,
             from I:\proj\bp/include/boost/python/object_core.hpp:14,
             from I:\proj\bp/include/boost/python/args.hpp:25,
             from I:\proj\bp/include/boost/python.hpp:11,
             from ..\PyCML\cml.cpp:11:
I:\proj\bp/include/boost/python/object/instance.hpp:14:36: warning: type attributes ignored after type is already defined
In file included from ..\PyCML\cml.cpp:28:0:
I:\proj\bp\PyCML/PyCMl/vector.h: In function 'void init_module_PyCML()':
I:\proj\bp\PyCML/PyCMl/vector.h:22:1: error: expected primary-expression before 'template'
I:\proj\bp\PyCML/PyCMl/vector.h:22:1: error: expected ';' before 'template'
..\PyCML\cml.cpp:58:1: error: expected '}' at end of input

4 个答案:

答案 0 :(得分:1)

正如其他人所建议的那样,之前 template声明错过了;

看看编译器看到了什么,因此:

g++ -E <yourfile>.cpp | less

答案 1 :(得分:0)

expected ';' before 'template'
expected primary-expression before 'template'

你在第一行之前忘记了一个分号。你能给出上一行吗?

答案 2 :(得分:0)

也许您应该使用C ++编译器g++而不是C编译器gcc

另一种可能性是你的一个标题#define是一个真正邪恶的宏。

答案 3 :(得分:0)

您发布的错误表明问题出在函数void init_module_PyCML()中。因此,您似乎尝试在另一个函数的主体内定义一个函数模板。

例如,尝试编译以下代码会给我带来同样的错误

void f()
{

template < typename T >
void g()
{
};

}

最后一个错误行暗示您可能忘记了}的结束init_module_PyCML()