pure virtual enumerations?

Is there a way to make a pure virtual enumeration?
If not, any suggestions how I could get a similar effect?
Here is the code I'm working with:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <stdio.h>

class System
{
  private: enum ERROR_FLAG; //can make pure virtual?
           virtual void PostError(ERROR_FLAG _error) = 0;
  public:  virtual void Update() = 0;
};

class Graphics : private System
{
  private: enum ERROR_FLAG { ERR_LOST_DEVICE };
           void PostError(ERROR_FLAG _error);
  public:  Graphics();
	   void Update();
};

Graphics::Graphics()
{
}

void Graphics::Update()
{
  PostError(ERR_LOST_DEVICE);
}

void Graphics::PostError(ERROR_FLAG _error)
{
  printf("Error message sent.\n");
}

int main() 
{
  Graphics graphics; //ERROR: cant instantiate abstract class
  return 0;
}

Right now I get an error saying:
error C2259: Graphics cannot instantiate abstract class due to following members:
'void System::PostError(System::ERROR_FLAG)' is abstract
...

I understand the problem is that the functions' signatures don't match.
void PostError(System::ERROR_FLAG)
void PostError(Graphics::ERROR_FLAG)

Making a few changes fixes this but I get a new error. This is not what I want
anyways. I want to force other classes that inherit from System to be required
to define an ERROR_FLAG enumeration.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <stdio.h>

class System
{
  private: virtual enum ERROR_FLAG; //change
           virtual void PostError(ERROR_FLAG _error) = 0;
  public:  virtual void Update() = 0;
};

class Graphics : private System
{
 private:  enum ERROR_FLAG { ERR_LOST_DEVICE };
           void PostError(::ERROR_FLAG _error); //change
  public:  Graphics();
           void Update();
};

Graphics::Graphics()
{
}

void Graphics::Update()
{
  PostError(ERR_LOST_DEVICE); //ERROR: ERR_LOST_DEVICE incompatible
}

void Graphics::PostError(::ERROR_FLAG _error) //change
{
  printf("Error message sent.\n");
}

int main() 
{
  Graphics graphics; //fixed
  return 0;
}


Thanks
I may resort to just making the PostError function take an int.
closed account (o1vk4iN6)
Why not just define the enum into system? If the values are going to be that different perhaps you should rethink using inheritance?

1
2
3
4
5
6
7
8
9
10
class System
{
public:
      enum ERROR_FLAG { ERR_LOST_DEVICE };

private:
      virtual void PostError(ERROR_FLAG _error) = 0;
public:  
      virtual void Update() = 0;
};
Last edited on
It is not possible to instantiate a class with a private pure virtual function. If it's private, what can any base class expect to do to it? They can't define it ;p
Thanks for the responses.

Sorry I wasn't more clear L B, I understand the error about the class being abstract and the reason for the abstraction error(non matching function signatures). I just wanted to know if there was a unique syntax or trick to get around the function signatures not matching due to the ERROR_FLAG enumeration being defined in system.

I don't want to define the enumeration inside System. The idea is that each System will send errors only to themselves and handle their own list of error messages. For example, If I have a Physics class and a Graphics class and both inherit from System, only the Physics class will know about its own errors and how to handle them. Same with Graphics, no System will know anything about any other System's errors.

Guess the ERROR_FLAG enumeration is not necessary though. Just going to make PostError take an int.

virtual void PostError(int _errorID) = 0;

Any programmer working with the System class should be able to figure out what the _errorID parameter through comments/documentation and be smart enough define their own enumeration/defines/constants.
Last edited on
Topic archived. No new replies allowed.