在 C++ 中,如何让两个类通过向量(map、multimap..)相互链接?
我有两个类,其中一个向量保存第三个类的实例。
基本上我有三类:
- 公司
- 市场
- 股票
公司和市场可以有 0..* 股票,股票必须与 1 个市场和 1 个公司相关联(打印价值等...)
我不知道如何正确地做到这一点。对我来说,问题是当我在市场和公司中创建一个股票向量时,我无法在公司和市场的股票中放置链接,因为当 Equities.h 是第一个时,它不知道在中找到的市场和公司类股票类。如果 Corporation.h 和 Market.h 是第一个,它会说我不知道 Corporation 和 Market 类中的 Equities 类。
为此构建解决方案的最佳代码是什么?当 VisualStudio 看到一个它从未见过的类时,我不能对他说走开吗? (因为定义只是到下一个,所以它没有改变任何东西......:()在C++中,在第一类中不可能有:第二类的链接或向量,在第二类中:链接或向量当我们构建解决方案时,第一个类没有问题吗?
编辑:
这是工作,我写了“类股票;类公司;类市场;”并且它不关心它不知道这些类的事实。对每个人来说都太快了!当我完成我的工作时,我会阅读你的链接:)
I have two class with a vector that holds instances of a third class.
Basically I have three classes:
- Corporation
- Market
- Equities
Corporation and market can have 0..* equities and equities must be linked to 1 market and 1 corporation (to print values, etc...)
I don't know how to do that properly. For me the problem is when I do one vector of equities in market and coporation, I can't put a link in Equities for Corporation and Market because when the Equities.h is the first it don't know Market and Corporation class found in Equities class. And if Corporation.h and Market.h are the first it say me than it don't know the Equities class found in Corporation and Market class.
What is the best code to build the solution for this? I can't say to VisualStudio to go away when he see a class than it never see already? (because the definition is just to the next so it change nothing ... :( ) It's impossible in C++ to have in the first class : a link or a vectorof the second class and in the second class : a link or a vector of the first class without having a problem when we build the solution?
Edit:
It's work, I wrote "class Equities; class Corporation; class Market;" and it didn't care about the fact that it didn't know these classes. Thank to everyone it was so quick! I will read your link when I will finish my job. :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
最简单的方法是在公共标头中转发声明或原型化所有类和类型,在其上放置防护,并包含所有代码文件中的标头。例如:
header.hpp:
body.cpp:
等等。
如果您只使用 Visual Studio,则可以使用
#pragma Once
代替#ifndef
防护。The simplest way is to forward declare or prototype all the classes and types in a common header, place guards on that, and include the header from all your code files. For example:
header.hpp:
body.cpp:
and so on.
If you're using Visual Studio exclusively, you can use
#pragma once
in place of the#ifndef
guards.您正在寻找包含防护和前向声明。因此,您可以将
class Market;
放在尚未定义 Market 的位置。You are looking for include guards and forward declarations. So, you could just put
class Market;
in the place where Market is not defined yet.你要找的是术语
循环依赖
,看这里:http://en。 wikipedia.org/wiki/Circular_dependency因此,您可以将
class Market;
行放在Market
尚未定义的位置。What are u looking for is term
circular dependency
look here: http://en.wikipedia.org/wiki/Circular_dependencySo, you could just put
class Market;
line in place whereMarket
isn't defined yet.怎么样:
How about:
“peachykeen”选项可能是最佳选择,但是,如果您使用继承或对实体进行子类化,另一个选项可能会对您有所帮助:
declarations.h
FinantialApp.h
请注意,工作类,其中每个类作为“基类”,
并且工人阶级互相引用彼此的基类,而不是类本身。
这可以帮助您避免循环/转发的东西,这很难使用。
请注意,有一个应用程序。用作主类和关联类(“权益”)容器的类。
检查“股权”类别是否代表“市场”和“公司”之间的关联。该类不是“市场”和“公司”的一部分,而是“应用程序”的组件。如果您想添加任何类,您可以通过容器来添加,而不是添加到类列表本身。
请注意,还有一个额外的“财务根基类”,它可以帮助您处理与所有类共享的内容。
"peachykeen" option may be the optimal, but, another option, that may help you, in case you use inheritance or subclassing your entities could be:
declarations.h
FinantialApp.h
Note that the working classes, each one of them as a "base class",
and the working classes reference each others base class, not the class itself.
That helps you avoid the circular / forward stuff, that its difficult to use.
Note, that there is an app. class that serves as a container for main classes, and association classes ("equity").
Check that the "equity" class represents an association between a "Market" and a "Corporation". That class is NOT part of the "Market" and a "Corporation", but a component of the "App". If you want to add any class, you do it thru the container, not a to the list of classes themselves.
Note, as an additional, that there is an additional "finantial root base class", that may help you with stuff shared with all classes.