0

Link to my last topic: C++ class forward declaration

Now main thing:

I decided to change all returned types to pointers to avoid memory leaks, but now I got:

27 C:\Dev-Cpp\Projektyyy\strategy\Tiles.h ISO C++ forbids declaration of `tile' with no type 
27 C:\Dev-Cpp\Projektyyy\strategy\Tiles.h expected `;' before "tick"

Its only in base class, everything else is ok... Every function in tile class which return *tile has this error...

Some code:

class tile
{
      public:
          double health;
          tile_type type;
          *tile takeDamage(int ammount) {return this;};
          *tile onDestroy() {return this;};
          *tile onUse() {return this;};
          *tile tick() {return this};
          virtual void onCreate() {};
};
Community
  • 1
  • 1
noisy cat
  • 2,865
  • 5
  • 33
  • 51
  • 3
    You don't "avoid memory leaks" by "changing all returned types to pointers". You avoid them by writing correct code. – Kerrek SB Feb 02 '12 at 20:45
  • @Kerrek SB I need to have pointers to have correct code :) – noisy cat Feb 02 '12 at 20:46
  • @kittyPL: That's debatable and impossible to tell from your code. Modern C++ usually contains hardly any naked pointers or `new` expressions at all, and `delete` never, so I wouldn't bank on it that you're up to a good solution... – Kerrek SB Feb 02 '12 at 20:47
  • @kittyPL : You certainly do not! – ildjarn Feb 02 '12 at 20:48
  • @gamernb when I read your comment I thought "pff, im not so stupid...", but then looked in my code and BANG! :D Double facepalm for me, would be a vote up and accept for you :) – noisy cat Feb 02 '12 at 20:48
  • 1
    @kittyPL I highly doubt that, but even if that is true, you using pointers will most likely lead [to more memory leaks](http://stackoverflow.com/questions/8839943/why-is-it-a-memory-leak-what-could-i-catch-if-i-shall-use-such-things-in-c/8840302#8840302), not avoid them. – R. Martinho Fernandes Feb 02 '12 at 20:49
  • @ildjarn if you mean that I have inline functions, you are right, but it's only a base class (Im going to make it virtual) and subclasses have separate files for headers and definitions :) – noisy cat Feb 02 '12 at 20:50
  • 1
    @kittyPL : No, I mean that you certainly do not need to have pointers to have correct code. (And _most_ memory leaks are caused by _using_ pointers, not by not using them...) – ildjarn Feb 02 '12 at 20:52
  • @ildjarn Well, read the topic in link and try to think a better way to change class... – noisy cat Feb 02 '12 at 21:53
  • @kittyPL : Armen's answer (which _you_ selected as the correct answer) already shows you the way to change the class -- notice that he doesn't use any pointers! – ildjarn Feb 02 '12 at 21:56
  • @ildjarn sorry, I dont understand. I have 2 separate classes in his answer. Im going to have tile foo[X][Y] tab, and I have tile children classes. I want to change tile_tree into tile_tree_apple (when apples grow up)... How to do that? I decided to use return value to assign new object to my map tab. Do you know any better way? – noisy cat Feb 02 '12 at 22:01
  • @ildjarn well, answer please, Im not sure about my decision. Still can change everything... :D – noisy cat Feb 02 '12 at 22:21
  • @kittyPL : I don't understand your last question at all, so I can't answer it, sorry. I'm only arguing against use of pointers. – ildjarn Feb 02 '12 at 22:22

2 Answers2

5

You're missing a semi-colon on the return for tick, and the * goes after the type when declaring pointers:

tile* tick() {return this;};
R. Martinho Fernandes
  • 228,013
  • 71
  • 433
  • 510
wocoburguesa
  • 738
  • 1
  • 5
  • 7
2
class tile
{
      public:
          double health;
          tile* takeDamage(int ammount) {return this;}
          tile* onDestroy() {return this;}
          tile* onUse() {return this;}
          tile* tick() {return this;}
          virtual void onCreate() {}
};

Here you go. I removed tile_type since I don't know what that is. Probably an enum. Apart from that you had lots of issues with semi-colons.

Robᵩ
  • 163,533
  • 20
  • 239
  • 308
Sid
  • 7,511
  • 2
  • 28
  • 41