Reputation: 298
I have a static method in class as follows in file Convert.h
class Convert
{
public :
static string convertIntToStr(unsigned int integer);
};
In Convert.cpp
string
Convert::convertIntToStr(unsigned int integer)
{
ostringstream ostr;
ostr << integer;
return ostr.str();
}
I use this in some other class method in another .cpp file as Convert::convertIntToStr, but I get linking error, which says undefined reference to Convert::convertIntToStr(unsigned int). Could you please let me know what could be wrong?
Upvotes: 2
Views: 632
Reputation: 1169
Put class convert in a header file, and include the same in the other .cpp file
#ifndef CONVERT.H_
#define CONVERT.H_
class Convert
{
public :
static string convertIntToStr(unsigned int integer);
};
#end if
Include it as #include "convert.h"
in the other file.
Upvotes: 1
Reputation: 2200
if you have defined the Convert in a namespace make sure that you are including that namespace when you call. Something like
namespace::Convert::convertIntToStr(...)
or the calling class is in the same namespace.
Upvotes: 3
Reputation: 591
It's a linker error and happens when it can't find the definition of a function, global variable, etc... are you linking all of your objects files?
Upvotes: 3
Reputation: 537
This should really be a comment, but I'm new to SO and it doesn't let me add comments yet.
Sorry if this is a silly question, but are you sure Convert.cpp was added to your project? It sounds like Convert.cpp is not being compiled into an object for the linker.
Upvotes: 4
Reputation: 5866
Ensure that you are properly linking all of your object files.
Upvotes: 3