0

Keep getting the following errors

"main.cpp:(.text+0x1c0): undefined reference to Student<int>::Add(int const&)'" "main.cpp:(.text+0x1fa): undefined reference toStudent::Remove()'" "main.cpp:(.text+0x21f): undefined reference to `Student::Clear()'"

Also "undefined reference to 'WinMain'

I am using classes templates and headers for a class project but I don't know where my errors are

main.cpp

#include <iostream>
#include <iomanip>
#include <string>
#include "student.h"

using namespace std;

int main()
{   Student <int> stud1;  // a student
char choice, answer;  // handles input from menu and controls loop
double score;             // the iten to be added to the end of the array

 do{
system("CLS");          // clears the screen
cout <<setw(30)<< " Main Menu\n\n\n";   // menu of options to add/remove or clear
cout << setw(15)<< " "<< "(1)- (A)dd\n\n";
cout << setw(15)<< " "<< "(2)- (R)emove \n\n";
cout << setw(15)<< " "<< "(3)- (C)lear\n\n\n";
cout << setw(35)<< "Enter Choice: ";cin >> choice;
choice = toupper(choice);
switch (choice)
{   case '1':
    case 'A':
            cout << "\nEnter score: "; cin >> score;
            if (stud1.Add(score)) // call to the add method
            {cout << score << "  Added\n\n";}
            break;
    case '2':
    case 'R':
            if(stud1.Remove())          // call to the remove method
            {cout << "\n\nRemoved\n\n";}
            break;
    case '3':
    case 'C':
            if (stud1.Clear())
            {cout << "\n\nCleared\n\n";}             // call to the clear method
            break;
 }
 cout << "Another Operation? "; cin >> answer; answer = toupper(answer);
}     while (answer == 'Y');  
cin.get();
return 0;   
}

student.cpp

#include <iostream>
#include <iomanip>
#include "student.h"

using namespace std;

// Adds a score to the end of the array
template <class T>
bool Student<T>::Add(const T &s)
{
    if (isFull()) return false;
    score[count] = s;
    count++;
    return true;
}

// Removes the last score of the array
template <class T>
bool Student<T>::Remove()
{
    if (isEmpty()) return false;
    score[count] = 0;
    return true;
}

// Clears the array of all contents
template <class T>
bool Student<T>::Clear()
{
    if (isEmpty()) return false;

    Print();

    for (count != 0; count--;)
    {
        score [count] = 0;
    }

    count = 0;

    return true;
}

// Checks if the array is full
template <class T>
const bool Student<T>::isFull()
{
    if (count == 5)
    {
        cout << "The array is full" << endl;
        return true;
    }
    return false;
}

// Checks if the array is empty
template <class T>
const bool Student<T>::isEmpty()
{
    if (count == 0)
    {
        cout << "The array is empty" << endl;
        return true;
    }
    return false;
}

// Prints the contents of the array from last to first
template <class T>
const void Student<T>::Print()
{
    cout << "Name: " << name << endl;

    for (int x = count; x != 0; x--)
    {
        cout << "Test score: " << score [x-1] << endl;
    }
}

// Explicit instantiation of class template to avoid compiler errors
template class Student<double>;
template class Student<int>;

student.h

// Class declaration
#include <string>
#include <iostream>

using namespace std;

// Guard
#ifndef STUDENT_H
#define STUDENT_H

template <class T>

class Student{
private:

    // Class data members
    string name;
    T score[5];
    int count;

public:
    // Constructor
    Student(int c = 0)
    {   
      cout << "Enter student name: ";
      cin >> name;

      count = c;
    } 

    // Destructor
    ~Student(){}

    // Member Functions
    bool Add(const T &);

    bool Remove();

    bool Clear();

    const bool isFull();

    const bool isEmpty();

    const void Print();
};

#endif
// Guard
lahr23
  • 11
  • If the WinMain error does not go away: https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix – Baum mit Augen Nov 15 '16 at 00:51
  • 1
    I think template implementation must remain in the header. – 2785528 Nov 15 '16 at 00:53
  • @DOUGLASO.MOEN Yes, but it can be separated in an other file. The OP just needs to include the cpp implementation of its template at the end of the header file, before the endif. – Papipone Nov 15 '16 at 01:52
  • @Papipone how do I implement the template at the end of the header file? by putting "template class Student;" before #endif ? – lahr23 Nov 15 '16 at 02:40
  • @Papipone - agreed. lahr23 - Generally frowned upon, but at the end of the header (before the endif), you may #include the xxxx.cpp file. It might be more acceptable if you rename the file to xxxx.inc and then #include that. I think it easier to simply cut / paste the contents of the cpp file into the header file (and while your at it, into the class Student) , and eventually delete the .cpp file. – 2785528 Nov 15 '16 at 04:15

0 Answers0