0

I have a method called initializeObjects() in my main.cpp file, and I want to call it from a method in another source file, named Scene.cpp. How do I do that? This is my main.cpp file, without the headers:

static void initializeObjects();
int main() {
    Scene myScene;
    myScene.render(640,480);
    return 0;
}

void initializeObjects(){
    //Add a plane of gray color
    Scene::shapes.push_back(std::make_shared<Plane>(Vector3D(0,1,1), Vector3D(0,0,80), COLOR_GRAY));
    //Add two spheres
    Scene::shapes.push_back(std::make_shared<Sphere>(100.0, Vector3D(0,50,0), COLOR_WHITE));
    Scene::shapes.push_back(std::make_shared<Sphere>(60.0,ORIGIN, COLOR_RED));
}
1
  • 3
    By marking the function static, you explicitly tell the compiler that it should not be accessible in any other source file. So if you want it thus accessible, drop static. Commented Mar 26, 2016 at 18:46

1 Answer 1

1

When you define a function with storage class specifier static, you explicitly say that you want this function to have an internal linkage - this means it should not be visible outside the translation unit where it is defined.

So to be able to call your function from some other translation unit (Scene.cpp), drop static specifier, and add declaration of your function to header file, which should be included by this other (Scene.cpp) translation unit.

Sign up to request clarification or add additional context in comments.

2 Comments

Okay, I added a declaration in a separate header file, should I use any other extern keyword? What is the scope of initializeObjects() now?
no need for extern, initializeObjects will have external linkage.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.