Sign in to follow this  

undefined reference to 'function'

This topic is 1954 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi everyone,
I am facing a very weird compiling error. I am using OpenGL with code::blocks IDE. However, I don't think that this problem is related to OpenGL at all.

I am using math.h header file. I have an GLfloat variable that contains an angle in degrees. However, when I use this angle for sin() or cos() functions it needs to be in radians. Hence, I created a separate class named "Misc" in a separate header file that has this conversion function

Misc.h
[CODE]
#ifndef MISC_H_INCLUDED
#define MISC_H_INCLUDED
#include <GL\gl.h>
#include <GL\glu.h>
class Misc
{
public:
Misc() {}
static GLfloat toRadians(GLfloat degrees);
static void foo();
};

#endif // MISC_H_INCLUDED
[/CODE]

Misc.cpp
[CODE]
#include "Misc.h"
GLfloat Misc::toRadians(GLfloat degrees)
{
return degrees * (3.14159265f / 180.0f);
}
void Misc::foo()
{
}
[/CODE]

hence, whenever I call these functions anywhere in the program, I get compilation errors of "undefined reference to 'Misc::toRadians(float)' "
and "undefined reference to 'Misc::foo()' "

however, wherever I am calling these functions, I have checked that I am including this header file in that source file....

both Misc.h and Misc.cpp are in the same folder....

However, I noticed that I defined the functions in Misc class itself, then I have no errors, If I define these functions outside the class in the same header file then I get multiple definitions error....

I am completely struck and I can't figure out what I am doing wrong here...?

Thanks for any help, appreciate it.... Edited by uzipaz

Share this post


Link to post
Share on other sites
[quote name='Mussi']
Are you sure Misc.cpp is being compiled? Also, why not just use a free function?
[/quote]

about the free function, I can use it, but what I am trying to do here right now is I have done before in previous projects without any problem, its just you know... a good programming practice...


[quote name='SamiHuutoniemi' timestamp='1344265081' post='4966677']
Linker errors like this usually means that the linker can't find the compiled product of something. Is it or have you tried having all files in the same directory?
[/quote]

Edit: after reading your post several times, I realized something and I checked the project properties and all the files that are to be compiled were not checked in.... apparently Misc.h and Misc.cpp were'nt checked to be compiled.... I checked both of these files and now the code runs just fine...... :) Edited by uzipaz

Share this post


Link to post
Share on other sites
I wouldn't say that it's better programming practice to put them in a class and make them static. You could use a namespace instead.

But to be honest, without the possibility to sit in front of your screen, it's hard to say why it doesn't link.

Share this post


Link to post
Share on other sites
[quote name='SamiHuutoniemi' timestamp='1344265582' post='4966682']
I wouldn't say that it's better programming practice to put them in a class and make them static. You could use a namespace instead.

But to be honest, without the possibility to sit in front of your screen, it's hard to say why it doesn't link.
[/quote]

you were right, error was generating because Misc.h and Misc.cpp were not compiling for some reason, I had to go to the project properties and check these to files in the build targets and now it works just fine.....

about good programming practice part.... I am an amateur in programming so these functions can be used anywhere in the program hence made them in class so that they will be easily addressed and made them static so that I won't have to create the object of the class to use them.... these are more like global functions....

Share this post


Link to post
Share on other sites
Nice to hear it works!

Exactly for this reason (to be used like global functions), I'd make them free functions instead. But if you are making an engine that will/can be used by others, it IS good practice to put them in a class or namespace. Namespaces, however, are a bit simpler, and you can use "using" to get rid of the namespace::-part to make very readable code.

Share this post


Link to post
Share on other sites
[quote name='uzipaz' timestamp='1344266037' post='4966684']
about good programming practice part.... I am an amateur in programming so these functions can be used anywhere in the program hence made them in class so that they will be easily addressed and made them static so that I won't have to create the object of the class to use them.... these are more like global functions....
[/quote]
Use free functions and group functions together using namespaces.

Share this post


Link to post
Share on other sites
[quote name='Mussi' timestamp='1344266521' post='4966691']
Use free functions and group functions together using namespaces.
[/quote]

Sure, I will try that next time.... :)

Share this post


Link to post
Share on other sites
Purely technically speaking, it's better programming practice to use a free function rather than even a static class method because each static class method can access the private members of the class, so becomes yet one more place where you could potentially inviolate class invariants.

From a purist perspective, any method that does not actually require access to the implementation of a class should not be part of that class interface.

Share this post


Link to post
Share on other sites

This topic is 1954 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this