2

I have a more complicated version of the following code in a larger project.

template <class classT>
class Base{
    private:
    protected:
        classT var;
    public:
        classT getVar(){return var;}
        void setVar(classT varIn){var = varIn;}
};

class Base2{
    private:
        std::string name;
    public:
        std::string getName(){return name;}
        void setName(std::string nameIn){name = nameIn;}
        virtual void printVars(){std::cout << '*' << name << std::endl;}
};

class Child1: public virtual Base2, public virtual Base<int>{
    private:
    public:
        virtual void printVars(){std::cout << ':' << getName() << std::endl;}
};

class Child2: public virtual Base2, public virtual Base<char>{
    private:
    public:
        virtual void printVars(){std::cout << '-' << getName() << std::endl;}
};

class GChild1: public virtual Child1, public virtual Child2, public virtual Base2, public virtual Base<std::string>{
    private:
    public:
        using Base<std::string>::getVar;
        using Base<std::string>::setVar;
        virtual void printVars(){std::cout << var << std::endl;}
};

Whenever I try to compile the above code I get the following error.

error: reference to 'var' is ambiguous

I understand that in this case there are several variables of that name within GChild1: Base::var through Child1, Base::var through Child2 and Base::var through GChild1. I also understand that I can resolve this issue by being more specific and using the scope operator. However, what I do not understand is why adding a 'using' line to the definition of GChild1 does not resolve this issue. For example

class GChild1: public virtual Child1, public virtual Child2, public virtual Base<std::string>{
private:
    using Base<std::string>::var;
public:
/* Same Code */
}

This produces the same error. I have seen similar questions asked but I felt they were either different enough to warrant a new question or their answers did not seem to suit my needs. I appreciate any guidance in solving this problem and understanding the solution(s).

PhiloEpisteme
  • 857
  • 3
  • 8
  • 19
  • 1
    gcc-4.7 and later seems to work and msvc seems to accept it too. However, mingw-gcc 4.6.3 rejects the code with the ambiguous var error. – greatwolf Jul 09 '13 at 19:35
  • I was afraid that was going to be my response. I have tried upgrading gcc to no avail. I will focus on this for now then. – PhiloEpisteme Jul 10 '13 at 16:50
  • "`class GChild1: public virtual Child1, public virtual Child2`" why do you repeat `public virtual Child2` here? – curiousguy Jul 15 '13 at 10:43
  • @curiousguy I repeat it because I want access to its member functions. I suppose I should have been more clear in that each class may contain other functions as well. I left them out because this question was more about the ambiguity error for the reference to 'var'. – PhiloEpisteme Jul 15 '13 at 16:00
  • @vckngs7 I mean, why do you repeat "`public virtual Base2`"? `Base2` is already inherited through `Child1` and through `Child2`. – curiousguy Jul 15 '13 at 16:05
  • @curiousugy It seems I overlooked that when I translated my project into a question. Good catch, I have edited this from the question. – PhiloEpisteme Jul 15 '13 at 16:09

0 Answers0