C++ overloaded member function error - c++

hi i was making a program with 3 classes and when i was using a member initialization list i got an error saying "no instance of overloaded function "people::people" matches the specified type:
MAIN.cpp
#include <iostream>
#include "conio.h"
#include <string>
#include "birthday.h"
#include "people.h"
using namespace std;
void main(){
birthday birthObj (30, 06, 1987);
people me("The King",birthObj);
_getch();
}
BIRTHDAY.h
#pragma once
class birthday
{
public:
birthday(int d, int m, int y);
void printdate();
private:
int month;
int day;
int year;
};
BIRTHDAY.cpp
#include "birthday.h"
#include <iostream>
#include "conio.h"
#include <string>
using namespace std;
birthday::birthday(int d, int m, int y)
{
month = m;
day = d;
year = y;
}
void birthday::printdate()
{
cout << day << "/" << month << "/" << year;
}
PEOPLE.h
#pragma once
#include <iostream>
#include "conio.h"
#include <string>
#include "birthday.h"
using namespace std;
class people
{
public:
people(string x, birthday bo);
void printInfo();
private:
string name;
birthday dateOfBirth;
};
PEOPLE.cpp
#include "people.h"
#include <iostream>
#include "conio.h"
#include <string>
#include "birthday.h"
using namespace std;
people::people()
: name(x), dateOfBirth(bo)
{
}
void people::printInfo()
{
cout << name << " was born on ";
dateOfBirth.printdate();
}

People.cpp should be:
people::people(string x, birthday bo) : name(x), dateOfBirth(bo) { }

You havn't implemented the people(string x, birthday bo); constructor. in your PEOPLE.cpp, change
people::people()
: name(x), dateOfBirth(bo)
to
people::people(string x, birthday bo)
: name(x), dateOfBirth(bo)

Your constructor in PEOPLE.cpp has the wrong signature:
It should be:
people::people(string x, birthday bo)
Instead of:
people::people()

people::people()
: name(x), dateOfBirth(bo)
{
}
You have forgotten your arguments to this constructor.

poeple ctor declaration and definition doesn't match!

Related

String declaration problems c++

I was trying to compile this simple program but whenever I try to compile it it gives me many errors and all are string related errors like "syntax error:identifier 'string' " and "undeclared identifier" for my string function and variable.
I tried to delete using namespace std; and using std::string instead but still the same errors occur.
I am using Visual Studio 2017.
#include "Animal.h"
#include <iostream>
#include <string>
using namespace std;
int main() {
Animal Cat;
cin.get();
}
and thats the Animal.h:
class Animal
{
public:
Animal();
void SetAnimalName(string x);
string GetName();
void SetAnimalAge(int y);
int GetAnimalAge();
private:
string AnimalName;
int AnimalAge;
};
Animal.cpp
#include "Animal.h"
#include <iostream>
#include <string>
using namespace std;
Animal::Animal()
{
AnimalName = "cat";
AnimalAge = 3;
std::cout << "the Animal is: " << AnimalName << std::endl << "its Age is: " << AnimalAge;
}
void Animal::SetAnimalName(string x) {
AnimalName = x;
}
string Animal::GetName() {
return AnimalName;
}
void Animal:: SetAnimalAge(int y) {
AnimalAge = y;
}
int Animal::GetAnimalAge() {
return AnimalAge;
}
You are missing the #include <string> in your Animal.h which breaks the compilation of your main.cpp.
You are also missing std::string in your Animal.h. As a general rule of thumb, don't use using namespace std and stick with prefixing standard library functions with the std namespace (std::string in your case).

Seg Fault when using ->GetString(" ") which is in a separate class

