C++如何从没有标题的不同文件中扩展类

发布于 2025-01-24 12:57:07 字数 555 浏览 2 评论 0原文

我正在尝试执行基本的多态性计划,并且遇到了一些问题。 我有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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

似狗非友 2025-01-31 12:57:07

我不应该使用标题文件,因为这是用于指定的分配。

该代码将无法如图所示。 dollar.cpp需要知道货币类的外观才能使用它。

理想解决方案是在翻译单元之间使用标头文件共享声明,使用.cpp文件来实现定义,例如

#ifndef CurrencyH
#define CurrencyH

class Currency{
    /* code */
};

#endif

em

#include <iostream>
#include "Currency.h"
using namespace std;

// implement Currency's methods as needed...

货币

#include <iostream>
#include "Currency.h"
using namespace std;

class Dollar : protected Currency{
    /* code /*
};

。 > Dollar.cpp 直接,并确保它Currency.cpp中的类声明完全匹配,以避免任何 odr 违规行为,例如:

currency.cpp:

#include <iostream>
using namespace std;

class Currency{
    /* code */
};

// implement Currency's methods as needed...

dollar.cpp:

#include <iostream>
using namespace std;

class Currency{
    /* code */
};

class Dollar : protected Currency{
    /* code /*
};

I’m not supposed to use header files as this is for an assignment that specifies so.

The code will not work as shown. Dollar.cpp needs to know what the Currency 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:

#ifndef CurrencyH
#define CurrencyH

class Currency{
    /* code */
};

#endif

Currency.cpp:

#include <iostream>
#include "Currency.h"
using namespace std;

// implement Currency's methods as needed...

Dollar.cpp:

#include <iostream>
#include "Currency.h"
using namespace std;

class Dollar : protected Currency{
    /* code /*
};

But, since that is not an option for you, you have no choice but to copy the entire Currency class declaration into Dollar.cpp directly, and make sure it exactly matches the class declaration in Currency.cpp to avoid any ODR violations, eg:

Currency.cpp:

#include <iostream>
using namespace std;

class Currency{
    /* code */
};

// implement Currency's methods as needed...

Dollar.cpp:

#include <iostream>
using namespace std;

class Currency{
    /* code */
};

class Dollar : protected Currency{
    /* code /*
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文