C++错误:"令牌之前的预期构造函数、析构函数或类型转换'<'

C++ error: "expected constructor, destructor, or type conversion before '<' token

本文关键字:析构函数 类型转换 构造函数 lt 错误 令牌 C++      更新时间:2023-10-16

我必须使用g++在Linux环境中为Windows构建一个C++库。构建时出现此错误:

RWGVector.cpp:5: error: g++ error: "expected constructor, destructor, 
or type conversion before '<' token

在 Visual Studio 2010 中构建它不会返回任何错误。 我正在使用 C++11(又名 c++0x(标准构建它。

我有两个文件,一个带有模板类声明(RWGVector.h(,另一个带有构造函数(RWGVector.cpp(。 我只保留了每个文件的基本部分,对错误负责。

RWGVector.h:

#ifndef _RWGVECTOR_H
#define _RWGVECTOR_H
#include <vector>
#include <rw/generic.h>
template<typename V> class RWGVector
{
    public:
        RWGVector<V>();
    private:
        std::vector<V> vector_;
};
#endif

RWGVector.cpp:

#include "RWGVector.h"
template<typename V>
RWGVector<V>::RWGVector() : vector_()       //<--- Line 5
{
}

导致此错误的原因是什么? 我该如何解决?

溶液:

删除RWGVector<V>();中的<V>

在类中,构造函数不需要模板参数的声明。因为当您指定类时,参数已符合

虽然我有一个其他问题,更具体地针对我的情况,因为包含文件,其中包含以下行:

#define RWGVector(Type) RWTValVector<Type>

注释该行解决了错误。

RWGVector.hRWGVector<V>();中删除<V>

#ifndef _RWGVECTOR_H
#define _RWGVECTOR_H
#include <vector>
#include <rw/generic.h>
template<typename V> class RWGVector
{
    public:
        RWGVector(); //  RWGVector<V> is wrong;
    private:
        std::vector<V> vector_;
};
#endif

class构造函数不需要模板参数的声明。因为当您指定class时,参数已符合

你在这里有一个额外的<V>

只需在您的RWGVector.h中做:

template<typename V> class RWGVector
{
public:
    RWGVector();
    //      ^^^
// ...
};
相关文章: