【发布时间】:2022-01-09 16:57:44
【问题描述】:
拥有这个基类:
Core.hpp:
#ifndef C3_CORE_HPP
#define C3_CORE_HPP
#include <c3/utils/Str.hpp>
#include <c3/utils/Vec.hpp>
#include <c3/school/Student.hpp>
class Core {
public:
Core() = default;
explicit Core(std::istream&in);
virtual ~Core();
virtual double grade() const;
const Str &getName() const;
double getMidterm() const;
double getFinal() const;
const Vec<double> &getHomeworks() const;
protected:
Vec<double> homeworks;
virtual std::istream &read(std::istream &in);
virtual Core *clone() const;
std::istream &read_common(std::istream &in);
private:
Str name;
double midterm{}, final{};
friend class Student;
};
std::istream &read_hw(std::istream &in, Vec<double> &hws);
#endif //C3_CORE_HP
和Grad.hpp:
#ifndef C3_GRAD_HPP
#define C3_GRAD_HPP
#include <c3/school/Core.hpp>
class Grad: public Core {
public:
Grad() = default;
explicit Grad(std::istream &in);
std::istream &read(std::istream &in) override;
double grade() const override;
protected:
Grad *clone() const override;
private:
double thesis{};
};
#endif //C3_GRAD_HPP
(代码根据书accelerated C++ by Andrew Koenig创建)
现在这让我出错了:
In file included from /home/shepherd/Desktop/cpp/cpp0book/c3/./c3/school/Student.hpp:8,
from /home/shepherd/Desktop/cpp/cpp0book/c3/./c3/school/Core.hpp:10,
from /home/shepherd/Desktop/cpp/cpp0book/c3/c3/main.cpp:4:
/home/shepherd/Desktop/cpp/cpp0book/c3/./c3/school/Grad.hpp:10:25: error: expected class-name before ‘{’ token
10 | class Grad: public Core {
| ^
/home/shepherd/Desktop/cpp/cpp0book/c3/./c3/school/Grad.hpp:15:19: error: ‘std::istream& Grad::read(std::istream&)’ marked ‘override’, but does not override
15 | std::istream &read(std::istream &in) override;
| ^~~~
/home/shepherd/Desktop/cpp/cpp0book/c3/./c3/school/Grad.hpp:16:12: error: ‘double Grad::grade() const’ marked ‘override’, but does not override
16 | double grade() const override;
| ^~~~~
/home/shepherd/Desktop/cpp/cpp0book/c3/./c3/school/Grad.hpp:19:11: error: ‘Grad* Grad::clone() const’ marked ‘override’, but does not override
19 | Grad *clone() const override;
| ^~~~~
In file included from /home/shepherd/Desktop/cpp/cpp0book/c3/./c3/school/Core.hpp:10,
from /home/shepherd/Desktop/cpp/cpp0book/c3/c3/main.cpp:4:
/home/shepherd/Desktop/cpp/cpp0book/c3/./c3/school/Student.hpp:26:5: error: ‘Core’ does not name a type
26 | Core *cp{};
| ^~~~
gmake[2]: *** [CMakeFiles/c3.dir/build.make:76: CMakeFiles/c3.dir/c3/main.cpp.o] Error 1
gmake[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/c3.dir/all] Error 2
gmake: *** [Makefile:91: all] Error 2
第一个错误是
error: expected class-name before ‘{’ token
10 | class Grad: public Core {
在我看来,即使包含 Core 类,编译器也无法识别。那么为什么编译器不能识别我的基类呢?
使用这个目录结构: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p1204r0.html
【问题讨论】:
-
试试
class Grad和:之间的空格 -
@infinitezero 没有帮助
-
@milanHrabos 尝试将 all
<c3/school/Core.hpp>替换为"c3/school/Core.hpp"。那是使用<>代替包含使用 "" 。对 每个 自定义包含的标题执行此操作。只需在所有自定义包含中将周围的<>替换为""。 -
@AnoopRana 我正在使用 open-std.org/jtc1/sc22/wg21/docs/papers/2018/p1204r0.html ,这不鼓励使用
""包含 -
@milanHrabos 您可以尝试在 Grad.hpp 中将
#include <c3/school/Core.hpp>替换为#include "c3/school/Core.hpp",看看是否有效。这是包含文件时的常见问题,这就是我建议它的原因。样式指南就是:指南。您不一定非要遵循它们,尤其是当它们导致某种错误时。
标签: c++ inheritance g++ include