C++如何从没有标题的不同文件中扩展类
我正在尝试执行基本的多态性计划,并且遇到了一些问题。 我有2个文件,currency.cpp and dollar.cpp
。
#include <iostream>
using namespace std;
class Currency{
/* code */
};
currency.cpp看起来像这样的东西
#include <iostream>
using namespace std;
class Dollar : protected Currency{
/* code /*
};
当我尝试在文件之间构建路径(Mac上的命令shift b)时, 作为班级(以Dollar.cpp为单位)。我不应该使用标题文件,因为这是用于指定的分配。我该怎么办?
我在2020 M1 Mac上使用VS代码,如果与其有任何关系
编辑: 感谢您的所有反馈。从我可以告诉我在这里问的内容不可能。此作业适用于接受多种语言的数据结构和算法类,因此教师也许在C ++中生锈。我只是要使用标题文件,教授只需要处理它。
I’m trying to do a basic polymorphism program and am having some problems.
I have 2 files, Currency.cpp and Dollar.cpp
Currency.cpp looks something like this
#include <iostream>
using namespace std;
class Currency{
/* code */
};
Dollar.cpp looks something like this
#include <iostream>
using namespace std;
class Dollar : protected Currency{
/* code /*
};
When I try to build paths between the files (command shift b on mac) it tells me that Currency is not recognized as a class (in Dollar.cpp that is). I’m not supposed to use header files as this is for an assignment that specifies so. What am I supposed to do?
I use vs code on a 2020 m1 Mac if that has anything to do with it
Edit:
Thanks for all the feedback. From what I can tell what I'm asking here isn't possible. This assignment is for a data structures and algorithms class that accepts multiple languages, so maybe the teacher is rusty in c++ or something. I'm just gonna use header files and the professor will just have to deal with it lol.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该代码将无法如图所示。
dollar.cpp
需要知道货币
类的外观才能使用它。理想解决方案是在翻译单元之间使用标头文件共享声明,使用
.cpp
文件来实现定义,例如:
em
货币
。 > Dollar.cpp 直接,并确保它与
Currency.cpp
中的类声明完全匹配,以避免任何 odr 违规行为,例如:currency.cpp:
dollar.cpp:
The code will not work as shown.
Dollar.cpp
needs to know what theCurrency
class looks like in order to use it.The ideal solution is to use a header file to share declarations across translation units, use the
.cpp
files to implement definitions, eg:Currency.h:
Currency.cpp:
Dollar.cpp:
But, since that is not an option for you, you have no choice but to copy the entire
Currency
class declaration intoDollar.cpp
directly, and make sure it exactly matches the class declaration inCurrency.cpp
to avoid any ODR violations, eg:Currency.cpp:
Dollar.cpp: