编译器无法在 C++ 中识别我的 class
compiler cannot recognize my class in c++
有这个基础class:
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
class 即使包含在内。那么为什么编译器不能识别我的基础class?
使用这个目录结构:
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p1204r0.html
在 git 存储库中,文件 main.cpp
包括 Core.hpp
,其中包括 Student.hpp
以获得朋友 class 学生的定义。在文件 Student.hpp
中包含 header Grad.hpp
,它本身包含 Core.hpp
。现在,由于 Core.hpp
在包含 Student.hpp
之前定义了包含防护 C3_CORE_HPP
,当 Grad.hpp
包含时,预处理器会跳过 Core.hpp
的内容。这就是编译器不知道 class Core
的原因,而 class Grad
已定义。
要解决这个问题,您可以在Core.hpp
中预定义class Student
并省略包含:
Core.hpp:
#ifndef C3_CORE_HPP
#define C3_CORE_HPP
#include <c3/utils/Str.hpp>
#include <c3/utils/Vec.hpp>
class Student;
class Core {
// [...]
friend class Student;
};
std::istream &read_hw(std::istream &in, Vec<double> &hws);
#endif //C3_CORE_HP
正如我在评论中所说,问题是由于循环依赖。特别是,您的
Student.hpp 包括 --> Grad.hpp 又包括 --> Core.hpp 最后包括 --> Student.hpp
因此,正如您从上面看到的,您最终到达了起点,即 Student.cpp。这就是为什么它被称为循环依赖。
要解决,只需从Core.hpp中删除#include <c3/school/Student.hpp>
。这是因为对于朋友声明 friend class Student
,你不需要转发声明或包含Student
class.
因此 modified/correct Core.hpp 文件如下所示:
#ifndef C3_CORE_HPP
#define C3_CORE_HPP
#include <c3/utils/Str.hpp>
#include <c3/utils/Vec.hpp>
//note i have removed the include header from here
class Core {
//other members here as before
private:
Str name;
double midterm{}, final{};
friend class Student;//THIS WORKS WITHOUT INCLUDING Student.hpp
};
std::istream &read_hw(std::istream &in, Vec<double> &hws);
#endif //C3_CORE_HPP
有这个基础class:
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
class 即使包含在内。那么为什么编译器不能识别我的基础class?
使用这个目录结构: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p1204r0.html
在 git 存储库中,文件 main.cpp
包括 Core.hpp
,其中包括 Student.hpp
以获得朋友 class 学生的定义。在文件 Student.hpp
中包含 header Grad.hpp
,它本身包含 Core.hpp
。现在,由于 Core.hpp
在包含 Student.hpp
之前定义了包含防护 C3_CORE_HPP
,当 Grad.hpp
包含时,预处理器会跳过 Core.hpp
的内容。这就是编译器不知道 class Core
的原因,而 class Grad
已定义。
要解决这个问题,您可以在Core.hpp
中预定义class Student
并省略包含:
Core.hpp:
#ifndef C3_CORE_HPP
#define C3_CORE_HPP
#include <c3/utils/Str.hpp>
#include <c3/utils/Vec.hpp>
class Student;
class Core {
// [...]
friend class Student;
};
std::istream &read_hw(std::istream &in, Vec<double> &hws);
#endif //C3_CORE_HP
正如我在评论中所说,问题是由于循环依赖。特别是,您的
Student.hpp 包括 --> Grad.hpp 又包括 --> Core.hpp 最后包括 --> Student.hpp
因此,正如您从上面看到的,您最终到达了起点,即 Student.cpp。这就是为什么它被称为循环依赖。
要解决,只需从Core.hpp中删除#include <c3/school/Student.hpp>
。这是因为对于朋友声明 friend class Student
,你不需要转发声明或包含Student
class.
因此 modified/correct Core.hpp 文件如下所示:
#ifndef C3_CORE_HPP
#define C3_CORE_HPP
#include <c3/utils/Str.hpp>
#include <c3/utils/Vec.hpp>
//note i have removed the include header from here
class Core {
//other members here as before
private:
Str name;
double midterm{}, final{};
friend class Student;//THIS WORKS WITHOUT INCLUDING Student.hpp
};
std::istream &read_hw(std::istream &in, Vec<double> &hws);
#endif //C3_CORE_HPP