c++ - 在 Eclipse C++ 中无法识别 protected 成员

标签 c++ eclipse inheritance protected

我在以下类中尝试访问 Base 类的 protected 成员,但在 eclipse 中出现错误“Field Factorized could not be resolved”。有人可以向我解释我做错了什么吗?为什么我不能访问变量 mFactorized ??

基础类

template <typename ValueType>
class AbstractDirectLinearSolver{
protected:
    bool mFactorized;
public:
    //Constructors, destructor
    AbstractDirectLinearSolver(){
        mFactorized = false;
    }

    virtual ~AbstractDirectLinearSolver();

    //Methods
    virtual void Solve(Vector<ValueType>& x, const Vector<ValueType>& b) const = 0;
    virtual void Factorize(AbstractMatrix<ValueType>& A) = 0;
};

派生类

#include "AbstractDirectLinearSolver.hpp"

template<typename ValueType>
class CholeskySolver: public AbstractDirectLinearSolver {
private:
    AbstractMatrix<ValueType> *mR; //Pointer = Abstract class NOT ALLOWS instantiation !!
public:
    CholeskySolver() {
        mR = NULL;
    }

    ~CholeskySolver() {
        if (this->mFactorized) {  //ERROR HERE
            delete mR;
        }
    }

    void Solve(const Vector<ValueType>& x, const Vector<ValueType>& b) {
        Vector<ValueType> y(mR->ApplyLowInv(b));
        x = mR->ApplyLowerTransponse(y);
    }

    void Factorize(AbstractMatrix<ValueType>& A) {
        if (mR != NULL)
            delete mR;
        mR = NULL;
        A.CholeskyFactorization(mR);
        this->mFactorized;           //ERROR HERE
    }
};

最佳答案

您正在尝试从类模板而不是类继承。将类头更改为:

template<typename ValueType>
class CholeskySolver: public AbstractDirectLinearSolver<ValueType>
                                                       ^^^^^^^^^^^

看起来编译器在提示 mFactorized 不是成员之后(因为它不知道基类)但在提示基类说明符无效之前退出了.

如果您要注释掉有问题的行,那么您会得到一个稍微好一点(尽管仍然相当困惑)的错误:expected class-name before ‘{’ token

关于c++ - 在 Eclipse C++ 中无法识别 protected 成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13528122/

相关文章:

java - 当调用类的任何方法时中断

javascript - 类方法不会覆盖类字段

javascript - 当两个对象都传递参数时使用对象继承

java - 继承和抽象参数传递

c++ - 如何编写一个快速函数来计算一个数的总除数?

c++ - 定义 _CRT_SECURE_NO_WARNINGS 会导致 vector 析构函数崩溃

c++ - 元组是否有隐含的字典序比较?

java - 从 SVN 导入 JEdit 时出现警告

java - 将C++映射转换为Java映射

java - 当测试不是Java方法时如何让Eclipse跳转到失败的JUnit测试