0

I have the below class that has two custom objects as member variables. I get errors with the bold section. What is the proper way to access protected members of the composition classes? I get 'within this context' errors the f, gleft, and gright functions and when I try to resize u.

class C
{
protected:
    A x;
    B y;
    double a;
    double b;
    Eigen::MatrixXd u;

public:
    double f(double x)
    {
        return x;
    }
    double gleft(double tau)
    {
        return tau
    }
    double gright(double tau)
    {
        return 0;
    }
    FD_Euro_Put(): x(), y(), a(0), b(0){}
    FD_Euro_Put(char y1, double y2, double y3, double y4, double x2,
            double x3, double x4, double x5, double x6, double x7):
            x(x1, x2, x3, x4, x5, x6, x7)
    {
        double Xleft = x1*x2;
        double Xright = x1*x3;
        double Tauf = x1*x1;
        double NN = floor((x1/x2);
        a = x1*x2 - 0.5;
        b = x1*x2 + 0.5;

        pde = HeatPDE(y1, NN, Xleft, Xright, Tauf, Alpha); //begin problem area
        u.resize(pde.N+1, pde.M+1); 
        if(fdtype == 'f')
            u = pde.Forward_Euler(&f, &gleft, &gright);
        else if(fdtype == 'b')
            u = pde.Backward_Euler(&f, &gleft, &gright);
        else if(fdtype == 'c')
            u = pde.Crank_Nicolson(&f, &gleft, &gright); //end problem area
        else
            cout << "Incorrect choice for finite difference type!" << endl;
    }
1
  • x1 seems to be missing in the constructor's signature... Also, you can't format a part of a code on Stack Overflow. Try to mark the section with code comments instead.
    – leemes
    Dec 6, 2012 at 19:02

1 Answer 1

0

You can't access protected members of a class unless it explicitly lists you as a friend or you inherit from that class.

A hack would be to wrap that class in your own class which gives getters for those members.

But from a logical standpoint, if those members aren't public, you probably shouldn't need access to them anyway.

3
  • so would I just declare x and y as: friend classA x, friend classB y?
    – postelrich
    Dec 6, 2012 at 19:25
  • @riotburn look up friend declarations. Dec 6, 2012 at 19:31
  • Ok I figured it out thanks! I have to add C as a public friend member to A and B.
    – postelrich
    Dec 6, 2012 at 20:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.