# inline and VS compiler error

This topic is 4852 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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 on other sites
Inline functions have to be implemented .. er .. inline. Put the body of the function into the header file and it should all work.

Alan

##### Share on other sites
Quote:
 Original post by AlanKempInline 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 on other sites
Quote:
 Original post by pmouseCan'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 on other sites
Quote:
Original post by Dave Hunt
Quote:
 Original post by pmouseCan'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 on other sites
You need to declare global variables in header files with an appropriate linkage class modifier, usually extern.

##### Share on other sites
Quote:
 Original post by Anonymous PosterACtually, 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 unitvoid foo();extern int bar;extern char str[10];// Examples of definitions -- Have these once per programvoid 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 on other sites
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 on other sites
Quote:
 Original post by SiCraneYou 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).

Rating++;