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" 开发者_JAVA百科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));
}
精彩评论