Compilation Error in C++ (beginner level) [duplicate] - c++

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 8 years ago.
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;
}

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,

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

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) { .. }

"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)

Related

LNK2019: unresolved external symbol _WinMain#16 referenced in function "int __cdecl invoke_main(void)" (?invoke_main##YAHXZ) [duplicate]

This question already has answers here:
c++ class why need main?
(5 answers)
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 1 year ago.
I'm new to c++ and I just don't get this error on visual studio community. I already tried so many things like changing properties settings. Like configuration Type. Also the Subsystem from Console to Windows Rewriting it. At this point I just really need help.
So this is my Header file.
#include <iostream>
#include<string>
using namespace std;
#ifndef University_h
#define University_h
class University
{
public:
University();
void setUniversityID(int);
int getUniversityID();
void setCourseIdSectId(int, int);
string getCourseIdSectId();
void setCredits(int);
int getCredits();
void setDaysTime(int, string);
string getDaysTime();
void setRoomID(int, int);
string getRoomID();
void setMaxallotment(int);
int getMaxallotment();
void setCampus(string);
string getCampus();
void setCourseInstructor(string);
string getCourseInstructor();
void setEnrollStudents(int);
int getEnrollStudents();
void setCourseStatus(string);
string getStatus();
private:
int universityIDNumber;
int DeptID;
int sectID;
int noOfCredits;
int days;
string time;
int buildingID;
int roomID;
int maxEnrollment;
string courseCampus;
string courseInstructor;
int noOfStudentsEnrolled;
string courseStatus;
};
#endif
And this is my main.cpp
#include "University.h"
University::University()
{
universityIDNumber = 0;
DeptID = 0;
sectID = 0;
noOfCredits = 0;
days = 0;
time = "00:00";
buildingID = 0;;
roomID = 0;
maxEnrollment = 0;
courseCampus = "unknown";
}
void University::setUniversityID(int ID)
{
universityIDNumber = ID;
}
int University::getUniversityID()
{
return universityIDNumber;
}
void University::setCourseIdSectId(int deptId, int sId)
{
DeptID = deptId;
sectID = sId;
}
string University::getCourseIdSectId()
{
return DeptID + " " + sectID;
}
void University::setCredits(int credits)
{
noOfCredits = credits;
}
int University::getCredits()
{
return noOfCredits;
}
void University::setDaysTime(int ds, string tm)
{
days = ds;
time = tm;
}
string University::getDaysTime()
{
return days + " " + time;
}
void University::setRoomID(int buildId, int roomid)
{
buildingID = buildId;
roomID = roomid;
}
string University::getRoomID()
{
return buildingID + " " + roomID;
}
void University::setMaxallotment(int maxNums)
{
maxEnrollment = maxNums;
}
int University::getMaxallotment()
{
return maxEnrollment;
}
void University::setCampus(string campusName)
{
courseCampus = campusName;
}
string University::getCampus()
{
return courseCampus;
}
void University::setCourseInstructor(string name)
{
courseInstructor = name;
}
string University::getCourseInstructor()
{
return courseInstructor;
}
void University::setEnrollStudents(int enrollment)
{
noOfStudentsEnrolled = enrollment;
}
int University::getEnrollStudents()
{
return noOfStudentsEnrolled;
}
void University::setCourseStatus(string status)
{
courseStatus = status;
}
string University::getStatus()
{
return courseStatus;
}
Do you remember any lectures (or sections in your text-book) telling you that all C++ programs must have a main function?
Well for Windows GUI programs this is called WinMain instead.
Unless you want a Windows GUI program you should make sure that you create a console type project in Visual Studio, and use the existing templates so Visual Studio creates a main function for you.

