Help, two errors

I am trying to write code that takes in virtual voids and pure virtual voids. My understanding is that with a pure virtual void you do not include the function in the base class, but in the subclass. If you don't, you make the subclass abstract. Well, there's the error I am getting for the first problem. I have an override in the subclass, so I can't figure out what the problem is.

The second error I am getting is that there is no instance of constructor that matches the argument list. Please help, I have read several things, rewatched the demos 3 times. I know it has to be something simple...I just can't see it.

Both errors are in the main.cpp file at the bottom of this message.

Thanks for the help.

Here is the code:

person.h
#pragma once
#include <string>
#include <iostream>

class Person
{
private:
const std::string first_name;
const std::string last_name;
const std::string race;
const std::string sex;
int age;

protected:
long phoneNumber;

public:
Person();
Person(const std::string & first_name, const std::string & last_name, const std::string & race, const std::string & sex, int & age, long long & phoneNumber);

virtual std::string Outputidentity() = 0;

virtual void Outputage() const;

virtual ~Person();

};

person.cpp

#include "Person.h"
#include "pch.h"
#include <iostream>

Person::Person()
:first_name("(Unknown first name)"), last_name("(Unknown last name)"), race("(Unknown race)"), sex("(Unknown sex)"), age (0), phoneNumber (0)
{
}

Person::Person(const std::string & first_name, const std::string & last_name, const std::string & race, const std::string & sex, int & age, long long & phoneNumber)
:first_name(first_name), last_name(last_name), race(race), sex(sex), age(age), phoneNumber(phoneNumber)
{
}


void Person::Outputage() const
{
std::cout << "I am " << age << " years old." << std::endl;
}

Person::~Person()
{
}

student.h
#pragma once
#include "Person.h"
#include <string>
#include "pch.h"
#include <iostream>
#include <list>

class Student : public Person
{
private:
std::string course;

public:
Student ();
Student(const std::string & first_name, const std::string & last_name, const std::string & race, const std::string & sex, int & age, long long & phoneNumber, const std::string & course);


virtual std::string Outputidentitiy();

virtual void Outputage() const;

virtual ~Student();
};

student.cpp

#include "Student.h"
#include "Person.h"
#include <string>

Student::Student()
:course("(Unknown course)")
{
}

Student::Student(const std::string & first_name, const std::string & last_name, const std::string & race, const std::string & sex, int & age, long long & phoneNumber, const std::string & course)
:Person (first_name, last_name, race, sex, age, phoneNumber), course(course)
{
}

std::string Student::Outputidentitiy()
{
return "I am a student.";
}


void Student::Outputage() const
{
Person::Outputage();
}


Student::~Student()
{
}

teacher.h

#pragma once
#include <string>
#include "Person.h"
#include "pch.h"
#include <iostream>
#include <list>

class Teacher : public Person
{
private:
std::string course;

public:
Teacher();
Teacher(const std::string & first_name, const std::string & last_name, const std::string & race, const std::string & sex, int & age, long long & phoneNumber);

std::string Person::Outputidentity();

virtual void Outputage() const;


virtual ~Teacher();
};

teacher.cpp

#include "Teacher.h"
#include "Person.h"
#include <string>

Teacher::Teacher()
:course("(Unknown course)")
{
}

Teacher::Teacher(const std::string & first_name, const std::string & last_name, const std::string & race, const std::string & sex, int & age, long long & phoneNumber)
:Person(first_name, last_name, race, sex, age, phoneNumber), course(course)
{
}

std::string Teacher::Outputidentity()
{
return "I am a teacher.";
}


void Teacher::Outputage() const
{
Person::Outputage ();
}

Teacher::~Teacher()
{
}

#include "pch.h"
#include "Person.h"
#include "Student.h"
#include "Teacher.h"
#include <string>
#include <iostream>



int main()
{

Person * p = new Student("Barry", "Slater", "White", "Male", 45, 6785551919, "Physics"); <error here: object of abstract class type student is not allowed: pure virtual function "Person::Outputidentity" has no overrider>
Person * t = new Teacher("Dwayne", "Wylds", "Asain", "Male", 61, 4045552323); <error here: No instance of object "Teacher::Teacher" matches the argument list. argument types are: (const char[7], const char [6], const char[6], const char[5], int, unsigned long) >


Well, there's the error I am getting for the first problem.
The problem is that you have a typo in the Student class. In the Teacher class you need to remove the scope operator Person:: in front of the Outputidentity() function.

You can use the override keyword in order to determine that the function must override:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Student : public Person
{
private:
std::string course;

public:
Student ();
Student(const std::string & first_name, const std::string & last_name, const std::string & race, const std::string & sex, int & age, long long & phoneNumber, const std::string & course);


virtual std::string Outputidentitiy() override; // Will be a compiler error due to ..titiy

virtual void Outputage() const;

virtual ~Student();
};
See:
https://en.cppreference.com/w/cpp/language/override

For the second error. The problem is this:

int & age, long long & phoneNumber

This are non const references. Hence you cannot pass constant values like 61. So either you remove the reference (&) or you make it const.
Thank you!!

I can't believe I missed the typo. I stared at that off and on all day yesterday.

Also, thanks for the const reference info. It was very helpful.
Topic archived. No new replies allowed.