So I have this really simple situation and I have no idea what is causing this error. This is the relevant bit:

header:

class foo{
  void bar(int n);
}

cpp:

#include "foo.h"
foo::bar(int n){
};

And then, from the main function in a separate .cpp file, I do this:

#include "foo.h"

int main(int argc, char *argv[]){
bar(3);
}

I get the error "undefined reference to foo::bar(int* n)"

I have everything linked together right, I've compiled this project plenty of times before. I can find other functions in foo and its constructor from main as well.

If I replace the call to bar with

bar(3.3);

I get the error "no matching function for call to foo::bar(double)" then "note: candidates are void foo::bar(int*)". So the compiler can locate the function just fine at some level.

Any ideas as to what is going on?

So I have this really simple situation and I have no idea what is causing this error. This is the relevant bit:

header:

class foo{
  void bar(int n);
}

cpp:

#include "foo.h"
foo::bar(int n){
};

And then, from the main function in a separate .cpp file, I do this:

#include "foo.h"

int main(int argc, char *argv[]){
bar(3);
}

I get the error "undefined reference to foo::bar(int* n)"

I have everything linked together right, I've compiled this project plenty of times before. I can find other functions in foo and its constructor from main as well.

If I replace the call to bar with

bar(3.3);

I get the error "no matching function for call to foo::bar(double)" then "note: candidates are void foo::bar(int*)". So the compiler can locate the function just fine at some level.

Any ideas as to what is going on?

This is an incomplete class definition:

class foo{
  void bar(int n);
} //<----missing semi-colon

This is an improper function definition:

#include "foo.h"
foo::bar(int n){
}; //<----extraneous semi-colon
#include "foo.h"

int main(int argc, char *argv[]){
bar(3);
}

Where is the object of class foo, that you need to use to access the function foo::bar()?

Also, are you sure you are linking the correct versions of the files? It doesn't make sense that it is trying to call foo::bar(int*) (notice the '*') when you are writing a call to foo::bar(int)...

Edited 6 Years Ago by Fbody: n/a

In your main function you are trying to call the foo::bar function directly, without an instance of the foo object. Usually you'd do something like

foo myfoo;
myfoo.bar(3);

and even then the bar function would have to be a public function. As it currently stands, the bar function is private (as it has not already been specified otherwise... at least according to your foobar example!)

In order to be able to call the foo::bar() function directly without an instance of the foo class, you'd need to ensure that the bar function was a public static function e.g.

class foo{
public:
	static void bar(int n){};
};

Which would allow you to call the function directly, but you'd have to do it like this:

foo::bar(3);

Not sure if any of this helps, but it's a start!
Cheers for now,
Jas.

OK, I do actually have an instance of foo (sorry, it was dim of me to leave that out of the example). That is not the problem.

And I am linking the right versions of the files, I have absolutely no idea where the compiler thinks this function with a pointer argument is coming from.

The semicolon after the class definition - do not know how that made its way into the post. It is not in my code, which would have given other compilation errors.

Am about to remove extraneous semicolons, will check back.

Thanks for all the comments!

Success! It was the missing semicolon after the foo class declaration. This messed up the function definition after the "foo.h" include.

Glad you got it. Always look there if you are getting weird errors right at the start of a program. If that semi-colon is missing, the compiler will get very confused.

This article has been dead for over six months. Start a new discussion instead.