在 C++ 中,如何让两个类通过向量(map、multimap..)相互链接?

发布于 2024-11-14 10:31:47 字数 585 浏览 3 评论 0原文

我有两个类,其中一个向量保存第三个类的实例。

基本上我有三类:

  • 公司
  • 市场
  • 股票

公司和市场可以有 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 技术交流群。

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

发布评论

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

评论(5

隐诗 2024-11-21 10:31:47

最简单的方法是在公共标头中转发声明或原型化所有类和类型,在其上放置防护,并包含所有代码文件中的标头。例如:

header.hpp:

#ifndef COMMON_HPP
#define COMMON_HPP

class Corporation;
class Market;
class Equities;

typedef std::shared_ptr<Corporation> CorpRef;

typedef std::list<CorpRef> CorpList;

#endif /*COMMON_HPP*/

body.cpp:

#include header.hpp

class Corporation
{
    static CorpRef Create();
};

等等。

如果您只使用 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:

#ifndef COMMON_HPP
#define COMMON_HPP

class Corporation;
class Market;
class Equities;

typedef std::shared_ptr<Corporation> CorpRef;

typedef std::list<CorpRef> CorpList;

#endif /*COMMON_HPP*/

body.cpp:

#include header.hpp

class Corporation
{
    static CorpRef Create();
};

and so on.

If you're using Visual Studio exclusively, you can use #pragma once in place of the #ifndef guards.

锦爱 2024-11-21 10:31:47

您正在寻找包含防护前向声明。因此,您可以将 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.

原来是傀儡 2024-11-21 10:31:47

你要找的是术语循环依赖,看这里: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_dependency

So, you could just put class Market; line in place where Market isn't defined yet.

无法言说的痛 2024-11-21 10:31:47

怎么样:

struct Corporation {
     vector <class Equity *> equities;
}

struct Market {
     vector <class Equity *> equities;
};

struct Equity {
     Market * market;
     Corporation * corp;
};

How about:

struct Corporation {
     vector <class Equity *> equities;
}

struct Market {
     vector <class Equity *> equities;
};

struct Equity {
     Market * market;
     Corporation * corp;
};
初懵 2024-11-21 10:31:47

“peachykeen”选项可能是最佳选择,但是,如果您使用继承或对实体进行子类化,另一个选项可能会对您有所帮助:


declarations.h


// these classes doesn't have references to each others

public class FinantialBaseClass
{
  // ...
}

public class CorporationBaseClass: FinantialBaseClass
{
  // ...
}

public class MarketBaseClass: FinantialBaseClass
{
  // ...
}

public class EquitiesBaseClass: FinantialBaseClass
{
  // ...
}


FinantialApp.h


// these classes have references, but, to parent classes,
// not each others

public class CorporationClass: CorporationBaseClass
{
  vector<EquitiesBaseClass> Equities;
}

public class MarketClass: MarketBaseClass
{
  vector<EquitiesBaseClass> Equities;
}

public class EquitiesClass: EquitiesBaseClass
{
  public CorporationBaseClass* Corporation;

  public MarketBaseClass* Market;

}

public class MyFinantialAppClass
{
  protected vector<CorporationBaseClass> Corporations;

  protected vector<MarketBaseClass> Markets;

  protected vector<EquitiesBaseClass> Equities;

  public CorporationBaseClass* addCorporation() { ... }

  public MarketBaseClass* addMarket() { ... }

  public EquitiesBaseClass* addEquity(CorporationBaseClass, MarketBaseClass)
   { ... }

}     

请注意,工作类,其中每个类作为“基类”,
并且工人阶级互相引用彼此的基类,而不是类本身。
这可以帮助您避免循环/转发的东西,这很难使用。

请注意,有一个应用程序。用作主类和关联类(“权益”)容器的类。

检查“股权”类别是否代表“市场”和“公司”之间的关联。该类不是“市场”和“公司”的一部分,而是“应用程序”的组件。如果您想添加任何类,您可以通过容器来添加,而不是添加到类列表本身。

请注意,还有一个额外的“财务根基类”,它可以帮助您处理与所有类共享的内容。

"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


// these classes doesn't have references to each others

public class FinantialBaseClass
{
  // ...
}

public class CorporationBaseClass: FinantialBaseClass
{
  // ...
}

public class MarketBaseClass: FinantialBaseClass
{
  // ...
}

public class EquitiesBaseClass: FinantialBaseClass
{
  // ...
}


FinantialApp.h


// these classes have references, but, to parent classes,
// not each others

public class CorporationClass: CorporationBaseClass
{
  vector<EquitiesBaseClass> Equities;
}

public class MarketClass: MarketBaseClass
{
  vector<EquitiesBaseClass> Equities;
}

public class EquitiesClass: EquitiesBaseClass
{
  public CorporationBaseClass* Corporation;

  public MarketBaseClass* Market;

}

public class MyFinantialAppClass
{
  protected vector<CorporationBaseClass> Corporations;

  protected vector<MarketBaseClass> Markets;

  protected vector<EquitiesBaseClass> Equities;

  public CorporationBaseClass* addCorporation() { ... }

  public MarketBaseClass* addMarket() { ... }

  public EquitiesBaseClass* addEquity(CorporationBaseClass, MarketBaseClass)
   { ... }

}     

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.

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