Getting the linker error for following c++ code [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 7 years ago.
I recently started learning object oriented programming..for the following code, I am getting the linker error !!
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
class person{
public:
string name;
person();
person(string n){
name = n ;
}
void setName(string k){
name = k;
}
string getName(){
return name;
}
};
class student : public person {
public:
string major;
void setMajor(string m){
major = m;
}
string getMajor(){
return major;
}
};
class faculty : public person{
public:
string department;
faculty(string dept){
department = dept;
}
void setDepartment(string depart){
department = depart;
}
string getDepartment(){
return department;
}
};
int main() {
student s;
s.setName("james");
s.setMajor("computer science");
string p = s.getName();
string p2 = s.getMajor();
cout << "student name and mjor is :" << p << p2 << endl;
faculty f("nanotech");
f.setName("chris");
f.setDepartment("electrical");
string f1 = f.getName();
string f2 = f.getDepartment();
cout << "facult name and department :" << f1 << f2 << endl;
return 0;
}
The following error i am getting , when i try to compile the code.
/tmp/ccYHu2de.o: In function `faculty::faculty(std::string)':
person.cpp:(.text._ZN7facultyC2ESs[_ZN7facultyC5ESs]+0x19): undefined reference to `person::person()'
/tmp/ccYHu2de.o: In function `student::student()':
person.cpp:(.text._ZN7studentC2Ev[_ZN7studentC5Ev]+0x15): undefined reference to `person::person()'
collect2: error: ld returned 1 exit status

In your class person you have a constructor that is declared but is not defined. The compiler let you compile the class but then the linker looks for an implementation (which is called forward declaration):
class person{
public:
string name;
person(); //constructor only declared
person(string n){
name = n ;
}
.....
};
The problem is that you inherit from person in your student and faculty, you use the default constructor but you never implement it. Try to add an implementation and the linker error will disappear:
class person{
public:
string name;
person(){} //constructor implemented
person(string n){
name = n ;
}
.....
};

The Problem is that you need to implement the constructor of the person class, Try this code: http://ideone.com/NfXP7R
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
class person{
public:
string name;
person() {
}
person(string n){
name = n ;
}
void setName(string k){
name = k;
}
string getName(){
return name;
}
};
class student : public person {
public:
string major;
void setMajor(string m){
major = m;
}
string getMajor(){
return major;
}
};
class faculty : public person{
public:
string department;
faculty(string dept){
department = dept;
}
void setDepartment(string depart){
department = depart;
}
string getDepartment(){
return department;
}
};
int main() {
student s;
s.setName("james");
s.setMajor("computer science");
string p = s.getName();
string p2 = s.getMajor();
cout << "student name and mjor is :" << p << p2 << endl;
faculty f("nanotech");
f.setName("chris");
f.setDepartment("electrical");
string f1 = f.getName();
string f2 = f.getDepartment();
cout << "facult name and department :" << f1 << f2 << endl;
return 0;
}

Related

Why may a class trying to change a value in a friendly class's field be triggernig an error?

So I've been learning how friendly classes work, and I created two classes, both being friendly to one another. Howewer, as soon as I write a method to modify a value in another class's field, I get a compilator error, and Visual Studio refuses to elaborate on how to fix it. Here is the code:
#include <iostream>
#include <cstring>
using namespace std;
class Human;
class Apple;
class Human
{
public:
Human();
Human(int bitepower, string& name);
void BiteApple(Apple& other)
{
other.weight -= this->bitepower;
}
void Print()
{
cout << "Имя = " << this->name << " Сила укуса = " << this->bitepower << " id = " << this->id << endl;
}
~Human();
private:
friend Apple;
int bitepower;
string name;
int id;
static int num;
};
Human::Human()
{
bitepower = 0;
num++;
id = num;
name = string("Unidentified " + id);
}
Human::Human(int test, string& name)
{
this->bitepower = test;
num++;
id = num;
this->name = name;
}
int Human::num = 0;
Human::~Human()
{
}
class Apple
{
public:
Apple();
Apple(int weight);
void Print()
{
cout << "Apple id = " << this->id << " Weight = " << this->weight << endl;
}
~Apple();
private:
friend Human;
int weight;
static int num;
int id;
};
Apple::Apple()
{
weight = 0;
num++;
id = num;
}
Apple::Apple(int weight)
{
this->weight = weight;
num++;
id = num;
}
int Apple::num = 0;
Apple::~Apple()
{
}
int main()
{
//Apple apple1(80);
//Human egor(20, "Егор");
//egor.Print();
//apple1.Print();
//egor.BiteApple(apple1);
//egor.Print();
//apple1.Print();
return 0;
}
And this is the exact method that causes everything to stop working:
void BiteApple(Apple& other)
{
other.weight -= this->bitepower;
}
After I've localised the problem, I've tried moving the announcements of the classes around, and outlining the very function to be friendly, but to no effect. I just can't seem to find the answer nowhere.
Here is the exact error I get:enter image description here
This reads as: Compilation error occured. Continue and load the last succsessfully built version?
I have also tried moving the declaration of Apple class below the declaration of Human class as suggested, but it had no effect. I would be grateful to be advised if this can be solved without creating a header file as I have not learned this yet, and if not, I'll need to learn file structure and return to friendly classes later.

Diamond problem with Multiple inheritance C++

I have a homework task with a given main.cpp code which is not allowed to be changed. According to that main.cpp and simple input and output(which is down below) example I must to finish the program.
My tries are: I'm trying to create 4 classes, class Person; class Worker; class Student; class InService; in my main function through instantiating an object of InService class I pass 4 parameters (name, sex, studentNo, workerNo); and with help of pointer of type of Base class, have the desired output. The error it shows is:
[Error] no unique final overrider for 'virtual std::string Person::getName()' in 'InService'
[Error] no unique final overrider for 'virtual int Person::getSex()' in 'InService'
I've tried to use virtual inheritance for that, but I can't really figure out how to solve this problem. I did some research on virtual inheritance, and referenced to other experts answers, but still getting confused with whole OOP stuff.
//Inservice.h
#include<string>
using namespace std;
class Person{
public:
Person();
~Person();
string name;
int sex;
virtual string getName() = 0;
virtual int getSex() = 0;
};
///////////////////////////////////////////////////
class Student:virtual public Person{
public:
Student();
~Student();
string sno;
virtual string getName() {
return name;
}
virtual int getSex(){
return sex;
}
string getSno(){
return sno;
}
};
//////////////////////////////////////////////////
class Worker:virtual public Person{
public:
Worker();
~Worker();
string wno;
virtual std::string getName(){
return name;
}
virtual int getSex(){
return sex;
}
string getWno(){
return wno;
}
};
///////////////////////////////////////////////////////
class InService: public Student, public Worker{
public:
InService(string _name, int _sex, string _sno, string _wno){
Person::name = _name;
Person::sex - _sex;
Worker::wno = _wno;
Student::sno = _sno;
}
};
///////////////////////////////////////////////////////
//main.cpp
#include <iostream>
#include "inservice.h"
using namespace std;
int main() {
string name, sno, wno;
int sex;
cin >> name;
cin >> sex;
cin >> sno;
cin >> wno;
InService is(name, sex, sno, wno);
Person* p = &is;
Student* s = &is;
Worker* w = &is;
cout << p->getName() << endl;
cout << p->getSex() << endl;
cout << s->getName() << endl;
cout << s->getSex() << endl;
cout << s->getSno() << endl;
cout << w->getName() << endl;
cout << w->getSex() << endl;
cout << w->getWno() << endl;
return 0;
}
Suppose my input is:
Jack
1 //1-for male; 0 -for female
12345678 //studentNo
87654321 //workerNo
I expect the output to be:
Jack
1
12345678
Jack
1
87654321
InService(string _name, int _sex, string _sno, string _wno){
Person::name = _name;
Person::sex - _sex;
Worker::wno = _wno;
Student::sno = _sno;
}
There's a typo there, Person::sex - _sex; should be Person::sex = _sex;
Also you can remove name and sex virtual function and have it just a standard function in Person, since it's exactly the same for all classes that derive from it. That will remove the ambiguity of which getName and getSex function that InService class virtual table needs to point to.

C++ Class variables not printing the right values assigned

I'm trying to create a simple class called 'Game' and assign some values to all three variables. However every time I run it, the values printed at the screen are completely irrelevant, and I'm pretty sure it has to do something with the class constructors but I don't know what exactly.The code is this:
#include <iostream>
#include <string>
using namespace std;
class Game
{
int id;
string name;
string winner;
public:
Game();
Game(int IDvalue, string NAMEvalue );
~Game();
void setId(int IDvalue);
void setName(string NAMEvalue);
void setWinner(string WINNERvalue);
int getId();
string getName();
string getWinner();
void status1();
};
Game::Game()
{
id = 0;
name = " ";
winner = " ";
}
Game::Game(int IDvalue, string NAMEvalue)
{
IDvalue = id;
NAMEvalue = name;
winner = " ";
}
Game::~Game()
{
}
void Game::setId(int IDvalue)
{
IDvalue = id;
}
void Game::setName(string NAMEvalue)
{
NAMEvalue = name;
}
void Game::setWinner(string WINNERvalue)
{
WINNERvalue = winner;
}
int Game::getId()
{
return id;
}
string Game::getName()
{
return name;
}
string Game::getWinner()
{
return winner;
}
void Game::status1()
{
cout << "Game's id : " << id << endl;
cout << "Game's type : " << name << endl;
cout << "Game's winner : " << winner << endl;
}
int main()
{
Game a(1, "Something");
a.setWinner("Someone");
a.status1();
return 0;
}
As you might have noticed (I'm sure you have) I'm pretty new to C++ ,so go easy on me...
Game::Game(int IDvalue, string NAMEvalue)
{
IDvalue = id;
NAMEvalue = name;
winner = " ";
}
Most of these assignments are backwards. To be consistent, the last line should have been
" " = winner;
This constructor isn't initializing id and name; instead it's assigning the (garbage) values of id and name to the parameters IDvalue and NAMEvalue, which are local variables in the function, so they're destroyed when the constructor returns and no one can see their modified values.
Fix:
Game::Game(int IDvalue, string NAMEvalue)
{
id = IDvalue;
name = NAMEvalue;
winner = " ";
}
Or better:
Game::Game(int IDvalue, string NAMEvalue)
: id(IDvalue)
, name(NAMEvalue)
, winner(" ")
{
}
By the way, your setter functions have the same problem.
You are assigning the member variables to the parameters instead of vice versa on the second constructor and set functions.
Game::Game(int IDvalue, string NAMEvalue)
{
IDvalue = id; --> id = IDvalue; OR setID(id);
NAMEvalue = name; --> name = NAMEvalue; OR setName(name);
winner = " ";
}
void Game::setId(int IDvalue)
{
IDvalue = id; --> id = IDvalue;
}
// ... the rest of the set functions

Storing Objects in an Array of a second Object

I have to create a small console application in C++ which will do the following:
Create class Subject which has next attributes: name of the subject, number of students and array of students who are attending that subject. After that createa class Student which has name and surname of the student as attributes. In Main file count how many duplicate names there are in each Subject.
I have few problems here. First one is that I don't know how to initialize an array in my Subject.h file. Second is how to actually put Student objects into Subject objects and compare the names in the end.
What I'd like output to look like:
Duplicate names in subjectA are: Michael.
Duplicate names in subjectB are: Nicholas, John.
Where subjectA and subjectB should be called C++ and C.
Here is my code so far (I googled for past hour or two about this problem of mine but I just couldn't find a proper answer/example).
NOTE: I'm including all these files for clarification.
Subject.h
#include <string>
#include <iostream>
using namespace std;
/*
* I should also have an array named `arrayOfStudents`
* which should store all students who are attending
* that Subject.
*/
class Subject
{
public:
Subject();
Subject(Subject &subject);
Subject(string nameOfSubject, int numberOfStudents);
~Subject();
const string getNameOfSubject();
const int getNumberOfStudents();
void setNameOfSubject(string nameOfSubject);
void setNumberOfStudents(int numberOfStudents);
void print();
private:
string nameOfSubject;
int numberOfStudents;
};
Subject.cpp
#include <iostream>
#include "Subject.h"
using namespace std;
Subject::Subject()
{
}
Subject::Subject(string nameOfSubject, int numberOfStudents)
{
this->nameOfSubject = nameOfSubject;
this->numberOfStudents = numberOfStudents;
}
Subject::Subject(Subject &Subject) : nameOfSubject(Subject.getNameOfSubject()), numberOfStudents(Subject.getNumberOfStudents())
{
}
Subject::~Subject()
{
cout << "Object is destroyed" << endl;
}
const string Subject::getNameOfSubject()
{
return nameOfSubject;
}
const int Subject::getNumberOfStudents()
{
return numberOfStudents;
}
void Subject::setNameOfSubject(string nameOfSubject)
{
nameOfSubject = this->nameOfSubject;
}
void Subject::setNumberOfStudents(int numberOfStudents)
{
numberOfStudents = this->numberOfStudents;
}
void Subject::print()
{
cout << "Subject: " << nameOfSubject << " :: Number of students: " << numberOfStudents << endl;
}
Student.h
#include <string>
#include <iostream>
using namespace std;
class Student
{
public:
Student();
Student(Student &student);
Student(string name, string surname);
~Student();
const string getName();
const string getSurname();
void setName(string name);
void setSurname(string surname);
void print();
private:
string name;
string surname;
};
Student.cpp
#include <iostream>
#include "Student.h"
using namespace std;
Student::Student()
{
}
Student::Student(string name, string surname)
{
this->name = name;
this->surname = surname;
}
Student::Student(Student &student) : name(student.getName()), surname(student.getSurname())
{
}
Student::~Student()
{
cout << "Object is destroyed" << endl;
}
const string Student::getName()
{
return name;
}
const string Student::getSurname()
{
return surname;
}
void Student::setName(string name)
{
name = this->name;
}
void Student::setSurname(string surname)
{
surname = this->surname;
}
void Student::print()
{
cout << "Student: " << name << " " << surname << endl;
}
Main.cpp
#include <iostream>
#include "Subject.h"
#include "Student.h"
using namespace std;
int main()
{
/*
* First three students should attend first Subject
* while other four the second Subject.
* Also note that only names matter and not surnames.
*/
Student stA("Michael", "Doe");
Student stB("Michael", "Doe");
Student stC("Thomas", "Doe");
Student stD("Nicholas", "Doe");
Student stE("Nicholas", "Doe");
Student stF("John", "Doe");
Student stG("John", "Doe");
Subject subjectA("C++", 3);
Subject subjectB("C", 4);
return 0;
}
1) Get an array of Students into your Subject object: you probably want to use vectors instead of arrays here:
in subject.h add
#include "Student.h"
public:
void addStudent(Student student);
private:
std::vector<Student> students_;
in subject.cpp add
void Subject::addStudent(Student student)
{
this->students_.push_back(student);
}
If you want to extract the student list somehow later, you need to write a function to access it (or make it public).
2) For finding duplicates, look here
Checking for duplicates in a vector
You have to pay attention: the Student objects are in your subject object, not the student names. You have to extract them first and e.g. put them in a vector.
Your task definition sais you should have an array of Students attribute of the Subject class, but i don't see this in your Subject class definition.
And maybe an add Student method and then iterate over the array.

What does "Invalid operands to binary expression (ostream and void)" mean, and how can it be fixed?

I'm encountering an error that says:
Invalid operands to binary expression ('ostream' (aka 'basic_ostream') and 'void')
I understand there are some questions related to this error posted on StackOverflow but I need some help and explanations regarding this specific context on what this error means.
In the main() function, I create a student object called s1. The error happens in main() where I'm trying to get the results of his GPA using a method of the Student class called getResults(double gpa).
#include <iostream>
using namespace std;
class Human{
protected: string name;
protected: int age;
public: Human(){
name = "Unknown";
age = 5;
}
public:
Human(string name, int age){
this->name = name;
this->age = age;
}
string getName(){
return name;
}
int getAge(){
return age;
}
void setName(string name){
this->name = name;
}
void setAge(int age){
this->age = age;
}
};
class Student: public Human{
protected: string school;
protected: double gpa;
public:
Student(string name, int age, string school, double gpa) : Human(name, age){
this->school = school;
this->gpa = gpa;
}
double getGPA(){
return gpa;
}
string getSchool(){
return school;
}
void setGPA(double gpa){
this->gpa = gpa;
}
void setSchool(string school){
this->school = school;
}
void getResult(double gpa){
if (gpa < 3.0) {
cout << "You did well!";
} else {
cout << "Try harder next time";
}
}
};
int main() {
Student s1 ("John", 23, 'm', "University of Chicago", 3.4);
double s1GPA = s1.getGPA();
cout << s1.getResult(s1GPA) << endl;
return 0;
}
Currently, your getResults function has a void return type, which means it doesn't actually return anything. Because of this, do not try to cout the result of this function in your main.
Consider the following edit:
// Your result is printed within this function
s1.getResult(s1GPA);
// Print a new line if you wish
cout << endl;
Also, since your getResults doesn't really get anything, I'd suggest changing the name to something like printResults.
Note
Notice how in your getResult it doesn't return anything because it's a void. In this function, you're just outputting text to the console with cout:
// Notice that this function doesn't actually return anything
void getResult(double gpa){
if (gpa < 3.0) {
// Output this message to console
cout << "You did well!";
} else {
// Output this message to console
cout << "Try harder next time";
}
}
When you have the statement in your main, it's trying to cout nothing because getResult is a void:
cout << s1.getResult(s1GPA) << endl;
// ^^^^^^^^^^^^^^^^^^^
// This doesn't return anything for cout to output.
That is why you only need to call getResult instead of trying to cout it.