我首先学习了C#,现在我开始使用C++.据我所知,newC++中的operator 与C#中的operator 不相似.
你能解释一下这个示例代码中内存泄漏的原因吗?
class A { ... };
struct B { ... };
A *object1 = new A();
B object2 = *(new B());
Run Code Online (Sandbox Code Playgroud) 当我尝试编译此代码时,我得到:
52 C:\Dev-Cpp\Projektyyy\strategy\Tiles.h invalid use of undefined type `struct tile_tree_apple'
46 C:\Dev-Cpp\Projektyyy\strategy\Tiles.h forward declaration of `struct tile_tree_apple'
Run Code Online (Sandbox Code Playgroud)
我的代码的一部分:
class tile_tree_apple;
class tile_tree : public tile
{
public:
tile onDestroy() {return *new tile_grass;};
tile tick() {if (rand()%20==0) return *new tile_tree_apple;};
void onCreate() {health=rand()%5+4; type=TILET_TREE;};
};
class tile_tree_apple : public tile
{
public:
tile onDestroy() {return *new tile_grass;};
tile tick() {if (rand()%20==0) return *new tile_tree;};
void onCreate() {health=rand()%5+4; type=TILET_TREE_APPLE;};
tile onUse() {return *new tile_tree;};
};
Run Code Online (Sandbox Code Playgroud)
我真的不知道该怎么做,我搜索了解决方案,但我找不到任何与我的问题相似的东西......实际上,我有更多的课程与父母"瓷砖",这是好的之前... Thanx任何帮助.
编辑:
我决定将所有返回的类型更改为指针以避免内存泄漏,但现在我得到了:
27 C:\Dev-Cpp\Projektyyy\strategy\Tiles.h ISO …Run Code Online (Sandbox Code Playgroud)