tags:

views:

1176

answers:

4

Okay,

Here is what I'm trying to do... Right now it is compiling but failing at linking... LNK2001

I want the methods static because there are no member variables, however I also want them inline for the speedups they provide.

What is the best way to do this? Here is what I have in a nutshell:

/* foo.h */
class foo
{
    static void bar(float* in);
};

/* foo.cpp */
inline void foo::bar(float* in)
{
    // some dark magic here
}

I'm trying to do this because I want to be able to go:

foo::bar(myFloatPtr);

foo doesn't have any member variables... it doesn't make sense to.

+2  A: 

You must define your inline function in the header file, not a separate implementation file. The definitions are needed when the header file is #included, if they are hoped to be inlined, after all.

The link failure you are seeing is because the declaration (in the header file) does not inform the compiler that the method is to be inline, whereas the implementation is inline, so not available for linking.

John Zwinck
+5  A: 

If you are calling bar from another cpp file, other than foo.cpp, it needs to be in a header file.

gatorfax
grrrr that makes sense
Polaris878
+3  A: 

First, I would put them in a namespace instead, because there is no logic at all in that "class". Second, you may define the functions body in the header file directly to allow the compiler to see them. Otherwise, you need whole program optimization to be done by the linker for inlining those functions(AFAIK).

AraK
Yeah you are probably right about the namespace... I had them in classes because they all had similar functionality
Polaris878
+1  A: 

Generally inline-able functions are implemented where they are declared (in the header file). The compiler is free to inline functions as you have them, but you can't force it to inline anything. If you're using Visual C++, enable "inline any suitable", "link-time code generation" and "favor fast code".

Tim Sylvester