0

So im newbie and im doing a school project (got really strict rules about datatypes) need to use arrays instead of vectors. and stuff like that. So im basicaly storing every gamemap type as 10x10 array and showing only size depending on userconfig file.

 template <class myType> 
 void printBoard(myType board[10][10], int size) {
    for (int j = 0; j < size; j++) {
        cout << endl;
        if (j == 0) {
            cout << "  ";
            for (int jj = 1; jj <= size; jj++)
                cout << jj << " ";
            cout << endl;
        }
        switch (j)
        {
        case 0: cout << "A "; break;
        case 1: cout << "B "; break;
        case 2: cout << "C "; break;
        case 3: cout << "D "; break;
        case 4: cout << "E "; break;
        case 5: cout << "F "; break;
        case 6: cout << "G "; break;
        case 7: cout << "H "; break;
        case 8: cout << "I "; break;
        case 9: cout << "J "; break;
        default:
            break;
        }
        for (int i = 0; i < size; i++) {
            cout << board[i][j] << " ";
        }
    }
    cout << endl;
    return;
}`

Thats my function for printing arrays of different size and type. It works well! But when i try to move function to new cpp file. i get unresolved external symbol error

Im not really sure how i should construct my header file. Ive tried different methods without success.

HEADER:

#pragma once

template <typename myType>
void printBoard(myType board[10][10],int size);

I think i need to read some more about this subject? Any help here?

  • How are you compiling? – scohe001 Mar 20 '18 at 16:40
  • btw you seem to know how templates work, why dont you make the size a template parameter? – 463035818_is_not_an_ai Mar 20 '18 at 16:42
  • you should either have the template definition in a header which you include, or in the same cpp file. Putting it into another cpp file won't work. See also here: https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file – nVxx Mar 20 '18 at 16:45
  • I have tried methods NathanOlivers link showed me but cant get my code to run from main cpp file :S – Valdemar Tuominen Mar 20 '18 at 16:56
  • And i dont really know how to edit my template so it would accept sizes of 5-10 without giving function new argument :D So any code showing me how its done is greatly respected! – Valdemar Tuominen Mar 20 '18 at 17:18

0 Answers0