Strange linkage error... Using eclipse

closed account (Lv0f92yv)
I have a static class function defined:

Function.cpp
1
2
3
4
5
6
//determine if this line is a function, return true if it is.
//does not include constructors.
static bool isFunction( string line )
{
	return true;
}


Function.h (public scope)
static bool isFunction( string );

I am trying to use it in my main program that includes Function.h:

if ( Function::isFunction( tests[i] ) )

And I get the following error:
../testharness1_Function.cpp:55: undefined reference to `Function::isFunction(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)


Not sure why this isn't working, I googled and saw somewhere that I needed to include some compiler option to take advantage of static linkage?

I am using Eclipse C++.

Wrong - it's because isFunction is not a static method of class Function. Static linkage means something else besides the myriad of uses of static in C++.

It must be a static method of class Function for you to call it as Function::isFunction()

If it's just sitting in Function.cpp, out in the open, call it directly: isFunction()

You have to watch out - static is an overloaded keyword in C++.
Either:
1. declare a static method inside your class in your .h and omit the static keyword in your .cpp file
or
2. in your .cpp file just define a static function for use inside that unit

Usually, it doesn't make sense to declare a static function in a header file...
Last edited on
closed account (Lv0f92yv)
Thanks - Chose option 1.
Topic archived. No new replies allowed.