Error Message C4506 - Inline Functions

Started by
8 comments, last by spudulike 20 years, 9 months ago
Hi, I have a class with a protected function called IsWindowClassRegistered that is marked as static and inline. static bool IsWindowClassRegistered( void ); I''ve defined the function in the CPP file as: bool Win32Window::IsWindowClassRegistered( void ) { return true; } But, each time I compile I''m getting a C4506 message: warning C4506: no definition for inline function ''bool Win32Window::IsWindowClassRegistered(void)''. I have a number of inline functions that are implemented this way and they are fine, it just happens to be this one. Could some explain what this message means and how to solve the problem? I am using Visual C++ and VS.NET 2003. Many Thanks, Michael
Advertisement
inline functions have to be defined in the header, no?
--- krez ([email="krez_AT_optonline_DOT_net"]krez_AT_optonline_DOT_net[/email])
I don''t think so?!? In the header file I have a class with a inline function:

.h = static inline HWND GetMainWindow( void );
.cpp = inline Win32Window::GetMainWindow( void ) { return hWnd; }

This works fine, its just for the static inline bool IsWindowClassRegistered( void ) function I get the above warning message.

Regards,

Michael

Inline code must be defined where it is declared, which is one reason its a bad idea to micro optimize before you complete the project. It makes sense to inline your GetFoo(), GetBar(), etc.. functions but you''re not going to see any reasonable increase in speed from this, since you presumably are not going to be calling these functions millions of times so the 6 extra cycles are not going to cost you anything and the inlining only obfuscates your code.

This is one reason I really wish C++ had a ''readonly/writeonly'' keyword. Readonly variables would be public for purposes of reading, but private for purposes of writing. writeonly would obviously be the opposite, and there are definitely times when a writeonly variable would come in hand.

readonly HWND hWnd;

That would be cool, but I''m sure academics will give a few hundred reasons why it would completely destroy the ''magic'' of the C++ language, otherwise it would have already been implemented.
You''re talking about making a variable const depending on where/how it''s being accessed?
daerid@gmail.com
quote:Original post by haro
This is one reason I really wish C++ had a 'readonly/writeonly' keyword. Readonly variables would be public for purposes of reading, but private for purposes of writing. writeonly would obviously be the opposite, and there are definitely times when a writeonly variable would come in hand.


Here, this is for you.
template <typename T> class readonly<T>{public:    readonly(const T& init) : data(init) { }    operator const T&() const { return data; }private:    const T data;};readonly<int> foo = 3;int i = foo + 2;foo = 7; // compile-time error


writeonly is left as an exercise to the reader (hint: assignment operator must be overloaded).
How appropriate. You fight like a cow.

[edited by - sneftel on July 7, 2003 7:34:36 PM]
quote:Original post by Sneftel
Here, this is for you.


That's just a templated const variable. Daerid was on to something more along what I was thinking of. I would like a way to declare a variable as 'const' when referenced in a different class than it was declared, but as a normal variable when accessed within the same class.

For example:

struct Class{  Class()  {    var = 123;  }  void Increase()  {    ++var;  }  readonly int var;};main(){  Class c;  int i = c.var + 5;  c.var = 3; // compile error  c.Increase(); // modifies readonly variable in same class}



And obviously a writeonly variable would be similiar. You could modify its value from outside of the class where it was declared, but could not read its value unless within the class it was declared.

[edited by - haro on July 7, 2003 9:37:00 PM]
class readonlyx{private:   int x;public:   int X() {return x;}};class writeonlyx{private:   int x;public:   void X(int val) {x=val;}};


why won''t those work?
quote:Original post by honayboyz
... some source ..
why won't those work?


Heh, pretty obvious I accidentally hijacked this thread. Read the original message. The OP was trying to get over the function call overhead on a typical accessor, by inlining it.. which was the whole point of the readonly variable. You could declare a variable as readonly instead of creating a function for accessing it. This eliminates all overhead and allows a C++ programmer to use classical C style data access ( direct instead of accessor based ) , while still maintaining C++ level safety and encapsulation of data.

EDIT: Started a new thread on the topic of readonly/writeonly.

[edited by - haro on July 7, 2003 12:56:01 AM]
Thank you all for your response.

Apparantly this warning is generated if you try to call an inlined function, which is defined in the source file, from outside of the source file where it was defined.

This is the only inline function that is called from an outside function thereby explaining why it only appears for this function.

The readonly keyword would be a great idea! By providing direct access to method, but as readonly, you can allow outside functions to access the data, without any function call overhead and at the same time stopping any un-authorized access that could cause the class to become unstable.

Just like in C#! But, I don''t think you can modify a readonly member outside of the constructor.

Haro: feel free to use this thread for a ''readonly keyword'' discussion.

Regards,

Michael

This topic is closed to new replies.

Advertisement