1

这是基类:

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

这是派生类:

#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
}

给定的错误是:

第 29 行:IntelliSense:不存在从“std::pair”到“long double”的合适转换函数

第 29 行:IntelliSense:函数调用中的参数太少

显然,问题是我无法从派生类访问基类中的方法,即使它是公共定义的。

如何使此代码工作?

(我的 IDE 是 Visual Studio 2010。)

4

4 回答 4

5

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;

于 2011-01-06T15:26:33.953 回答
2

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.

于 2011-01-06T15:26:23.933 回答
1

为了调用类方法 write ::AddData(x, y);。新Plotter::AddData方法使DataLogger::AddData 隐形

于 2011-01-06T15:24:49.753 回答
1

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));
}
于 2011-01-06T15:27:27.757 回答