Archived

This topic is now archived and is closed to further replies.

spudulike

Error Message C4506 - Inline Functions

Recommended Posts

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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites