#Need help with template error that is done as an exercise from the book

42 messages · Page 1 of 1 (latest)

exotic mirage
#

i have a shuffle_deck function that shuffles an array of cards with 52 members of it and then as i read along we also add jokers and a different deck that is an array with std::array<std::variant<Card,Joker>,54> and as an exercise/optional instead of overloading the shuffle_deck function you can use function template which i still havent figured it out how to make it work

      HEADER FILE
    //void shuffle_deck(std::array < Card,52 > & deck);
    //void shuffle_deck(std::array < std::variant<Card, Joker>, 54 > & deck);
    template<typename T>
    void shuffle_deck(T & deck);

        SOURCE FILE
    //void shuffle_deck(std::array<Card, 52>& deck) {
//    std::random_device rd;
//    std::mt19937 gen{ rd() };
//    std::ranges::shuffle(deck, gen);
//}
// 
    //void shuffle_deck(std::array < std::variant<Card, Joker>, 54 >& deck) {
    //    std::random_device rd;
    //    std::mt19937 gen{ rd() };
    //    std::ranges::shuffle(deck, gen);
    //}

    template<typename T>
    void shuffle_deck(T& deck) {
        std::random_device rd;
        std::mt19937 gen{ rd() };
        std::ranges::shuffle(deck, gen);
    }

      MAIN SOURCE FILE

    int main() {

    std::array<Card,52> deck = create_deck();
    std::array<std::variant<Card, Joker>, 54> deck_with_jokers = create_extended_deck();

    shuffle_deck(deck);
    shuffle_deck(deck_with_jokers);
    ```
royal fieldBOT
#

When your question is answered use !solved or the button below to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

mystic trout
#

!code

royal fieldBOT
#
How to Format Code on Discord
Markup

```cpp
int main() {}
```

Result
int main() {}
queen sandal
#

Do you get compilation errors? If so, show them

exotic mirage
#

oh right one second

#

i get this build error Build started at 7:56 μμ...

1>main.cpp
1>playing_cards.cpp
1>Generating Code...
1>main.obj : error LNK2019: unresolved external symbol "void __cdecl cards::shuffle_deck<class std::array<class std::variant<class cards::Card,struct cards::Joker>,54> >(class std::array<class std::variant<class cards::Card,struct cards::Joker>,54> &)" (??$shuffle_deck@V?$array@V?$variant@VCard@cards@@UJoker@2@@std@@$0DG@@std@@@cards@@YAXAEAV?$array@V?$variant@VCard@cards@@UJoker@2@@std@@$0DG@@std@@@Z) referenced in function main
1>C:\Users\kanek\source\repos\CardGameReplayRetry\x64\Debug\CardGameReplayRetry.exe : fatal error LNK1120: 1 unresolved externals
1>Done building project "CardGameReplayRetry.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
========== Build completed at 7:56 μμ and took 02,997 seconds ==========```
#

and also

#

compiler error

#

cpp unresolved external symbol "void __cdecl cards::shuffle_deck<class std::array<class std::variant<class cards::Card,struct cards::Joker>,54> >(class std::array<class std::variant<class cards::Card,struct cards::Joker>,54> &)" (??$shuffle_deck@V?$array@V?$variant@VCard@cards@@UJoker@2@@std@@$0DG@@std@@@cards@@YAXAEAV?$array@V?$variant@VCard@cards@@UJoker@2@@std@@$0DG@@std@@@Z) referenced in function main

#

1 unresolved externals

#

should i format this too?

queen sandal
#

Define your template in the header

#

Yeah, you should format this too

exotic mirage
#

template<typename T> void shuffle_deck(T & deck);

#

this is inside the header file and namespace cards

mystic trout
#

Template definitions generally need to be visible by the compiler, so it must be in the header

exotic mirage
#

does it need to be outside of namespace..?

mystic trout
#

It needs to defined be in the same namespace that it's declared in

#

Just like any other symbol

exotic mirage
#

its in both namespaces

mystic trout
#

The issue is that it needs to be defined in the header

#

Has nothing to do with namespace

exotic mirage
mystic trout
#

Template definitions generally need to be visible by the compiler, so it must be in the header

queen sandal
#

Replace this ; with the function body, and remove the function definition from the .cpp file

mystic trout
#

Function templates are different than regular functions. The compiler needs to know how it's defined when it uses it, and the only way that works is if it's in the header.

exotic mirage
#

because it looks at the header first and then the other source file

#

so it needs to be in a complete package in the header?

exotic mirage
mystic trout
#

No. The compiler for main.cpp won't look in cards.cpp (or whatever) for the definition of shuffle_deck<T>

#

It will only look in the header

#

Function templates are unique in this way

exotic mirage
#

ill note it down that templates are unique and need to be defined in the header

hexed oyster
#

A template definition should be reachable from where it is used, which means those should be in the same translation unit

mystic trout
#

There's some edge cases where you can put it in the cpp file, but they're few and far between. It's a thing where you'll know it when you get experienced enough to come across it

exotic mirage
#

yes of course ill figure it out the more i practice and etc

exotic mirage
#

thank you for your very quick help and teaching me everybody 🙏🏻

#

!solved

royal fieldBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity

hexed oyster