10

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
  • Also, from a design standpoint, would it be possible to use a reference to a float rather than a pointer? A reference is usually preferred. Commented Oct 27, 2009 at 0:58
  • yes, i know... but in this case i can't get around the dynamicness :) Commented Oct 27, 2009 at 1:02

4 Answers 4

11

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

Sign up to request clarification or add additional context in comments.

Comments

4

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).

1 Comment

Yeah you are probably right about the namespace... I had them in classes because they all had similar functionality
3

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.

Comments

1

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".

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.