gpt4 book ai didi

c++ - ‘<’ token 错误之前的 GCC 预期模板名称

转载 作者:行者123 更新时间:2023-12-04 18:54:51 26 4
gpt4 key购买 nike

关闭。这个问题需要debugging details .它目前不接受答案。












编辑问题以包含 desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem .这将帮助其他人回答问题。


7年前关闭。







Improve this question




我浏览了其他类似的主题,但没有找到我的问题的答案......

下面的代码说明了这种情况。一个基类和一个派生类:

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
};

}

派生.hpp
namespace test {

class Derived : public Base<int> {
public:
Derived();
virtual ~Derived();
};

} /* namespace aeirtuaccess */

派生.cpp
#include "Derived.hpp"

namespace test {

Derived::Derived() {
// TODO Auto-generated constructor stub

};

Derived::~Derived() {
// TODO Auto-generated destructor stub
};

}

当我用 Coliru - see it in action here 编译这段代码时,它工作正常,但是当我使用 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指编译这个文件的时候。

关于c++ - ‘<’ token 错误之前的 GCC 预期模板名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30742260/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com