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).