0

I'm a new guy in C++ and I could not understand where I am wrong in this code. I take this error:

ClCompile:
1>  Student.cpp
1>Student.obj : error LNK2019: unresolved external symbol "public: void __thiscall Student::setExamGrade(int,int)" (?setExamGrade@Student@@QAEXHH@Z) referenced in function _main
1>c:\users\administrator\documents\visual studio 2010\Projects\LAB1\Debug\LAB1.exe : fatal error LNK1120: 1 unresolved externals
1>
1>Build FAILED.

Could you please help me? Code here:

Student.h

#ifndef STUDENT_H
#define STUDENT_H

#include <string>
using namespace std;

class Student
{
    private:
        int ID;
        string name;
        int *exams;
    public:
        Student();
        Student(int ID, string name);
        void setExamGrade(int index, int grade);
        int getOverallGrade();
        void display();
};
#endif

Student.cpp

#include "Student.h"
#include <iostream>
using namespace std;

int total;
int count;
int average;
int exams[3];

void main() {
    Student *s = new Student(123, "John"); 
    s->setExamGrade(0, 80); 
    s->setExamGrade(1, 60); 
    s->setExamGrade(2, 95); 
    s->display(); 
    delete s;
}

Student :: Student()
{
    ID = 0;
    name = "";
}

Student :: Student(int num, string text)
{
    this->ID = num;
    this->name = text;
}

void setExamGrade(int index, int grade)
{
    exams[index] = grade;
    total += exams[index];
    count = index +1;
}

int getOverallGrade()
{
    average = total/count;
    return average;
}

void Student :: display()
{
    cout << "ID:" << ID << "NAME:" << name << "GRADE:" << endl;
}
Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
A.D
  • 7
  • 5

4 Answers4

3

You declare the method:

void setExamGrade(int index, int grade);

Inside the class Student But you don't define the method. You do define a function with the same name.

   void setExamGrade(int index, int grade)
   {  // STUFF
   }

But that is not a method definition,

Martin York
  • 257,169
  • 86
  • 333
  • 562
2

I think you missed the Student :: before setExamGrade and getOverallGrade.

256dpi
  • 111
  • 2
1

You have it defined like so

void setExamGrade(int index, int grade) { .. }

That is just a function by itself, and it doesn't belong to a class. You want

void Student::setExamGrade(int index, int grade) { .. }
xian
  • 4,657
  • 5
  • 34
  • 38
1

"unresolved external symbol" means the body of the code in question is not found by the linker.

In this case it's the Student::setExamGrade method whose body is not found.

Your code appears to have defined a function setExamGrade but this has not been flagged as a Student:: method (in the way that you have successfully done for Student::display)

jez
  • 14,867
  • 5
  • 37
  • 64