calling a class in C++

Programming, for all ages and all languages.
Post Reply
stanko51
Member
Member
Posts: 32
Joined: Fri Mar 27, 2009 6:58 pm

calling a class in C++

Post 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
User avatar
neon
Member
Member
Posts: 1567
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

Re: calling a class in C++

Post 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.
OS Development Series | Wiki | os | ncc
char c[2]={"\x90\xC3"};int main(){void(*f)()=(void(__cdecl*)(void))(void*)&c;f();}
stanko51
Member
Member
Posts: 32
Joined: Fri Mar 27, 2009 6:58 pm

Re: calling a class in C++

Post 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.
User avatar
neon
Member
Member
Posts: 1567
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

Re: calling a class in C++

Post 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.)
OS Development Series | Wiki | os | ncc
char c[2]={"\x90\xC3"};int main(){void(*f)()=(void(__cdecl*)(void))(void*)&c;f();}
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: calling a class in C++

Post by Solar »

Hmmm... you are aware that your header file declares a class Moteur, and your implementation file defines a class Engine?
Every good solution is obvious once you've found it.
User avatar
Creature
Member
Member
Posts: 548
Joined: Sat Dec 27, 2008 2:34 pm
Location: Belgium

Re: calling a class in C++

Post 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?
When the chance of succeeding is 99%, there is still a 50% chance of that success happening.
Post Reply