Недопустимое использование функции-члена (вы забыли '()'?) - PullRequest
0 голосов
/ 02 августа 2020

У меня возникла эта ошибка, и я не могу полностью понять, почему это происходит. Я пытаюсь использовать функцию шаблона внутри базового класса как такового:

в файле .h

class CylindricalWave {
public:
    virtual ~CylindricalWave() {}
    virtual double intensity(double r, double z, double t) = 0;
    virtual std::complex<double> efield(double r, double z, double t) = 0;

    template<int Nr, int Nz>
    Eigen::Matrix<std::complex<double>, Nz, Nr> efield(Eigen::Matrix<double, Nz, Nr>& rs, Eigen::Matrix<double, Nz, Nr>& zs, double t);

    template<int Nr, int Nz>
    Eigen::Matrix<std::complex<double>, Nz, Nr> intensity(Eigen::Matrix<double, Nz, Nr>& rs, Eigen::Matrix<double, Nz, Nr>& zs, double t);
};

Я определяю свои методы шаблона в .h файл также:

template<int Nr, int Nz>
Eigen::Matrix<std::complex<double>, Nz, Nr> CylindricalWave::efield(Eigen::Matrix<double, Nz, Nr>& rs, Eigen::Matrix<double, Nz, Nr>& zs, double t) {
        Eigen::Matrix<std::complex<double>, Nz, Nr> output;
        for (size_t i = 0, size = rs.size(); i < size; i++)
        {
            
            double temporary_r = (*(rs.data() + i));
            double temporary_z = (*(zs.data() + i));

            *(output.data()+i) = this->efield(temporary_r, temporary_z, t); //here I call the virtual efield of Cylindrical Wave. I thought the compiler would understand that it has to call the child method that accepts doubles.

            
        }
        return output;
    }

Теперь у меня есть дочерний класс с именем GaussianBeam, в котором я переопределяю метод efield:

class GaussianBeam : public CylindricalWave {
    public:

        
        virtual std::complex<double> efield(double r, double z, double t); //this is the define this virtual method.


        virtual double intensity(double r, double z, double t); //I also define this one.
    };

Теперь, когда efield определено ( версия, которая принимает double r, double z, double t), я надеялся, что моя шаблонная версия efield будет работать, позволив мне позвонить:

GaussianBeam gauss = GaussianBeam();
const int Nx = 3;
const int Ny = 2;
    
//creates the "meshgrid"
Eigen::Matrix<double, Ny, Nx> X = Eigen::RowVectorXd::LinSpaced(Nx, -5e-4,5e-4).replicate(Ny,1);
Eigen::Matrix<double, Ny, Nx> Y = Eigen::VectorXd::LinSpaced(Ny, -5e-3, 5e-3).replicate(1, Nx);

    
//calculates the field:
//I would have expected the code to call the parent version of efield since it's the only that matched the signature of the arguments. Yet, I feel like it's trying to call the one that uses doubles that was defined in the class GaussianBeam.
gauss.efield<Nx, Ny>(X, Y, 0);

Но эта последняя строка просто говорит Invalid use of member function (did you forget the ‘()’ ?), что я не очень понимаю ну, так как я думал, что GaussianBeam унаследует шаблонную версию efield, и я смогу вызвать ее из экземпляра GaussianBeam.

Почему это происходит? Что я неправильно понимаю насчет наследования? Спасибо.

1 Ответ

3 голосов
/ 02 августа 2020

Добавьте эту строку где-нибудь в GaussianBeam:

using CylindricalWave::efield;

В противном случае, efield член в GaussianBeam скрывает все члены с именем efield в базовом классе. А поскольку GaussianBeam::efield не является шаблоном, компилятор предполагает, что угловая скобка в gauss.efield<Nx, Ny>(X, Y, 0) является оператором «меньше».

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...