I am using Sleep from Windows.h, and when I go to compile it is giving me these errors. I tried googling them and found nothing. Also i wouldn't expect there to be issues like this from the include files from GCC. Can anyone tell me what these errors could be from.
edit: with a lot of this code it is unfinished, but wanted to get it tested partially. I mainly just wanted an idea of the type of screw up on my end to look for because I have no idea where to start.
>g++ -o test.exe main.cpp
In file included from C:/TDM-GCC-64/x86_64-w64-mingw32/include/windows.h:65:0,
from main.cpp:3:
C:/TDM-GCC-64/x86_64-w64-mingw32/include/excpt.h:14:8: error: expected unqualified-id before string constant
extern "C" {
^
In file included from C:/TDM-GCC-64/x86_64-w64-mingw32/include/windows.h:65:0,
from main.cpp:3:
C:/TDM-GCC-64/x86_64-w64-mingw32/include/excpt.h:128:18: error: expected '}' before end of line
C:/TDM-GCC-64/x86_64-w64-mingw32/include/excpt.h:128:18: error: expected unqualified-id before end of line
C:/TDM-GCC-64/x86_64-w64-mingw32/include/excpt.h:128:18: error: expected declaration before end of line
here is the main:
#include "program/skull_ascii.h"
#include "Player.h"
//#include "Building.h"
#include "Windows.h"
#include <iomanip>
#include <stdlib.h>
#include <string>
//prototypes
void intro();
void cityScene(Player*);
Player* getUserInfo();
void scroll(std::string text);
int main(){
intro(); //flashy ascii text intro
player* user = getUserInfo();
cityScene(user);
}
void intro(){ //not done tweaking
system("mode con:cols=81 lines=30");
system("color 02");
skull_title();
Sleep(1000);
for(int i = 1; i < 15; i++){
system("color 04");
Sleep(i*i / 2);
system("color 02");
Sleep(i*i);
}
system("color 04");
system("pause");
system("cls");
system("mode con:cols=120 lines=30");
}
Player* getUserInfo(){ //not done, just enough to test
player* user = new Player("test", 56);
return user;
}
void cityScene(Player* user){ //not close to finished, but shouldn't cause any issue
std::string prompt = "arbitrary text"; // will print out with other function
system("pause");
system("cls");
std::cout << "what do you want to do?: \n"
std::cout << "\t1.Just relax its not that important\n";
std::cout << "\t2.scavenge the apartment for items\n";
std::cout << "\t3.Leave immediately, there's no time left\n";
std::cout << ":";
}
void scroll(std::string text){
for(int i = 0; i < text.length(); i++){
std::cout << text[i];
Sleep(200);
if(i%45 == 0) std::cout << "\n";
}
}
the player class is a messy container atm, and isn't done
here is the player class:
#pragma once
#include <string>
#include <list>
#include <iostream>
#include "Item.h"
class Player{
std::string name;
int age;
std::list<Item> inventory;
public:
Player(std::string n, int a){
this->name = n;
this->age = a;
}
/* F-name: updateScreen()
params: none
return: void
purpos: updates the play screen with current player information
*/void update(){
system("cls"); // clear screen
std::cout << "NAME:" << name << " |\
AGE:XXX | \
WEAPONS: weapon 1, weapon2 | \
PACK_WEIGHT:XXXXXXXKgs | \
PARTY: name1, name2, name3, name4 | \
LOCATION: big city";
}
/*
F-name: addItem()
params: item object to add
return: void
purpos: adds an item to the inventory list
*/ void addItem(Item newItem){}
#pragma once
#include <iostream>
#include <string>
class Item{
std::string name;
std::string desc;
public:
Item(){
name = "void";
desc = "void";
}
Item(const Item& copy){
this->name = copy.name;
this->desc = copy.desc;
}
Item(std::string n, std::string d){
name = n;
desc = d;
}
~Item(){}
friend std::ostream& operator<< (std::ostream& out, const Item& thing){
out << thing.name;
return out;
}
void setName(std::string n){name = n;}
void setDesc(std::string d){desc = d;}
std::string getName(){return name;}
std::string getDesc(){return desc;}
};
The Player class definition in Player.h is not closed correctly. You forgot to add:
};
at the end. Right now, this causes the next header you include to cause a compilation error.
Related
I'm trying to create a function below in my CreateReport class called load() that copies all the records (data) from my graduate.dat file into my static vector of Record pointers called primaryCollection. I created a Record class with variables that make up each Record, and in my load() function in createReport.cc I attempted to read each line in the file, create a Record object with each line, add it to my vector, and then print everything in primaryCollection.
The problem is every time I attempt to use primaryCollection, I keep getting the error:
CreateReport.o: In function `CreateReport::CreateReport()':
CreateReport.cc:(.text+0x43): undefined reference to `CreateReport::primaryCollection'
CreateReport.o: In function `CreateReport::load()':
CreateReport.cc:(.text+0x2ac): undefined reference to `CreateReport::primaryCollection'
CreateReport.cc:(.text+0x31d): undefined reference to `CreateReport::primaryCollection'
CreateReport.cc:(.text+0x32f): undefined reference to `CreateReport::primaryCollection'
I get 4 undefined references for the 4 times I mention primaryCollection in createReport.cc. I'm not sure if I'm initializing primaryCollection correctly and if that is whats causing these undefined references. I don't know if this is relevant to my problem, but CreateReport is also an abstract class and has a few subclasses called ReportOne, ReportTwo, etc.
primaryCollection is supposed to be a static vector of Record pointers and I'm also not allowed to use std::map for this task.
I would appreciate any help with this issue. I looked at this post Undefined reference to static variable c++ but I still don't really understand what to do. I'm not allowed to make global variables and I'm dealing with a collection rather than a single variable.
My graduate.dat file is formatted like below in the format < year province degree >
2000 AB Bachelor's
2005 AB Bachelor's
2005 MB College
Each line basically represents a Record. So the first record here is 2000 AB Bachelor's
EDIT: So I made changes to my code based on the comments by adding the line vector<Record*> CreateReport::primaryCollection; above my constructor, but it gives me the error:
CreateReport.cc:13:34: error: conflicting declaration ‘std::vector<Record*> CreateReport::primaryCollection’
vector<Record*> CreateReport::primaryCollection;
^~~~~~~~~~~~~~~~~
In file included from CreateReport.cc:5:0:
CreateReport.h:23:33: note: previous declaration as ‘std::vector<Record*>* CreateReport::primaryCollection’
static std::vector<Record*>* primaryCollection; //STL vector of record pointers
^~~~~~~~~~~~~~~~~
CreateReport.cc:13:34: error: declaration of ‘std::vector<Record*>* CreateReport::primaryCollection’ outside of class is not definition [-fpermissive]
vector<Record*> CreateReport::primaryCollection;
Any ideas how to fix this?
Record.h
#ifndef RECORD_H
#define RECORD_H
#include <iostream>
#include <string>
class Record{
public:
Record(int = 0, string = "", string = "");
~Record();
private:
int year;
string province;
string degree;
};
#endif
Record.cc
#include <iostream>
#include <string>
using namespace std;
#include "Record.h"
Record::Record(int i1, string s1, string s2) : year(i1), province(s1), degree(s2){}
Record::~Record(){}
CreateReport.h
#ifndef CREATEREPORT_H
#define CREATEREPORT_H
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <iterator>
#include <algorithm>
#include <cstdlib>
#include "Record.h"
class CreateReport{
public:
CreateReport();
static void load();
protected:
static vector<Record*> primaryCollection; //STL vector of record pointers
};
#endif
CreateReport.cc
#include <iostream>
using namespace std;
#include <string>
#include "CreateReport.h"
vector<Record*> CreateReport::primaryCollection;
CreateReport::CreateReport(){
}
void CreateReport::load(){
int year;
string province, degree;
ostream_iterator<Record*> outItr(cout);
ifstream infile("graduate.dat", ios::in);
if (!infile) {
cout << "Error: could not open file" << endl;
exit(1);
}
while (infile >> year >> province >> degree) { //as long as were not at end of file
Record* record = new Record(year, province, degree); //create Record object with this data
primaryCollection->push_back(record); //undefined reference
}
cout<<endl<<"List of Records:"<<endl;
copy(primaryCollection->begin(), primaryCollection->end(), outItr); //2 undefined references
}
Second version using `Record*` for `std::vector primaryCollection`.
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
using namespace std;
class Record{
public:
Record(int = 0, string = "", string = "");
~Record()=default;
friend std::ostream& operator<<(std::ostream&, const Record&);
private:
int year;
string province;
string degree;
};
// **** output overload for Record ***********
std::ostream& operator<<(std::ostream& os, const Record& rd)
{
os << "year = " << rd.year << " prov = " << rd.province << " degree = " << rd.degree << std::endl;
return os;
}
// ****** end of output overload **************
Record::Record(int i1, string s1, string s2) : year(i1), province(s1), degree(s2){}
//end of Record.cc
//
class CreateReport{
public:
CreateReport() = default;
void load();
protected:
static vector<Record*> primaryCollection; //STL vector of record pointers
};
//***************** you need this line ********************
std::vector<Record*> CreateReport::primaryCollection;
//*********************************************************
void CreateReport::load(){
int year;
string province, degree;
ifstream infile("graduate.dat", ios::in);
if (!infile) {
cout << "Error: could not open file" << endl;
exit(1);
}
while (infile >> year >> province >> degree) {
Record *a = new Record(year, province, degree);
primaryCollection.push_back( a );
}
cout<<endl<<"List of Records:"<<endl;
for (int i = 0; i<primaryCollection.size(); ++i ) std::cout << *primaryCollection[i];
}
int main()
{
CreateReport mime;
mime.load();
}
Three major problems:
Using std::vector<*Record> cause many un-necessary difficulties;
For static member vector, a extra definition outside the class is necessary.std::vector<Record> CreateReport::primaryCollection;. This erases the undefined error message.
Using copy to std::cout doesn't work, it provide no method of printing Record. Suggest to write a output overload.
Based on these, I provide a version as follows (mixed all headers together.)
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
using namespace std;
class Record{
public:
Record(int = 0, string = "", string = "");
~Record()=default;
friend std::ostream& operator<<(std::ostream&, const Record&);
private:
int year;
string province;
string degree;
};
// **** output overload for Record ***********
std::ostream& operator<<(std::ostream& os, const Record& rd)
{
os << "year = " << rd.year << " prov = " << rd.province << " degree = " << rd.degree << std::endl;
return os;
}
// ****** end of output overload **************
Record::Record(int i1, string s1, string s2) : year(i1), province(s1), degree(s2){}
//end of Record.cc
//
class CreateReport{
public:
CreateReport() = default;
void load();
protected:
static vector<Record> primaryCollection;
};
//***************** you need this line ********************
vector<Record> CreateReport::primaryCollection;
//*********************************************************
void CreateReport::load(){
int year;
string province, degree;
ifstream infile("graduate.dat", ios::in);
if (!infile) {
cout << "Error: could not open file" << endl;
exit(1);
}
while (infile >> year >> province >> degree) {
primaryCollection.push_back( Record(year, province, degree) );
}
cout<<endl<<"List of Records:"<<endl;
for (int i = 0; i<primaryCollection.size(); ++i ) std::cout << primaryCollection[i];
}
int main()
{
CreateReport mime;
mime.load();
}
I was doing homework for a C++ game that we have to do from CO SCI 136 class and the instructions states:
Modify your Homework 4 Problem 1 solution thus:
Replace the array with a dynamic array
Read the winning points M from a file
Read the number of players N from a file
Read the player names from a file.
I am using Visual Studio 2017 and I am having trobule with these errors:
Error C2664 'void Player::setName(std::string &)': cannot convert argument 1 from 'const std::string' to 'std::string &' player.cpp 7
Error C2511 'void Player::setName(const std::string &)': overloaded member function not found in 'Player' player.cpp 18
Error C2597 illegal reference to non-static member 'Player::name' player.cpp 19
Is there any way to fix these errors?
Here are my codes of
player.h
#pragma once
#include <string>
using namespace std;
class Player
{
private:
string name;
int points;
bool skipturn = false;
public:
Player(const string& new_name = "No Name");
string getName() const;
int getPoints() const;
void setName(string& new_name);
void setPoints(int new_points);
void setLossHalfPoints();
void setSkipTurn(bool isSkip);
bool isSkipTurn();
};
player.cpp
#include <string>
using namespace std;
#include "player.h"
Player::Player(const string& new_name)
{
setName(new_name);
}
string Player::getName() const
{
return name;
}
int Player::getPoints() const
{
return points;
}
void Player::setName(const string& new_name)
{
name = new_name;
}
void Player::setPoints(int new_points)
{
points = new_points;
}
void Player::setLossHalfPoints()
{
this->points /= 2;
}
void Player::setSkipTurn(bool isSkip)
{
this->skipturn = isSkip;
}
bool Player::isSkipTurn()
{
return this->skipturn;
}
source.cpp
#include "stdafx.h"
#include <iostream>
#include <string>
#include <ctime>
#include <iomanip>
#include <random>
#include<fstream>
#include "player.h"
using namespace std;
int main()
{
int M;
int N;
Player *player; //Declaring as a dynamic array
player = new Player[N];
string *names;
names = new string[N];
ifstream file, file1, file2; //opening the file in read mode
string line;
file.open("Mdata.dat");
file >> M; //Reading the M data from the file
file.close();
file1.open("Ndata.dat");
file1 >> N; //Reading the N data from the file
file1.close();
file2.open("names.dat");
if (file2.is_open()) //if the file is open
{
while (!file2.eof()) //while the end of file is NOT reached
{
getline(file2, line); //get one line from the file
for (int i = 0; i<N; i++)
{
names[i] = line; //reading names from file into names array
}
}
file2.close();
}
for (int i = 0; i < N; i++) //setting the player names from names array
{
player[i].setName(names[i]); player[i].setPoints(0);
}
default_random_engine dre(17890714);
uniform_int_distribution<int> player_uid(0, N - 1);
uniform_int_distribution<int> dice_uid(1, 6);
int index = player_uid(dre);
do
{
index = (index + 1) % N;//implements circular array
if (player[index].isSkipTurn())
{
cout << player[index].getName() << '/' << setw(2) << "skip turn" << endl;
player[index].setSkipTurn(false);// clear skip turn
index = (index + 1) % N;//implements circular array
}
int die1 = dice_uid(dre);
int die2 = dice_uid(dre);
int points = player[index].getPoints();
player[index].setPoints(points + die1 + die2);
if (player[index].getPoints() > M)
{
player[index].setLossHalfPoints();// set half of then points
player[index].setSkipTurn(true);// set skip turn
cout << player[index].getName() << '/' << setw(2) << player[index].getPoints() << '/' << setw(2) << player[index].getPoints() * 2 << endl;
}
else {
cout << player[index].getName() << '/' << setw(2) << die1 + die2 << '/' << setw(2) << player[index].getPoints() << endl;
}
} while (player[index].getPoints() != M);
cout << player[index].getName() << " wins" << endl;
system("pause");
return 0;
}
Let's look at the compiler error.
Error C2664 'void Player::setName(std::string &)': cannot convert argument 1 from 'const std::string' to 'std::string &' player.cpp 7
It is complaining that it cannot convert const std::string to std::string when calling the setName() function at line 7 of player.cpp. But we have a setName function in the player class, right? What's wrong?
If you look at it more carefully in your Player.h, the declaration of the function setName is missing the const attribute.
void setName(string& new_name);
If you add const to this that will solve it.
The question:
Why is the following error happening?
definition of implicity-declared 'Clothing::Clothing()
The context:
As an assignment I have to do constructors, destructors and methods in a class Clothing. I'm having a problem when I try to define the constructor in clothing.cpp. I have read that the problem is because I did not declare the constructor in clothing.h, but I think how I have done it, it's declared. I have no clue where the problem lies.
My code:
clothing.h:
#ifndef CLOTHING_H_
#define CLOTHING_H_
#include <string>
#include <iostream>
using namespace std;
class Clothing {
private:
int gender;
int size;
string name;
public:
Clothing();
Clothing(const Clothing &t);
Clothing(int gender, int size, string name);
~Clothing();
int getGender();
int getSize();
string getName();
void setGender(int gender1);
void setSize(int size1);
void setName(string name1);
void print();
void toString();
};
#endif /* CLOTHING_H_ */
clothing.cpp:
#include <iostream>
#include "clothing.h"
#include <string>
#include <sstream>
using namespace std;
Clothing::Clothing() :
gender(1),
size(1),
name("outofstock") {
}
Clothing::Clothing(const Clothing& t) :
gender(t.gender),
size(t.size),
name(t.name) {
}
Clothing::Clothing(int gender, int size, string name) {
}
int Clothing::getGender() {
return gender;
}
int Clothing::getSize() {
return size;
}
string Clothing::getName() {
return name;
}
void Clothing::setGender(int gender1) {
gender = gender1;
}
void Clothing::setSize(int size1) {
size = size1;
}
void Clothing::setName(string name1) {
name = name1;
}
void Clothing::print() {
cout << name << " " << gender << " " << size << endl;
}
void Clothing::toString() {
stringstream ss;
ss << name << " " << gender << " " << size;
cout << ss.str();
}
Errors: \src\clothing.cpp:7:21: error: definition of implicitly-declared 'Clothing::Clothing()'
\src\clothing.cpp:14:37: error: definition of implicitly-declared 'Clothing::Clothing(const Clothing&)'
The error is: you declared a destructor but you didn't define it. Add a definition for the destructor or define it as default:
#ifndef CLOTHING_H_
#define CLOTHING_H_
#include <string>
#include <iostream>
using namespace std;
class Clothing {
private:
int gender;
int size;
string name;
public:
Clothing();
Clothing(const Clothing &t);
Clothing(int gender, int size, string name);
~Clothing() = default; // <-- add a default destructor
int getGender();
int getSize();
string getName();
void setGender(int gender1);
void setSize(int size1);
void setName(string name1);
void print();
void toString();
};
#endif /* CLOTHING_H_ */
After fixing this your code snippet works: tio.run
If you have more problems with your code, the problems are outside of your provided code snippet.
This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 4 years ago.
My professor gave me two class header and .cpp files to build on. When I include these in main, they work fine. Whenever I just use his files, I get linker errors with clang and xcode.
Here's the error:
shannigan#mbp-007100 inheritance (master) $ make main
c++ main.cpp -o main
Undefined symbols for architecture x86_64:
"SavitchEmployees::SalariedEmployee::SalariedEmployee()", referenced from:
_main in main-0d7e27.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [main] Error 1
Here's my main:
#include "employee.h"
#include "salariedemployee.h"
#include <string>
#include <cstdlib>
#include <iostream>
using namespace SavitchEmployees;
using namespace std;
int main() {
cout << "Do I run?" << endl;
SalariedEmployee sam;
return 0;
};
The header file for Employee:
//This is the header file employee.h.
//This is the interface for the class Employee.
//This is primarily intended to be used as a base class to derive
//classes for different kinds of employees.
#ifndef EMPLOYEE_H
#define EMPLOYEE_H
#include <string>
using std::string;
namespace SavitchEmployees
{
class Employee
{
public:
Employee( );
Employee(const string& theName, const string& theSsn);
string getName( ) const;
string getSsn( ) const;
double getNetPay( ) const;
void setName(const string& newName);
void setSsn(const string& newSsn);
void setNetPay(double newNetPay);
void printCheck( ) const;
protected:
string name;
string ssn;
double netPay;
};
}//SavitchEmployees
#endif //EMPLOYEE_H
The CPP file for main:
//This is the file: employee.cpp
//This is the implementation for the class Employee.
//The interface for the class Employee is in the header file employee.h.
#include <string>
#include <cstdlib>
#include <iostream>
#include "employee.h"
using std::string;
using std::cout;
namespace SavitchEmployees
{
Employee::Employee( ) : name("No name yet"), ssn("No number yet"), netPay(0)
{
//deliberately empty
}
Employee::Employee(const string& theName, const string& theNumber)
: name(theName), ssn(theNumber), netPay(0)
{
//deliberately empty
}
string Employee::getName( ) const
{
return name;
}
string Employee::getSsn( ) const
{
return ssn;
}
double Employee::getNetPay( ) const
{
return netPay;
}
void Employee::setName(const string& newName)
{
name = newName;
}
void Employee::setSsn(const string& newSsn)
{
ssn = newSsn;
}
void Employee::setNetPay (double newNetPay)
{
netPay = newNetPay;
}
void Employee::printCheck( ) const
{
cout << "\nERROR: printCheck FUNCTION CALLED FOR AN \n"
<< "UNDIFFERENTIATED EMPLOYEE. Aborting the program.\n"
<< "Check with the author of the program about this bug.\n";
exit(1);
}
}//SavitchEmployees
SalariedEmployees header:
//This is the header file salariedemployee.h.
//This is the interface for the class SalariedEmployee.
#ifndef SALARIEDEMPLOYEE_H
#define SALARIEDEMPLOYEE_H
#include <string>
#include "employee.h"
using std::string;
namespace SavitchEmployees
{
class SalariedEmployee : public Employee
{
protected:
double salary;//weekly
public:
SalariedEmployee( );
SalariedEmployee (const string& theName, const string& theSsn,
double theWeeklySalary);
double getSalary( ) const;
void setSalary(double newSalary);
void printCheck( );
};
}//SavitchEmployees
#endif //SALARIEDEMPLOYEE_H
SalariedEmployee.cpp:
//This is the file salariedemployee.cpp
//This is the implementation for the class SalariedEmployee.
//The interface for the class SalariedEmployee is in
//the header file salariedemployee.h.
#include <iostream>
#include <string>
#include "salariedemployee.h"
using std::string;
using std::cout;
using std::endl;
namespace SavitchEmployees
{
SalariedEmployee::SalariedEmployee( ) : Employee( ), salary(0)
{
//deliberately empty
}
SalariedEmployee::SalariedEmployee(const string& newName, const string& newNumber,
double newWeeklyPay)
: Employee(newName, newNumber), salary(newWeeklyPay)
{
//deliberately empty
}
double SalariedEmployee::getSalary( ) const
{
return salary;
}
void SalariedEmployee::setSalary(double newSalary)
{
salary = newSalary;
}
void SalariedEmployee::printCheck( )
{
setNetPay(salary);
cout << "\n__________________________________________________\n";
cout << "Pay to the order of " << getName( ) << endl;
cout << "The sum of " << getNetPay( ) << " Dollars\n";
cout << "_________________________________________________\n";
cout << "Check Stub NOT NEGOTIABLE \n";
cout << "Employee Number: " << getSsn( ) << endl;
cout << "Salaried Employee. Regular Pay: "
<< salary << endl;
cout << "_________________________________________________\n";
}
}//SavitchEmployees
How can I get rid of these linker errors so I can focus on my actual code? Is there anything obvious wrong? The only thing I've changed was making the "private" variables protected.
I can't see the class named SalariedEmployee.
I think the main function should look like this.
int main() {
cout << "Do I run?" << endl;
Employee sam;
return 0;
};
You have to use Employee instead of SalariedEmployee
I'm trying to write a program that takes the grades and prints out the following:
ID:123 NAME:John GRADE:78
but instead I'm getting:
ID:-842150451 NAME: GRADE: 78
Can you guys help me and give me some extra tips to make my code cleaner since I'm fairly new to C++.
Student.h
#ifndef STUDENT_H
#define STUDENT_H
#include <iostream>
#include <string>
using namespace std;
class Student {
public:
Student(int num, string text);
int getID();
void setExamGrade(int a, int b);
int getOverallGrade();
void display();
string getName();
string name;
int id;
int exams[3];
int sum;
int average;
};
#endif
Student.cpp
#ifndef STUDENT_CPP
#define STUDENT_CPP
#include "Student.h"
#include <iostream>
#include <string>
using namespace std;
Student::Student(int num, string text)
{
num = id;
text = name;
exams[0, 1, 2] = 0;
}
int Student::getID() {
return id;
}
string Student::getName() {
return name;
}
void Student::setExamGrade(int a, int b) {
exams[a] = b;
}
int Student::getOverallGrade() {
sum = exams[0] + exams[1] + exams[2];
average = sum / 3;
return average;
}
void Student::display() {
cout << "ID: " << getID();
cout << " NAME: " << getName();
cout << " GRADE: " << getOverallGrade() << endl;
}
#endif
gradebook.cpp
#ifndef GRADEBOOK_CPP
#define GRADEBOOK_CPP
#include "Student.h"
#include <iostream>
using namespace std;
int main() {
Student *s = new Student(123, "John");
s->setExamGrade(0, 80);
s->setExamGrade(1, 60);
s->setExamGrade(2, 95);
s->display();
delete s;
return 0;
}
#endif
You never assign to id in the constructor, hence it's uninitialized and you will have undefined behavior when you print it.
Change
num = id;
to
id = num;
Same with the name.
Also, the statement
exams[0, 1, 2] = 0;
doesn't do what you expect it to do, it only initializes exams[2] to sero, and leaves the rest uninitialized. The expression 0, 1, 2 uses the comma operator.
Either assign to all members of the array separately, or use a constructor member initializer list (which I recommend for all the initialization).