【发布时间】:2013-09-25 07:59:11
【问题描述】:
我必须使用 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>
注释该行解决了错误。
【问题讨论】:
标签: c++ class templates constructor