3

I would like to know how or if I can make my public enum payloadTypes accessible to my private method threshold() in my following class:

class A {

private: 
    unsigned int  threshold(payloadTypes kindOfPayload,int x,int y);

public:
 enum payloadTypes {
  INVALID =-1,
  TEST=0,
  THRESHOLDS,
  RX,
 };

}

I am getting this error if I do as above, and I don't want to change the scope of my enum to private

error: 'payloadTypes ' has not been declared

unsigned int threshold(payloadTypes kindOfPayload,int x,int y);

ndarkness
  • 1,011
  • 2
  • 16
  • 36

4 Answers4

6

Since thresholdsGetter is not part of the class definition, you need to write A::payloadTypes rather than payloadTypes since Argument Dependent Lookup cannot come to the rescue.

Alternatively, do you have a typo in your implementation of threshold? You've written thresholdsGetter, according to the compiler error. But despite what Stroustrup says1, you still need to declare your enum before the function in your class definition. If you correct that then your code will work as it is.


1Note that

struct foo
{
    enum bar {};
    void foobar(bar b){}
};

will compile, but

struct foo
{
    void foobar(bar b){}
    enum bar {};
};

will not.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • Probably a typo yeah, it's amazing when OP doesn't paste the code but just rewrites it. – Hatted Rooster May 17 '17 at 09:58
  • 1
    Are you sure? When I compile the class (with the enum declared later in the class), I get the error: `unknown type name 'payloadTypes'`. Moving the enum before the function removes the error. – pingul May 17 '17 at 10:00
  • 1
    See [this post](http://stackoverflow.com/questions/6281083/c-order-of-defs-in-the-class-surprise), for example. – pingul May 17 '17 at 10:02
4

You must declare the enum before using it.

class A {
public:
    enum payloadTypes {
        INVALID = -1,
        TEST = 0,
        THRESHOLDS,
        RX,
    };
private:
    unsigned int  threshold(payloadTypes kindOfPayload, int x, int y);

}
Bathsheba
  • 231,907
  • 34
  • 361
  • 483
Zedee.Chen
  • 212
  • 1
  • 7
3

Your compiler error is about thresholdsGetter(), but your code presented is about threshold(). In case the compiler error is the truth, then you need to change your prototype from:

unsigned int  thresholdsGetter(payloadTypes kindOfPayload,int x,int y);

to this:

unsigned int  thresholdsGetter(A::payloadTypes kindOfPayload,int x,int y);

That way A::payloadTypes resolves the enum, given that it's defined before the function. As a mention below, enums cannot be forwarded.


In case the code you posted is the truth, the following hold true:

The problem is that when you declare your function, the enum is not declared or defined.

This will work:

class A {

public:
 enum payloadTypes {
  INVALID =-1,
  TEST=0,
  THRESHOLDS,
  RX,
 };

private:
unsigned int  threshold(payloadTypes kindOfPayload,int x,int y);

};

However, you cannot forward declare the enum. Read more in Forward declaring an enum in c++.

Community
  • 1
  • 1
gsamaras
  • 71,951
  • 46
  • 188
  • 305
1

Nothing in C++ can be used without declaring it before usage or forward declaring it. Its not an issue with public or private at all. Its simply an issue that when you are using the enum in the function it is not yet declared. However an enum cannot be forward declared, so you will have to swap the public & private blocks to make it work.

TheWaterProgrammer
  • 7,055
  • 12
  • 70
  • 159