davivid
davivid

Reputation: 5960

How to access a variable from the parent class

How can I access a variable from a parent class? I thought the below code would do this, but when I try to print out the value of name in Controller.cpp I get the error:

Member access into incomplete type 'TestApp'

TestApp.cpp

#include "cinder/app/AppNative.h"
#include "Controller.h"

using namespace ci;
using namespace ci::app;
using namespace std;

class TestApp : public AppNative
{
  public:
    void setup();
    void update();

    string name = "Parent";

    Controller controller;
};

void TestApp::setup()
{
    controller.setup(this);
}

void TestApp::update()
{
    controller.update();
}


CINDER_APP_NATIVE( TestApp, RendererGl )

Controller.h

#pragma once

class TestApp;

class Controller
{
    public:
        void setup(TestApp* parent);
        void update();
        TestApp* p;
};

Controller.cpp

#include "Controller.h"

void Controller::setup(TestApp* parent)
{
    p = parent;
}

void Controller::update()
{
    std::cout << p->name << std::endl;
}

Upvotes: 0

Views: 384

Answers (3)

Vlad from Moscow
Vlad from Moscow

Reputation: 310920

Class Controller knows nothing about what dara members class TestApp has. It knows only that there is class TestApp that is defined somewhere else.

Upvotes: 0

john
john

Reputation: 87944

This has nothing to do with parent classes. TestApp is defined in TestApp.cpp, it's not visible to the code in Controller.cpp. You need to move the definition of TestApp to a header file (called TestApp.h say) and then #include "TestApp.h" in Controller.cpp.

Upvotes: 2

juanchopanza
juanchopanza

Reputation: 227370

You need to put the TestApp class definition in a header, and include that in TestApp.cpp and Controller.cpp.

Upvotes: 2

Related Questions