Sign in to follow this  
pmouse

inline and VS compiler error

Recommended Posts

pmouse    148
namespace MGS
{
	namespace Math
	{
		inline float MGS_Sin(float angle);
	};
};

and in the CPP file:
inline float MGS::Math::MGS_Sin(float theta)
{}

The above code, when compiled, gives the following error: error LNK2019: unresolved external symbol "float __cdecl MGS::Math::MGS_Sin(float)" (?MGS_Sin@Math@MGS@@YAMM@Z) referenced in function _WinMain@16 All headers have been properly included. What's the error here?

Share this post


Link to post
Share on other sites
pmouse    148
Quote:
Original post by AlanKemp
Inline functions have to be implemented .. er .. inline. Put the body of the function into the header file and it should all work.

Alan


Yeah I just figured that out.

Can't do it tho, need to look up table values and the table can't be defined in the header file :(

Share this post


Link to post
Share on other sites
Dave Hunt    4872
Quote:
Original post by pmouse
Can't do it tho, need to look up table values and the table can't be defined in the header file :(


But the table can be declared in the header file.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Quote:
Original post by Dave Hunt
Quote:
Original post by pmouse
Can't do it tho, need to look up table values and the table can't be defined in the header file :(


But the table can be declared in the header file.


ACtually, declaring a global var in the header and have it included multiple times give me an error in MSVC. Which is not what I expected since I have both pragma once and #ifndef...#endif protection.

Share this post


Link to post
Share on other sites
Sneftel    1788
Quote:
Original post by Anonymous Poster
ACtually, declaring a global var in the header and have it included multiple times give me an error in MSVC.

No, _declaring_ it in multiple translation units is fine. _Defining_ it is not.

// Examples of declarations -- Have these once per translation unit
void foo();
extern int bar;
extern char str[10];

// Examples of definitions -- Have these once per program
void foo() { printf("Foooo!\n"; }
int bar;
char str[10] = "eeep!";


_Declaring_ something more than once in the same translation unit will give an error at compile time, and that is what inclusion guards such as #ifndef...#endif and #pragma once prevent. _Defining_ something in more than one translation unit will give an error at link time, and inclusion guards won't help you.

Share this post


Link to post
Share on other sites
SiCrane    11839
Quote:
Original post by Sneftel
_Declaring_ something more than once in the same translation unit will give an error at compile time, and that is what inclusion guards such as #ifndef...#endif and #pragma once prevent.


<nitpick>Actually declarations that aren't also definitions are legal to duplicate within a translation unit. e.g. extern variables, forward declarations, function declarations, etc. It's the one definition rule, not the one declaration rule. So a program that does:

extern int i;
extern int i;

while strange, is perfectly legal C++.
</nitpick>

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Quote:
Original post by SiCrane
You need to declare global variables in header files with an appropriate linkage class modifier, usually extern.

More information on extern (and other good stuff for using header files properly).


Great Link! Explains everything.

Rating++;

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this