Page 1 of 1

calling a class in C++

Posted: Sat Apr 04, 2009 10:44 am
by stanko51
Hello,

I'm trying to call a class in C++ but when i try to link i get the error :
kernel.cpp:(.text+0xe): undefined reference to 'Engine::Engine()'
kernel.cpp:(.text+01d): undefined reference to 'Engine::Test()'

Here is my code :

kernel.cpp

Code: Select all

#include "includes/Engine.h"
extern "C" void _main(struct multiboot_data* mbd, unsigned int magic);

void _main( struct multiboot_data* mbd, unsigned int magic )
{
   Moteur m;
   m.Test();
   
   for(;;){}
}
Engine.cpp

Code: Select all

class Engine{
      Engine::Engine(){
           
           }
           
      void Engine::Test(){
           unsigned char *videoram = (unsigned char *) 0xb8077;
           videoram[0] = 65;
           videoram[1] = 0x07;
           }
      };      
Engine.h

Code: Select all

class Moteur{
      public:
             Moteur();
             void Test();
      };

My make file execute like this :
i586-elf-g++ -c Moteur.cpp -nostdlib -nostartfiles -nodefaultlibs
i586-elf-g++ -c kernel.cpp -nostdlib -nostartfiles -nodefaultlibs
i586-elf-ld -T linker.ld -o kernel.bin loader.o Moteur.o kernel.o

But the last line returns the error.

Do you have any idea what i'm doing wrong ?

Thank you very much

Re: calling a class in C++

Posted: Sat Apr 04, 2009 11:09 am
by neon
This should be in general programming.

Also, why are you defining your engine class functions using scope resolution (::)? Its not needed when defining functions within the same scope as the class itself. i.e., instead of void Engine::Test(), all you need is void Test().

Lastly, you are getting the errors because the class Moteur member functions are not defined anywhere. I cannot help you in resolving this problem without having a better understanding of what your intentions are.

Re: calling a class in C++

Posted: Sat Apr 04, 2009 11:28 am
by stanko51
sorry I made mistake in posting my question. It should be :

Engine.h

Code: Select all

class Engine{
      public:
             Engine();
             void Test();
      private:
      };
      
and for the linker :

i586-elf-g++ -c Engine.cpp -nostdlib -nostartfiles -nodefaultlibs
i586-elf-g++ -c kernel.cpp -nostdlib -nostartfiles -nodefaultlibs
i586-elf-ld -T linker.ld -o kernel.bin loader.o Engine.o kernel.o

I guess it makes more sense like this.


So basically, what im trying to do issimply to have a class, create an instance in my _main and call one of its method. But i can't link with what i'm doing.

thank you for your help

PS: I know the :: should not be required. I just put them to make sure, because I am not familiar with system programing i thought it might be the reason of my problem.

Re: calling a class in C++

Posted: Sat Apr 04, 2009 12:02 pm
by neon
stanko51 wrote:PS: I know the :: should not be required. I just put them to make sure, because I am not familiar with system programing i thought it might be the reason of my problem.
Scope resolution (::) is only dependent on the language syntax. It has nothing to do with anything else.

In any case, the linker error is still caused by the same reason that I pointed earlier.

If that is your engine.h, I assume the member functions are defined in a engine.cpp file, and that engine.cpp is being built with the project. What you can do is simply include the engine header file, and use the class the same way you are using the Moteur class.

Either way, to fix your error is to either use your engine class (as noted earlier) or define the member functions in the Moteur class (as noted in an earlier post.)

Re: calling a class in C++

Posted: Sun Apr 05, 2009 3:31 am
by Solar
Hmmm... you are aware that your header file declares a class Moteur, and your implementation file defines a class Engine?

Re: calling a class in C++

Posted: Sun Apr 05, 2009 3:42 am
by Creature
stanko51 wrote:sorry I made mistake in posting my question. It should be :

Engine.h

Code: Select all

class Engine{
      public:
             Engine();
             void Test();
      private:
      };
      
and for the linker :

i586-elf-g++ -c Engine.cpp -nostdlib -nostartfiles -nodefaultlibs
i586-elf-g++ -c kernel.cpp -nostdlib -nostartfiles -nodefaultlibs
i586-elf-ld -T linker.ld -o kernel.bin loader.o Engine.o kernel.o

I guess it makes more sense like this.


So basically, what im trying to do issimply to have a class, create an instance in my _main and call one of its method. But i can't link with what i'm doing.

thank you for your help

PS: I know the :: should not be required. I just put them to make sure, because I am not familiar with system programing i thought it might be the reason of my problem.
Have you defined the functions in a .cpp file?

Code: Select all

//e.g. Engine.cpp

Engine::Engine()
{

}

Engine::Test()
{

}
And are you sure that CPP file (if you have it) is being linked in the executable?