I'm practicing some basic C++ right now, and decided to create a class in a header file and the constructor, GetString, etc functions in a separate file.
When I create my object using
"Person Bob" and use "." the code works fine, but if I do Person* Bob, the SetName(x) function seg faults, when I use ->SetName(x, with x being a "abc" string or a string variable
Main.cpp
#include <iostream>
#include <string>
#include "namevalue.h"
using namespace std;
int main(){
Person Bob;
string temp = "bob";
Bob.SetName(temp);
Bob.SetMoney(3000);
cout << Bob.GetName() << " " << Bob.GetMoney() << endl;
return 0;
}
Person.h
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
class Person{
public:
Person();
Person(int money, string name);
void SetName(string y);
void SetMoney(int x);
int GetMoney();
string GetName();
private:
int money;
string name;
};
Person.cpp
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <array>
#include "namevalue.h"
using namespace std;
Person::Person(){
name = " ";
money = 0;
}
Person::Person(int x, string y){
SetName(y);
SetMoney(x);
}
void Person::SetMoney(int x){
money = x;
}
void Person::SetName(string x){
name = x;
}
int Person::GetMoney(){
return money;
}
string Person::GetName(){
return name;
}
If you declare a pointer variable, you need to populate it first with a valid instance. Otherwise, it is pointing to invalid memory and you will get the memory fault you are experiencing.
This should work.
Person* Bob = new Person();
Bob->SetName("Bob");
Bob->SetMoney(3000);
When you're finished, free the memory.
delete Bob;

class undefined C++

In test i am trying to create object partTimeEmployee, but i get an undefined reference error, and i don't understand why? I dont understand the error because i pass it the right kind of data. is my .cpp and .h not included right?
test.cpp: (.text+0xb7): undefined reference to 'partTimeEmployee(std::basic_string, std::allocator >)'
work.h
#ifndef WORK_H
#define WORK_H
using namespace std;
#include <string>
class work{
public:
void DisplayMe();
work(string x,string y);
work();
~work();
string name;
string id;
};
#endif
work.cpp
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib.h>
#include "work.h"
using namespace std;
work::work(){
cout<<name<<"in default work constructor"<<endl;
}
work::~work(){
cout<< name << " calling work destructor. " << endl;
}
work::work(string x, string y){
name = x;
id = y;
cout << "in parent constructor" <<endl;
}
void work::DisplayMe(){
cout << "NAME: " << name << " ID#: " << id <<endl;
}
Employee.h
#ifndef EMPLOYEE_H
#define EMPLOYEE_H
include "work.h"
using namespace std;
class Employee : public sfasu{
public:
Employee();
Employee(string x);
~Employee();
string department;
};
#endif
Employee.cpp
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib.h>
#include "Employee.h"
using namespace std;
Employee::Employee(){
cout<<name<<"in default sfasu constructor"<<endl;
}
Employee::~Employee(){
cout<< name << " calling parent destructor. " << endl;
}
Employee::Employee(string x){
department = x;
cout << "in parent constructor" <<endl;
}
partTimeEmployee.h
#ifndef PARTTIMEEMPLOYEE_H
#define PARTTIMEEMPLOYEE_H
#include "Employee.h"
using namespace std;
class partTimeEmployee : public Employee{
public:
partTimeEmployee();
partTimeEmployee(string x);
~partTimeEmployee();
string hourly_wage;
};
#endif
partTimeEmployee.cpp
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib.h>
#include "partTimeEmployee.h"
using namespace std;
partTimeEmployee::partTimeEmployee(){
cout<<"in default partTimeEmployee constructor"<<endl;
}
partTimeEmployee::~partTimeEmployee(){
cout<< " calling FTP destructor. " << endl;
}
partTimeEmployee::partTimeEmployee(string x){
hourly_wage = x;
cout << "partTimeEmployeeconstructor" <<endl;
}
test.cpp
#include <iostream>
#include <iomanip>
#include "sfasu.h"
#include "Employee.h"
#include "partTimeEmployee.h"
using namespace std;
int main(){
partTimeEmployee one("data");
}
Change
class partTimeEmployee : public partTimeEmployee
to
class partTimeEmployee : public Employee
When you build application you first compile source files (usually .cpp) to object files (usually .o or .obj) and then link them together into executable. It can be done explicitly through command line, using makefile or such or IDE. Your error shows that object file from compilation of partTimeEmployee.cpp is not included in linking. You do not provide enough information how you build your executable so it is difficult to say how to fix it.

C++ class and inheritance error: undefined reference to derived class

I am attempting to make part of a program that uses a bank account class as the base class and checking and savings as the derived classes. I have been trying to set up the basic framework before I do any fancy data handling and I've followed some tutorials to get a better understanding of classes and inheritance.
I have looked for answers but the answers I have found don't seem to be my problem but I might just need another set of eyes on my code.
the compiler errors:
In function main':
badriver.cpp:20: undefined reference toChecking::getAccount()'
badriver.cpp:23: undefined reference to Checking::setAccount(int)'
badriver.cpp:24: undefined reference toSavings::setAccount(int)'
badriver.cpp:26: undefined reference to `Checking::getAccount()'
badriver.cpp
#include "BankAccount.cpp"
#include "Checking.cpp"
#include "Savings.cpp"
#include <string>
#include <iostream>
using namespace std;
int main(){
Checking c;
Savings s;
cout << "Checking: " << c.getAccount() << " - Type: " << c.getType() << endl;
cout << "Savings: " << s.getAccount() << " - Type: " << s.getType() << endl;
c.setAccount(9);
s.setAccount(15);
cout << "New Checking: " << c.getAccount() << endl;
cout << "New Savings: " << s.getAccount() << endl;
return 0;
}
BankAccount.h
#ifndef BANKACCOUNT_H
#define BANKACCOUNT_H
#include <string>
using std::string;
using std::ostream;
using std::istream;
class BankAccount{
private:
int myAccount;
const char* color;
public:
// default constructor
BankAccount();
BankAccount(int account);
virtual ~BankAccount();
virtual void setAccount(int)=0;
int getAccount();
//
// void setSAccount(int);
// int getSAccount();
//
virtual const char* getColor();
virtual const char* getType() = 0;
//virtual const char* getCType() = 0;
protected:
void setColor(const char*);
};
#endif // BANKACCOUNT_H
BankAccount.cpp
#include "BankAccount.h"
#include "Checking.h"
#include "Savings.h"
#include <string>
#include <iostream>
#include <cstdlib>
using namespace std;
// default constructor
BankAccount::BankAccount(){
account = 1;
}
BankAccount::~BankAccount(){}
// void BankAccount::setAccount(int account){
// myAccount = account;
// }
int BankAccount::getAccount(){
return myAccount ;
}
BankAccount::BankAccount(int account){
myAccount = account;
}
const char* BankAccount::getColor(){
return color;
}
void BankAccount::setColor(const char* c){
color = c;
}
Checking.h
#ifndef CHECKING_H
#define CHECKING_H
#include "BankAccount.h"
#include <string>
using std::string;
using std::ostream;
using std::istream;
class Checking : public BankAccount{
private:
const char* type;
public:
Checking();
virtual ~Checking();
void setAccount(int account);
virtual const char* getType();
void setChecking(int);
int getChecking();
};
#endif //CHECKING_H
Checking.cpp
#include "Checking.h"
#include <string>
#include <string>
#include <iostream>
#include <cstdlib>
using namespace std;
Checking::Checking() : BankAccount(1), type("Checking"){}
Checking::~Checking(){}
BankAccount::~BankAccount(){}
void BankAccount::setAccount(int account){
myAccount = account;
}
const char* Checking::getType(){
return type;
}
Savings.h
#ifndef SAVINGS_H
#define SAVINGS_H
#include "BankAccount.h"
#include <string>
using std::string;
using std::ostream;
using std::istream;
class Savings: public BankAccount{
private:
const char* type;
public:
Savings();
virtual ~Savings();
void setAccount(int account);
virtual const char* getType();
void setSavings(int);
int getSavings();
};
#endif // SAVINGS_H
Savings.cpp
#include "Savings.h"
#include <string>
#include <string>
#include <iostream>
#include <cstdlib>
using namespace std;
Savings::Savings() : BankAccount(2), type("Savings"){}
Savings::~Savings(){}
BankAccount::~BankAccount(){}
void BankAccount::setAccount(int account){
myAccount = account;
}
const char* Savings::getType(){
return type;
}
Thanks for any help pointing me in the right direction.
Checking.cpp and Savings.cpp contain:
BankAccount::~BankAccount(){}
void BankAccount::setAccount(int account){
myAccount = account;
}
This causes undefined behaviour because you defined those functions in multiple files. You need to delete those lines from Checking.cpp and Savings.cpp, and instead put in definitions for the functions which are listed as being missing in the compiler output:
void Checking::setAccount(int account){
// code here
}
etc.

an error "has no member named"

I have this snippet of the code
account.cpp
#include "account.h"
#include <iostream>
#include <string>
using namespace std;
Account::Account(string firstName, string lastName, int id)
: strFirstName(firstName), strLastName(lastName), nID(id) {}
void Account::printAccount(){
cout << strFirstName;
}
account.h
#include <string>
using std::string;
class Account{
private:
string strLastName; //Client's last name
string strFirstName; //Client's first name
int nID; //Client's ID number
int nLines; //Number of lines related to account
double lastBill;
public:
Account(string firstName, string lastName, int id);
void printAccount();
};
company.h
#ifndef CELLULAR_COMPANY_H
#define CELLULAR_COMPANY_H
#include <string>
#include <list>
#include <iostream>
#include "account.h"
using namespace std;
class Company {
private:
list<Account> listOfAccounts;
public:
void addAccount(string firstName, string lastName, int id) {
Account newAccount(firstName, lastName, id);
listOfAccounts.push_back(newAccount);
}
void printAccounts(){
for(list<Account>::iterator i = listOfAccounts.begin(); i != listOfAccounts.end(); ++i){
i.printAccount; //here bug
}
}
};
#endif // CELLULAR_COMPANY_H
main.cpp
#include "cellularcompany.h"
int main(){
Company newCompany;
newCompany.addAccount("Pavel", "Nedved", 11111);
newCompany.printAccounts();
return 0;
}
can somebody please explain what does my error mean? thanks in advance (I have it in company.h see comment there)
I have bug 'struct std::_List_iterator<Account>' has no member named 'printAccount'
You forgot the parentheses after printAccount(). Otherwise, it's not a method call. Also, you need to use the -> operator, since it's an iterator.
for(list<Account>::iterator i = listOfAccounts.begin();
i != listOfAccounts.end(); ++i)
{
i->printAccount(); // Note the ()!
// This is equivalent to (*i).printAccount();
}
Try to change i.printAccount; to i->printAccount();