0

I currently have the following in my header file:

class Scene {
private:
    const char* file;
public:
    void sceneRender();
    void setFile(const char* f) { file = f; }
    void printFile() { cout << file; }
};

I want to define this function in another file apart from it but am having trouble with what to put inside of texture.loadFromFile() in order to make it run. It runs fine if I just keep this whole thing in the class definition in the header.

void sceneRender() {
    sf::Event event;
    sf::Texture texture;
    texture.loadFromFile(file);
    sf::Sprite sprite;
    sprite.setTexture(texture);
    while (window.pollEvent(event))
    {
        if (event.type == sf::Event::Closed)
            window.close();
    }
    window.clear(sf::Color::Black);
    window.draw(sprite);
    window.display();
}

Is there a way to do this. Also is there any advantage to defining all of ones functions outside the header file where the class is aside from aesthetics?

Thank you

senreigh
  • 3
  • 1

2 Answers2

0

The class definition is usually on a .h file so you'd have:

myfile.h

class Scene {
private:
    const char* file;
public:
    void sceneRender();
    void setFile(const char* f) { file = f; }
    void printFile() { cout << file; }
};

And in a separate file, which usually has the .cpp extension and the same name as the header file, you define the methods of the class, you will have to include the name of the header where the class is defined and use the class name along with the scope resolution operator(::) as a prefix of the method to be defined, in this case Scene::sceneRender, in order for the class members be to recognized and correctly defined:

myfile.cpp

#include "myfile.h" //include header file

void Scene::sceneRender() { //class scope
    //...
    texture.loadFromFile(file); //file member is now recognized and can be used
    //...
}

There are several reasons to separate declaration/definition of methods of a given class but that subject is widely discussed in the platform, for example in these two answers:

https://stackoverflow.com/a/333902/6865932

https://stackoverflow.com/a/9076043/68659322

anastaciu
  • 23,467
  • 7
  • 28
  • 53
0

If you are trying to define the function in a different file, you would need to write it like:

void Scene::sceneRender()
{//your function definition here
}

and also #include "headerName.h" at the beginning of that file.


About why you would want to put codes separate from your header file, you can check this wikipedia page: Class implementation file

Using this structure, a class definition file containing the declaration of the class and its members is also created. If the class definition has been included and the implementation file for its methods is available, the user can instantiate an object of the class. The purpose of this structure is to keep the implementation code hidden, but allow the user to view the design.

Users make use of the public interface of an object so as to make creating objects as simple as possible, ensuring that client code does not distract the user with unnecessary details of the class's implementation. This allows the user the information needed to use the class effectively, but prevents him or her from damaging the compiled code.

Ranoiaetep
  • 5,872
  • 1
  • 14
  • 39