简体   繁体   English

如何在另一个类中使用结构定义

[英]how to use struct define in another class

I have class which looks like 我上课看起来像

struct Games
{ char Name[80];
char Rating;
bool  Played;
long NumberOfKills;
} Unreal, Blizzard[3];

typedef Games* Gamesptr;

int _tmain(int argc, _TCHAR* argv[])
{
    int x;
    Gamesptr  ptr =  new Games;
    return 0;
}

Now i want to use the struct defined above in another class like below 现在我想在下面的另一个类中使用上面定义的结构

structTest::structTest(void)
{
}

structTest::~structTest(void)
{
}
void structTest::test(void)
{
    // goes here        
    Games *structptr = new Games;// This not working ....
}

how do i do this ..when I tried it throws me ' no appropriate default constructor available' error... 我该怎么做..当我尝试它时会抛出“没有合适的默认构造函数可用”错误...

It looks like your struct Games is declared in one file, and structTest in another file. 看起来您的struct Games是在一个文件中声明的,而structTest在另一个文件中声明的。 Is that right? 那正确吗?

Your structTest class needs to see the definition of the Games struct in order to work. 为了工作,您的structTest类需要查看Games结构的定义。

Put the Games struct in its own file (maybe call it Games.h ). Games结构放入其自己的文件(可能称为Games.h )。 #include this in the file that includes your main function and in the file that defines the structTest class. #include在包含您的main功能的文件和定义structTest类的文件中。

Note that Unreal and Blizzard[3] still need to be in the file that includes your main function, not in the header file, which should contain only the struct definition (and optionally the typedef if you plan to use that in other modules). 注意, UnrealBlizzard[3]仍需要包含main函数的文件中,而不是头文件中,头文件应仅包含struct定义(如果打算在其他模块中使用,则可以包含typedef )。

I think you should improve your formatting, because even at this moment it has already become hard to read. 我认为您应该改善格式,因为即使在这一刻, 它已经变得难以阅读。 I can hardly ever think of what might happen at the following stages of your project. 我几乎想不出在项目的以下阶段会发生什么。

Now - directly to the question. 现在-直接问问题。

I think you want to have the following: 我想您要具备以下条件:

struct Games {
   // whatever
   bool value;
};

struct GamesHolder {
   Games games;
};

Now, if the Games isn't just a raw data holder, but stands for some kind of the abstraction or holds a too much data to be placed on the stack, you might want to use the following pattern: 现在,如果Games不只是原始数据持有人,而是代表某种抽象或者持有太多数据以至于不能放在堆栈上, 那么您可能想要使用以下模式:

struct GamesHolder {
   Games* games_pointer;
};

The initialization now may be implemented the following way: 现在可以通过以下方式实现初始化:

GamesHolder games_holder;
games_holder.games_pointer = new Games(...whatever);

// If the memory is not cleared when the 'games_holder'
// leaves the scope - bang - a memory leak.

As long as sometimes manual memory management becomes a pain in the *ss, you also might want to start using shared pointers: 只要有时手动内存管理在* ss中变得很痛苦,您可能还想开始使用共享指针:

struct GamesHolder {
   std::shared_ptr<Games> games_pointer;
}

GamesHolder games_holder;
games_holder.games_pointer.reset(new Games(...));

// No leak if the 'holder' leaves the scope - memory
// is being automatically disposed.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM