C++错误:"令牌之前的预期构造函数、析构函数或类型转换'<'
C++ error: "expected constructor, destructor, or type conversion before '<' token
我必须使用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.h
中RWGVector<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();
// ^^^
// ...
};
相关文章:
- LINUX 操作系统上的错误:令牌之前预期的构造函数、析构函数或类型转换'('?
- 错误 c++ 在令牌之前'('预期的构造函数、析构函数或类型转换
- 错误:在“(”标记之前进行预期的构造函数、析构函数或类型转换.即使我有一个构造函数
- 获取错误:在“(”标记之前进行预期的构造函数、析构函数或类型转换
- 如何销毁通过"放置新"构造的无析构函数类型
- 错误:令牌之前的预期构造函数、析构函数或类型转换'*'
- 不同的析构函数类型
- 字符串常量之前的预期构造函数、析构函数或类型转换
- 需要构造函数/析构函数/类型转换错误
- C++模板错误:'<'令牌之前的预期构造函数、析构函数或类型转换
- 错误:在非常简单的示例中,预期的构造函数、析构函数或类型转换
- 错误:'<'令牌之前的预期构造函数、析构函数、类型转换
- 令牌之前的预期构造函数、析构函数或类型转换'*'
- 错误:标记之前的预期构造函数、析构函数或类型转换?
- 错误:在“(”标记之前进行预期的构造函数、析构函数或类型转换
- C++ 错误:令牌之前的预期构造函数、析构函数或类型转换'*'
- C++ 错误:在“(”标记之前进行预期的构造函数、析构函数或类型转换
- 预期的构造函数、析构函数或类型转换
- 不稳定的声明行为:Rational.h:25:错误:在"&"标记之前预期的构造函数、析构函数或类型转换
- 编译时错误:期望的构造函数、析构函数或转换