문제

This is the base class:

template <class T>
class DataLogger
{
        // ...
    public:
        void AddData(T Data);
        // ...
}

And this is the derived class:

#include "DataLogger.h"
#include <utility>

class Plotter : public DataLogger<std::pair<long double, long double>>
{
        // ...
    public:
        void AddData(long double x, long double y);
        // ...
}

// This method is supposed to wrap the original AddData() method
// which was defined in the base class
void Plotter::AddData(long double x, long double y)
{
    AddData(std::make_pair(x, y));  // LINE #29
}

The given error is:

LINE 29: IntelliSense: no suitable conversion function from "std::pair" to "long double" exists

LINE 29: IntelliSense: too few arguments in function call

Apparently, the problem is that I can not access to the method in base class from derived class, even though it is defined public.

How do I make this code work?

(My IDE is Visual Studio 2010.)

도움이 되었습니까?

해결책

Your AddData from the base is hidden by the AddData from derived class. Either explicitly qualify the call DataLogger<std::pair<long double, long double>>::AddData(...) or bring it to scope with using DataLogger<std::pair<long double, long double>>::AddData;

다른 팁

Your AddData in derived class hides the function AddData in the base class, so all you need to do is, unhide the latter using using directive:

class Plotter : public DataLogger<std::pair<long double, long double>>
{

public:
   //unhiding : bringing base class function into scope!
   using DataLogger<std::pair<long double, long double>>::AddData;

};

Read Item 33: Avoid hiding inherited names from Effective C++ by Scott Meyers.

In order to call the super class method write ::AddData(x, y);. The new Plotter::AddData method makes DataLogger::AddData invisible.

The problem is not "that I can not access to the method in base class from derived class, even though it is defined public".

The problem is that Plotter::AddData is trying to call itself (with a pair) instead of the AddData in the base class.

You can make the call explicit by writing

void Plotter::AddData(long double x, long double y)
{
    DataLogger<std::pair<long double, long double>>::AddData(std::make_pair(x, y));
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top