#Help with separating classes into multiple modules.

33 messages · Page 1 of 1 (latest)

finite forge
#

So I have this simple 3D renderer that works with a OneLoneCoders PixelGameEngine.

There needs to be one class for the scene, which inherits from the olc::PixelGameEngine class.

I have a couple of functions i have written. Theres is a function to draw a line, and clear the screen. Which both need to inherit from the olc::PixelGameEngine class

And i have some linear algebra functions like matrix multiplication, which do not need to inherit from anything.

How do i separate the code below, into different modules, one for math funcs, one for drawing functions, and one for the scene itself, without causing multiple definition errors and stuff ?

buoyant juncoBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

finite forge
#

This is the main.cpp files, which currently contains all the code

#

here is my makefile

build:
    g++ -o main src/main.cpp src/screenFuncs.cpp -lX11 -lGL -lpthread -lpng
run:
    ./main
clean:
    rm main
restive goblet
#

I don’t have time to help but I love your username lol

#

I’m sure someone will jump in soon

finite forge
finite forge
#

YALL DONT IGNOR THIS PLSSSS

crystal moon
#

just make new files and import the functions you need to use?

#

Do you know how to do that?

#

@finite forge

finite forge
#

yeah i do. i tried it

#

but everything is within a class

#

idk how to do it with classes

#

or how to make a header file for a class and put the implementation elsewhere

crystal moon
#
#include <cstdio>
int b() { return 3; }

class X {
public:
  int A() { return b(); }
};

int main() {
  X x;
  int val = x.A();
  printf("%d", val);
}
#

here's a class which uses a function which is not defined in the class

#

You can do that without having to make extra files

#

it's not the best solution but it's a step to refactoring it

finite forge
#

oh okk that should work

#

but it seems like a work around ?and wouldnt it be slightly slower ? No better way to do it ?

crystal moon
#

There are better ways to do it, but in any case you're going to have to make your functions somewhere

#

You can read about header files and then do that if you prefer, but that will just be functions in other files which you call

#

You can always define new methods in the class your that you're subclassing

finite forge
#

okk ill do some reading and get back . Thankss !

crystal moon
#

nw

hidden mirage
#

You just need to declare the function beforehand so the compiler knows it will be there.

void Yeet(int x);

void Ya()
{
    Yeet(1337);
}

void Yeet(int x)
{
    // do something with x
}

With a class it will look like this

class Classy
{
    void Moo();
}

void Classy::Moo()
{
    // ...
}
#

Header files just build on this concept, you put all the declarations in a seperate file. There are many free lectures about this online 🙂

finite forge
#

ohh yess thats working just how i needed

#

thanks !
i guess then the

{
  void Moo();
}
``` is what goes into the header file ?
buoyant juncoBOT
#

@finite forge Has your question been resolved? If so, type !solved :)

finite forge
#

!solved