member access into incomplete type

Started by
4 comments, last by inanhour& 9 years, 5 months ago
Hello. Thanks in advance for reading.

In this pattern http://gameprogrammingpatterns.com/component.html

with the example:

Bjørn contains an instance of inputComponent and calls its method like input_.update(this*); this method then accesses members of bjørn, like bjorn.velocity -= WALK_ACCELERATION;

I didn’t understand how this could work, each object accessing each others members/methods without causing a dependancy loop. So I made a little test program, but I can’t get it to work. I get the error 'member access into incomplete type 'object''.Am I missing something? Or not understanding the pattern?

My basic test:

object.h:
#include "component.h"
 
class object{
private:
    component _component;
public:
    const char* getSomething();
    void doSomething();
};
object.cpp:
#include "object.h"

void object::doSomething(){
    _component.accessThis(*this);
}

const char* getSomething(){
    return "something";
}
component.h:
#include <iostream>

class object;

class component{
public:
    void accessThis(object& obj);
};
component.cpp:
#include "component.h"

void component::accessThis(object& obj){
    std::cout<< "got" << obj.getSomething() << ".\n";
}
Advertisement

component.cpp has to include object.h to get the definition of the object class. You also need include guards to prevent multiple inclusions.

component.cpp has to include object.h to get the definition of the object class. You also need include guards to prevent multiple inclusions.


Thanks for your quick response.

When I put #include "object.h" in component.cpp i get a linker error:

Undefined symbols for architecture x86_64:
"object::getSomething()", referenced from:
component::accessThis(object&) in component.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

The definition of the getSomething function belongs to the object class, but you've defined it as a free function instead.

In other words, in the object.cpp, this:


const char* getSomething(){

needs to be changed to this:


const char* object::getSomething(){

Hello to all my stalkers.

Oh my god I am an idiot. tongue.png

This topic is closed to new replies.

Advertisement