4

I need to create a static object inside a class definition. It is possible in Java, but in C++ I get an error:

../PlaceID.h:9:43: error: invalid use of incomplete type ‘class
PlaceID’ ../PlaceID.h:3:7: error: forward declaration of ‘class
PlaceID’ ../PlaceID.h:9:43: error: invalid in-class initialization of static data 

My class looks like this:

#include <string>

class PlaceID {

public:

    inline PlaceID(const std::string placeName):mPlaceName(placeName) {}

    const static PlaceID OUTSIDE = PlaceID("");

private:
    std::string mPlaceName;
};

Is it possible to make an object of a class inside this class? What are prerequisites that it must hold?

Benjamin
  • 3,217
  • 2
  • 27
  • 42

1 Answers1

12

You can't define the member variable because the class isn't fully defined yet. You have to do like this instead:

class PlaceID {

public:

    inline PlaceID(const std::string placeName):mPlaceName(placeName) {}

    const static PlaceID OUTSIDE;

private:
    std::string mPlaceName;
};

const PlaceID PlaceID::OUTSIDE = PlaceID("");
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • I was thinking about this, but my concern was about memory. But now I realized, that memory used in both options are actually equal. BTW, in your answer should be "const static" but you're right anyway. – Benjamin Jul 25 '12 at 10:06
  • 3
    @Benjamin The `static` keyword is only needed when declaring the variable inside the class, and not when defining it outside. – Some programmer dude Jul 25 '12 at 10:09
  • Getting a compiler warning for multiple declarations of the static const in classes where I include this class. LNK4006: already defined in x.obj – MNCODE Aug 03 '18 at 08:23
  • @MNCODE Definitions must happen only *once*. Move the definition to a single source file. – Some programmer dude Aug 03 '18 at 08:24
  • Thank you I defined the static const in the header file after class definition. It works in the source file. – MNCODE Aug 03 '18 at 08:26
  • Can you please elaborate why `static` keyword is necessary when declaring the variable inside the class? I know that without `static`, the declaring does not work, but why a `static` will eliminate the 'incomplete type is not allowed' error? – xin Jun 20 '19 at 03:10
  • @feng The `static` declaration is irrelevant for the problem being asked about here. It's merely to make the variable a "class" member variable instead of an "instance" member variable. – Some programmer dude Jun 20 '19 at 04:38
  • I know they are irrelevant. I just don't understand the syntax and googled and found this post. Why makeing a variable a 'class' member will pass the compiler 'incomplete type' check before the class' declaration is finished? What happeds in memory? Thanks! – xin Jun 20 '19 at 05:07
  • @feng A class starts with `class some_name {` and ends with the closing `};`. Before the closing `};` there's simply no full definition of the class, the compiler doesn't know the size of the class, how large the objects will be. Therefore it's impossible to create object instances of the class before the closing `};`. It doesn't matter if it's a class-member (declared using the `static` keyword) or a "normal" (non-static) member variable. – Some programmer dude Jun 20 '19 at 18:14