0

I tried to outsource some functions in another.cpp but if I change someObject in another.cpp, it has to effect someObject in main.cpp. So this is basically the structure now. Which doesn't affect camera.


main.cpp

someObject camera;
setCamera(someObject camera);

int main()
{
    setCamera(camera);
    camera.doSomething();
}

another.cpp

someObject tempCamera;

void setCamera(someObject camera)
{
    tempCamera = camera;
}

void anotherFunction()
{
    tempCamera.doSomethingElse();
}

so what I want to do is making tempCamera pointing to camera,

but when I do it like this:

someObject *tempCamera;

void setCamera(someObject camera)
{
    tempCamera = &camera;
}

anotherFunction()
{
    tempCamera.doSomethingElse();
}

I get an error, that the expression must have a class type. My Question now is: How do I call a function from a pointer or what alternatives do I have?

Student
  • 805
  • 1
  • 8
  • 11

1 Answers1

0

You are running afoul of the One Definition Rule. You probably want to refer to the same someObject tempCamera; in both files, which you can do by marking all but one declaration as extern.

main.cpp

someObject camera;
setCamera(someObject camera);

int main()
{
   setCamera(camera);
   camera.doSomething();
}

another.cpp

extern someObject tempCamera;

void setCamera(someObject camera)
{
   tempCamera = camera;
}

void anotherFunction(){
   tempCamera.doSomethingElse();
}

third.cpp

extern someObject tempCamera;

void yetFurtherFunction(){
   tempCamera.doSomethingDifferent();
}

Repeating this declaration will soon get tedious, so it is common to put the extern declartion in a header file that is #included in multiple .cpp files (a.k.a. Translation Units)

Caleth
  • 52,200
  • 2
  • 44
  • 75