0

I receive a warning from the cpp compiler which I can't seem to resolve. I'd like to at least understand what I am doing wrong...

sketch\IRectangle.h:7:20: warning: ISO C++ forbids declaration of 'Rectangle' with no type [-fpermissive]

Rectangle(int,int);

                ^

Header file

#ifndef RECTANGLE_H
#define RECTANGLE_H

class IRectangle
{
public:
  Rectangle(int,int);
  void set_values (int,int);
  int area (void);

private:
  int width, height; 
};

#endif

The Rectangle implementation

#include "IRectangle.h"

IRectangle::Rectangle(int width, int height)
{

}

void IRectangle::set_values (int a,int b)
{
}

int IRectangle::area()
{
  return 0;
}

After googling I ran into this and this thread but I triple checked if the prototypes match, so I really can't figure out what I am doing wrong.

PS: is it OK to prefix interface with 'I' in C++?

Community
  • 1
  • 1
bas
  • 13,550
  • 20
  • 69
  • 146
  • 1
    `Rectangle(int,int);` isn't the same as `IRectangle(int,int);` _"PS: is it OK to prefix interface with 'I' in C++?"_ Yes. But a constructor doesn't make sense for an interface though. – πάντα ῥεῖ Sep 03 '16 at 20:06
  • 1
    *PS: is it OK to prefix interface with 'I' in C++?* The compiler does not care whether you do or not. Personally, I think that if it seems necessary, that probably means your class hierarchy is too complicated. – zwol Sep 03 '16 at 20:08
  • @zwol Right, When I saw the answer I figured that out too. Thx for pointing it out. That was my problem all along... – bas Sep 03 '16 at 20:09
  • 1
    I suspect the `I` in this case stands for 'integer' and not 'interface'. There are no virtual functions that I can see. But when you start adding type prefixes like this you really should be thinking `template`. – Spencer Sep 03 '16 at 20:17

1 Answers1

7

The constructor has to have the same name as the class. If the class is named IRectangle, then the constructor has to be named IRectangle.

Brian Bi
  • 111,498
  • 10
  • 176
  • 312
  • Aha, I guess that also answers the PS, I should NOT prefix interfaces with "I". I get it now thank you! – bas Sep 03 '16 at 20:08
  • 1
    @bas: No, it doesn't; you can call the class `Rectangle` or `IRectangle` or `UrgleBurgle` .. really whatever you like. But the class's constructor declaration must use the name of the class, not some other name. (The constructor actually doesn't have its own name, despite appearances, and despite what this answer suggests.) Finally, there's nothing about your class that anyone would typically call an "interface". – Lightness Races in Orbit Sep 03 '16 at 20:24
  • @LightnessRacesinOrbit I am still not there indeed. I solved the warning by changing the constructor to `IRectangle::IRectangle(int width, int height) {}` and made a similar change in the class (/interface) definition: `IRectangle(int a, int b);`. But is that what you would appreciate if your colleague produced code like that? I am still googling and hope to find an example a proper C++ class : interface example. Maybe you could push me in the right direction? – bas Sep 03 '16 at 20:30
  • @bas: Not without knowing what you're trying to accomplish or what the problem is, no. You have a working class so what is the problem? I still don't see why you think it's an "interface", though, so the name `IRectangle` is a strange choice. – Lightness Races in Orbit Sep 03 '16 at 20:31
  • @LightnessRacesinOrbit, I probably have the wrong mind set (C#/Java/Python). There I'd have a class (e.g. Rectangle) which implements an interface (e.g. IRectangle). So for e.g. for test driven development that would be great so that I can mock the Rectangle class and only test the 'subject under test'. Please note; the Rectangle class is just a silly start for me, trying to get re-acquainted with C++ (it really has been long since I was active in C++, hence the probably stupid questions...) – bas Sep 03 '16 at 20:39
  • @LightnessRacesinOrbit, riiiiight, this link refreshed a damn lot ! :). http://stackoverflow.com/questions/318064/how-do-you-declare-an-interface-in-c. I will find my way back into C++, thx for the help – bas Sep 03 '16 at 21:04
  • @bas: There's nothing wrong with making an interface, but that's not what you've done here! This would be the case even if your program were written in C#, Java or Python. – Lightness Races in Orbit Sep 04 '16 at 00:11