c++ - Template re-definition error when compiled with VS 2010 -


i'm running sort of template re-definition error. not solution-breaking error (i've found workarounds), i'd still understand why code erroneous.

template <typename randomengine, typename randomdistribution = std::uniform_int_distribution<int> > struct base_random_generator {     randomengine generator;     randomdistribution distribution;     base_random_generator(randomengine gen,                            randomdistribution dist = randomdistribution(0, 10))         : generator(gen), distribution(dist) { }     int operator()() {         return distribution(generator);     } };  base_random_generator< std::mt19937 > create_mt_generator(std::mt19937::result_type seed = std::mt19937::default_seed) {     return base_random_generator< std::mt19937 > (std::mt19937(seed)); } 

visual studio 2010 fails compile code above error:

... defined in main.obj

fatal error lnk1169: 1 or more multiply defined symbols found

if remove create_mt_generator function, however, error goes away , again. main idea able pass struct behaves std::function can call on , on again random integer.

that function looks this:

template <typename container, typename randomgenerator> void scramble(container c, randomgenerator rand) { } 

i thought need able create random generator class on fly parameters given user.

only types of functions should defined in header file:

  • inline
  • template
  • static

in other cases, put definition in 1 compilation unit, , use forward declaration (prototype) in header file make visible other compilation units.


Comments

Popular posts from this blog

ios - iPhone/iPad different view orientations in different views , and apple approval process -

java Extracting Zip file -

C# WinForm - loading screen -