3
 // In A.h
 class A
 {
  public:
    enum eMyEnum{ eOne, eTwo, eThree };
  public:
    A(eMyEnum e);
 }

 // In B.h
 #include "A.h"
 class B
 {
    B();
    private:
       A memberA;
 }

 // In B.cpp
#include "B.h"
 B::B(void) : memberA(A::eOne)
 {}

The declaration to 'memberA' gives me a compile error using the g++ compiler: error: 'A::eOne' is not a type

How can I overcome this? Do I simply need to create a default constructor that takes no parameters?

一二三
  • 21,059
  • 11
  • 65
  • 74
lost_bits1110
  • 2,380
  • 6
  • 33
  • 44

4 Answers4

8

It sounds like you are trying to initialise a member variable. You could do something like:

class B
{
public:
    B() : memberA(A::eOne) {}  // Initializer list in constructor
private:
    A memberA;
};
Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
1
class B
{
    public:
    B(A::eMyEnum someValue = A::eOne) : memberA(someValue) {};

    private:   
    A memberA;
}
mpm
  • 3,534
  • 23
  • 33
sqykly
  • 1,586
  • 10
  • 16
  • hmm - unfortunately I don't want to have to modify the constructor for class B, as it wouldn't make sense in my particular design. – lost_bits1110 Sep 12 '11 at 23:54
  • you'll have to modify the constructor for class B, because that's where you have to initialize memberA. Think about it - at what other point would memberA be initialized if not at the time the B is constructed? Get rid of the argument if you don't like it, but it won't break anything, and it's really the only way to do it. – sqykly Sep 13 '11 at 00:40
1

A constructor expects a eMyEnum. It is not clear why you would want B's constructor to not accept an eMyEnum parameter too. Anyway, assuming that your aim is to pass the argument to A's constructor as A::eOne (as opposed to A::eMyEnum::eOne), you could try the following code, which uses typedef.

#include <iostream>
using namespace std;

class A {
public:
    typedef enum { eOne, eTwo, eThree } eMyEnum;
public:
    A(eMyEnum e) {
        cout << "A ctor" << endl;
    }
};

class B {
public:
    B() : memberA(A::eOne) {
        cout << "B ctor" << endl;
    }
private:
    A memberA;    

};

int main() {
    B b;
}

// output
A ctor
B ctor

However, notice that memberA's constructor is always called with the argument as A::eOne. You have not showed how this argument is used in the constructor, but I presume that in your real code it initialises a member of A. If the member must always have the same value, make it const and remove the parameter from the constructor.

Arvind Pai
  • 31
  • 1
  • thanks , yes this would work! I ended up accepting Oli's answer though as he was just first. Thank-you though, appreciate it! – lost_bits1110 Sep 13 '11 at 22:43
  • I am glad you found it useful. No worries about accepting the answer. I just happen to live in a different time zone. Bad luck! – Arvind Pai Sep 16 '11 at 05:44
0

eOne is not the type, eMyEnum is the type. What you're essentially saying is that "You must pass the literal 2 into this method" - it doesn't make any sense. If you don't mean to pass an enum into it, you'll have to clarify what you were going for.

Kyle W
  • 3,702
  • 20
  • 32