GCC期望模板名在' < '标记错误之前
GCC expected template-name before ‘<’ token error
我已经浏览了其他类似的主题,但没有找到我的问题的答案…
下面的代码说明了这种情况。基类和派生类:
Base.hpp
namespace test {
template<class T>
class Base {
public:
Base();
virtual ~Base();
};
}
Base.cpp
#include "Base.hpp"
namespace test {
template<class T>
Base<T>::Base() {
// TODO Auto-generated constructor stub
};
template<class T>
Base<T>::~Base() {
// TODO Auto-generated destructor stub
};
}
Derived.hpp
namespace test {
class Derived : public Base<int> {
public:
Derived();
virtual ~Derived();
};
} /* namespace aeirtuaccess */
Derived.cpp
#include "Derived.hpp"
namespace test {
Derived::Derived() {
// TODO Auto-generated constructor stub
};
Derived::~Derived() {
// TODO Auto-generated destructor stub
};
}
当我用Coliru编译这段代码时,它工作得很好,但是当我使用g++进入我的Ubuntu环境时,我有以下错误:
>g++ Base.cpp Derived.cppIn file included from Derived.cpp:2:0:
Derived.hpp:3:28: error: expected template-name before ‘<’ token
class Derived : public Base<int> {
^
Derived.hpp:3:28: error: expected ‘{’ before ‘<’ token
Derived.hpp:3:28: error: expected unqualified-id before ‘<’ token
Derived.cpp:6:18: error: invalid use of incomplete type ‘class test::Derived’
Derived::Derived() {
^
In file included from Derived.cpp:2:0:
Derived.hpp:3:7: error: forward declaration of ‘class test::Derived’
class Derived : public Base<int> {
^
Derived.cpp:11:19: error: invalid use of incomplete type ‘class test::Derived’
Derived::~Derived() {
^
In file included from Derived.cpp:2:0:
Derived.hpp:3:7: error: forward declaration of ‘class test::Derived’
class Derived : public Base<int> {
编译器之间有什么区别吗?我应该使用特定的g++标志或版本吗?在我的Ubuntu环境中,我需要做些什么来修复这个问题吗?
在Derived.hpp
中,您需要添加:
#include "Base.hpp"
在顶部。否则,编译器在编译该文件时不知道Base
指的是什么。
相关文章:
- 提升精神 x3 错误处理程序与期望
- 我一直得到一个期望;错误,我不确定为什么
- 使用 gtest EXPECT_CALL 时竞争条件段错误,而另一个期望是执行相同的方法
- Variadic模板:候选人期望1个参数,0提供(扣除错误)
- 我有 3 个错误:期望一个")",期望一个表达式,long 类型的参数与 U32 类型的参数不兼容
- 错误:“;期望的类名”;
- 错误C2057:期望的常量表达式
- 错误:')'令牌强制转换问题之前期望的主表达式
- Boost Python运行时错误,将派生类型的对象从Python传递给c++函数,期望将shared_ptr传递给基类
- 语法错误,意外的标记,期望文件结束
- 错误:当通过P2P通道连接两个NodeContainers时,期望主表达式在ns3中的const之前
- 错误:':'令牌之前期望的初始化项
- c++ -错误:在'{'标记之前期望的类名
- 错误:期望声明
- 试图为继承类的子类编写构造函数,猜测语法,期望的主表达式错误
- if语句中的函数:期望的错误表达式
- 编译错误:solution.c:20:5: Error:期望的标识符或'('在' {' token{之前
- 编译时错误:期望的构造函数、析构函数或转换
- File.cpp:148:错误:期望的主表达式在`.不同的符号
- 错误:期望的类名在“{”之前