visual studio 2015 c++ unresolved external symbol link error [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 7 years ago.
I'm a visual studio 2015 c++ newby who's trying to write some game code at home.
I'm getting this link error:
LNK2019 unresolved external symbol "public: class std::basic_string,class std::allocator > __thiscall display_utils::fit_int_2(int)" (?fit_int_2#display_utils##QAE?AV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##H#Z) referenced in function "public: void __thiscall bat_stats::disp_bat_stats(struct bat_stats::bat_stats_typ)" (?disp_bat_stats#bat_stats##QAEXUbat_stats_typ#1##Z)
It apparently doesn't like the string I'm using to access the returned string from function fit_int_2. I've google searched for a solution, but can't find anything that fixes my problem. Note that the code compiled and linked before i I added the fit_int_2 call. Thanks in advance if you can help me out. The code is below:
bat_stats.h
#pragma once
class bat_stats
{
public:
struct bat_stats_typ
{
int gm;
int ab;
int ht;
int dbl;
int trpl;
int hr;
int rbi;
int sb;
int cs;
int bb;
int ibb;
int sf;
int sac;
int k;
int gidp;
int err;
float ave;
float slg;
float obp;
};
void disp_bat_hdr();
void disp_bat_stats( bat_stats_typ );
private:
int dummy;
};
bat_stats.cpp
#include <iostream>
using std::cout;
std::cin;
#include <string>
using std::string;
#include "bat_stats.h"
#include "display_utils.h"
void bat_stats::disp_bat_hdr()
{
cout << " G AB H 2B 3B HR RBI SB CS BB IW SF SH K GDP E AVE SLG OBP\n";
}
void bat_stats::disp_bat_stats( bat_stats_typ bat )
{
display_utils dut;
string s;
s = dut.fit_int_2( bat.gm ); // <- the problem is here!
cout << s << bat.gm << " ";
cout << bat.ab << "\n\n";
}
display_utils.h
#pragma once
#include <string>
using std::string;
class display_utils
{
public:
void insert_5_lines();
string fit_int_2( int );
private:
int dummy;
};
display_utils.cpp
#include <iostream>
using std::cout;
#include "display_utils.h"
void display_utils::insert_5_lines()
{
cout << "\n\n\n\n\n";
}
string fit_int_2(int i0)
{
string s0 = "";
if (i0 < 10)
{
s0 = " ";
}
return s0;
}
You need to change
string fit_int_2(int i0)
to
string display_utils::fit_int_2(int i0)
(You need to define the member function - currently you're defining an unrelated global function.)

How to add elements to a vector

I'm trying to add elements to a vector in my program. But I'm not sure if I'm doing it right. So far what I have below does not work. I get an error that says:
error LNK2019: unresolved external symbol "public: __thiscall MySet::MySet(void)" (??0MySet##QAE#XZ) referenced in function _main
Here is my code:
#include <iostream>
#include <map>
#include <vector>
using namespace std;
class MySet{
public:
vector<int> elements;
MySet();
void addElement(int value);
int removeElement(int index);
int sum();
int size();
};
void MySet::addElement(int value){
elements.push_back(value);
}
int main(int argc, char *argv[]){
int value;
MySet set;
cout << "Enter your numbers " << endl;
cin >> value;
while(value != -1){
set.addElement(value);
}
system("PAUSE");
}
first:
You never define your MySet ctor.Define it or remove your declaration of MySet().
second:
cin>>value out of while loop,so just input once,you maybe want write code like this:
EDIT:
while(cin >> value){
if(value==-1)
break;
set.addElement(value);
}

C++ error lnk 2019 [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 8 years ago.
I was just trying to see if I can read a text file and display but I have this error:
2 error LNK2019: unresolved external symbol "public: void __thiscall
WeatherReport::displayReport(void)"
(?displayReport#WeatherReport##QAEXXZ) referenced in function _main
Can anyone explain me what is causing this, why this is happening and how to fix this problem?
#include<fstream>
#include<iomanip>
#include<stdio.h>
#include<cmath>
#include<iostream>
using namespace std;
class WeatherReport
{
WeatherReport friend monthEnd(WeatherReport, WeatherReport);
private:
int dayofMonth;
int highTemp;
int lowTemp;
double amoutRain;
double amoutSnow;
public:
WeatherReport(int Day = 0);
void setValues(int, int, int, double, double);
void getValues();
void displayReport();
}
void WeatherReport::setValues(int dom, int ht, int lt, double ar, double as)
{
dayofMonth = dom;
highTemp = ht;
lowTemp = lt;
amoutRain = ar;
amoutSnow = as;
}
int main()
{
const int DAYS = 30;
WeatherReport day[DAYS];
WeatherReport summary;
int i = 0;
ifstream inFile;
inFile.open("WeatherTest.txt");
if (!inFile)
cout << "File not opended!" << endl;
else
{
int dom, ht, lt;
double ar, as;
while (inFile >> dom >> ht >> lt >> ar >> as)
{
day[i].setValues(dom, ht, lt, ar, as);
i++;
}
inFile.close();
for (int i = 0; i < DAYS; i++)
{
day[i].displayReport();
//read one line of data from the file
//pass the data to setValues to initialize the object
}
system("PAUSE");
return 0;
}
Your displayReport does not have a function body, and as such does not have an external symbol referencing it, hence the error.
Add a function body for displayReport, and the issue will go away:
void WeatherReport::displayReport()
{
//Place your code here.
}
The following code can be used to reproduce this error:
[header file- test.h]:
#include "StdAfx.h"
void someOtherFunction();
void someFunction(string thisVar);
[code file- test.cpp]:
#include "StdAfx.h"
#include "test.h"
void someOtherFunction()
{
printf("Hello World!");
}
[function body for someFunction(string thisVar) is missing!]
The error speaks for itself
LNK2019: unresolved external symbol "public: void __thiscall WeatherReport::displayReport(void)
It can't find the definition for WeatherReport::displayReport(). I see its declaration in your code, but there's no definition anywhere. Either you didn't write a definition, or you provided it and didn't link the .cpp file that it's in. I'm guessing the former.
Seems like displayReport() does not have a body - it is only declared, but not defined. Add the following:
void WeatherReport::displayReport()
{
//your code
}

print() not a member of class?

I am trying to run a getter/setter code in C++ Visual Studio 2008 and have my header file, implementation file and main file. In the main, there is a print function (P1.print();) which tries to print the class object P1. I get error of print() not a member of Persontype. When I declare void print(); in header, I get 3 errors -----
Persontest.obj : error LNK2005: _main already defined in proj1.obj -----
Persontest.obj : error LNK2019: unresolved external symbol "public: void __thiscall
Persontype::print(void)" (?print#Persontype##QAEXXZ) referenced in function _main ----- fatal error LNK1120: 1 unresolved externals.
Can someone pls help me resolve this issue?
Persontype.h(header file):
#ifndef H_Persontype
#define H_Persontype
#include <iostream>
#include <string>
using namespace std;
class Persontype{
public:
Persontype();
Persontype(string fn, string mn, string ln, char g);
//setter
void setfirstName(string fn);
void setmiddleName(string mn);
void setlastName(string ln);
void setGender(char g);
//getter
string getfirstName() const;
string getlastName() const;
string getmiddleName() const;
char getGender() const;
private:
string firstName;
string middleName;
string lastName;
char gender;
};
#endif/
Person.cpp(implementation file):
#include <iostream>
#include <string>
#include "Persontype.h"
using namespace std;
//default constructor
Persontype::Persontype()
{
firstName = "Me";
middleName = "My";
lastName = "Mine";
gender = 'X';
}
//specific constructor
Persontype::Persontype(string fn, string mn, string ln, char g){
firstName = fn;
middleName = mn;
lastName = ln;
gender = g;
}
//setters
void Persontype::setfirstName(string fn)
{
firstName = fn;
}
void Persontype::setmiddleName(string mn)
{
middleName = mn;
}
void Persontype::setlastName(string ln)
{
lastName = ln;
}
void Persontype::setGender(char g)
{
gender = g;
}
//getters
string Persontype::getfirstName () const
{
return firstName;
}
string Persontype::getmiddleName () const
{
return middleName;
}
string Persontype::getlastName () const
{
return lastName;
}
char Persontype::getGender() const
{
return gender;
}
Persontest.cpp(main file):
#include "Persontype.h"
int main(){
Persontype P1("tom","smith","alice",'m');
P1.print();
}
You, of course, need to both declare it in the Persontype class declaration in the header:
void print() const;
and define it in the .cpp file:
void Persontype::print(void) const
{
cout << "My name is " << getfirstName() << ", I live on the second floor\n";
}
The errors about multiple main() have nothing to do with print(), that's some other error you're doing either in code which you're not showing or in how you build your program.