vendredi 28 juin 2019

Getting 'member is ambigous' error when compiling derived classes from templated base class

Below is a simplified version of my code. Basically, I am trying to create some Data Provider classes, and also some Data Inquirer classes that call the method from the registered Data Providers.


#include <iostream>


template <typename T>
class Provider
{
    public:
        virtual ~Provider( ) = default;
        virtual T Provide( ) = 0;
};


template <typename T>
class Inquirer
{
    public:
        Provider<T>* provider;

    public:
        void Register( Provider<T>& provider ) {
            this->provider = &provider;
        }

    public:
        void Inquire( T& data ) const {
            data = provider->Provide( );
        }
};


class Data1 : public Provider<int>
{
    public:
        int Provide( ) {
            return 42;
        }
};


class Data2 : public Provider<double>
{
    public:
        double Provide( ) {
            return 3.14;
        }
};


class Query : public Inquirer<int>, public Inquirer<double>
{
    public:
        void PrintInt( ) {
            int data;
            Inquire( data );
            std::cout << "Value: " << data;
        }


    public:    
        void PrintDouble( ) {
            double data;
            Inquire( data );
            std::cout << "Value: " << data;
        }
};


int main( void )
{
    Data1 data1;
    Data2 data2;
    Query query;

    query.Register( data1 );
    query.Register( data2 );

    query.PrintInt( );
    query.PrintDouble( );
    return 0;
}


When compiling, I get 'member is ambigous' errors in both the query.Register() calls and the Inquire() calls inside the Query Class. Since I am passing very specific parameters to each method, I don't understand why this error is happening...

How can I fix this, so that the correct methods are called?

P.S. I am compiling with gcc -std=c++11 (gcc version 5.4)

Aucun commentaire:

Enregistrer un commentaire