As I was busy inlining some of my functions, I was wondering if it is really necessary to inline anything. Doesn't the compiler automatically inline stuff, or do I have false memories? And if the compiler does inline stuff for me, would it be best to let it do its job or should I mess up something by doing it myself?
The reason I thought of the first question is because I can't get my now-inlined functions to work. A quick example:
test.cpp
#include <iostream>
#include <string>
#include "test.h"
void main()
{
std::cout << getString();
std::cin.get();
}
test2.cpp
#include "test.h"
inline
std::string getString()
{
return "Beam me up, Scotty!";
}
test.h
#ifndef TEST_H
#define TEST_H
#include <string>
std::string getString();
#endif
This generates the error:
test.obj : error LNK2019: unresolved external symbol "class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __cdecl getString(void)" (?getString@@YA?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ) referenced in function _main
C:\w\e\Projects\test\Debug\test.exe : fatal error LNK1120: 1 unresolved externals
Removing the inline will make the program work.