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.
Related
I am coding a little RPG(Role Playing Game)
Here is the situation: I created an object Personnage.
In my classes, I created a method atttaquer. But I would like that after calling my method attaquer it writes something like this: Goliath attaque David . But to that, I need to grab the name of the Object. Because the player may want to edit the name of Object (The personage name) before playing.
There is my code:
Personnage.h
#ifndef Personnage_h
#define Personnage_h
#include <string>
#include "Arme.h"
class Personnage{
//methods
public:
Personnage();
Personnage(std::string nomArme, int degatsArme);
Personnage(int vie, int mana);
// ~Personnage();
void recevoirDegats(int nbDegats);
void attaquer(Personnage &cible);
private:
// Attributs
int m_vie;
int m_magie;
std::string m_nom;
};
#endif
My Personnage.cpp code:
#include "Personnage.h"
#include <string>
#include <iostream>
void Personnage::recevoirDegats(int nbDegats){
m_vie -= nbDegats;
if (m_vie < 0) {
m_vie = 0;
}
}
void Personnage::attaquer(Personnage &cible){
cible.recevoirDegats(m_arme.getDegats());
// if David attacks Goliath I want to write std::cout << David << "attaque "<< Goliath << endl; but I do not know how to grab the name of the object after it's creation
}
There is my main.cpp
#include <iostream>
#include <string>
#include "Personnage.h"
//#include "Personnage.cpp"
#include <ctime>
using namespace std;
int main()
{
Personnage David, Goliath, Atangana("Ak47", 35);
Goliath.attaquer(David);
return 0;
}
If you want to give your objects names, it cannot be the variable names. They are only meant for the compiler and they are fixed. So you need to create a class that can have a name:
class NamedObject
{
private:
std::string m_name;
public:
const std::string& getName() const
{
return m_name;
}
void setName(const std::string& name)
{
m_name = name;
}
}
And if you want your classes to have a name, the easiest way would be to derive from it:
class Personnage : NamedObject {
Then you can say:
Personnage player1, player2;
player1.setName("David");
player2.setName("Goliath");
Alternatively, you can get those string from user input.
And if you need to address one by name:
std::cout << player1.getName() << " please make your move." << std::endl;
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).
When I printed the variables passed through, the default is printed first, followed by what I want passed. So the final result remains the same. The initialization is found in Owner.h and Owner.cpp. Variables are passed starting from the Dog.cpp. I've also tried changing my print statements to Dog.owner... but the result was the same.
Owner.h
#define OWNER_H
#include <iostream>
#include <string>
#include "Dog.h"
using namespace std;
class Owner {
private:
string name;
int age;
public:
Owner(string ownerName = "Lucy" , int ownerAge = 10); // default params
string getName();
int getAge();
};
#endif
Owner.cpp
#include <iostream>
#include <string>
#include "Dog.h"
using namespace std;
// Getters
string Owner::getName() {return name;}
int Owner::getAge() {return age;}
// Constructors
Owner::Owner(string ownerName, int ownerAge) :name(ownerName), age(ownerAge) {
Owner::getName();
Owner::getAge();
}
Dog.h
#ifndef DOG_H
#define DOG_H
#include <iostream>
#include <string>
#include "Owner.h"
using namespace std;
class Dog {
private:
string breed;
int age;
Owner owner;
static int dogCount;
public:
Dog();
Dog(string, int);
// Getter and Setter methods
void setBreed(string var);
void setAge(int var);
string getBreed();
int getAge();
// Other
void printDogInfo();
static int getDogCount() {return dogCount;}
};
#endif
Dog.cpp
#include <iostream>
#include <string>
#include "Dog.h"
#include "Owner.h"
using namespace std;
// Constructors
Dog::Dog(string ownerName, int ownerAge) {
Owner(ownerName, ownerAge);
dogCount++;
}
Dog::Dog() {
}
void Dog::printDogInfo() {
cout << "owner: " << owner.getName() << ", " << owner.getAge() << " yo" << endl << endl;
}
int main() {
Dog myDog1("Belle", 15);
myDog1.setBreed("Siberian Husky");
myDog1.setAge(2);
myDog1.printDogInfo();
return 0;
}
Dog::Dog(string ownerName, int ownerAge) {
Owner(ownerName, ownerAge);
dogCount++;
}
By:
Dog::Dog(string ownerName, int ownerAge) : Owner(ownerName, ownerAge) {
dogCount++;
}
Probably, you also want to fix this:
Owner::Owner(string ownerName, int ownerAge) :name(ownerName), age(ownerAge) {
// Owner::getName(); not needed
// Owner::getAge(); not needed
}
Dog::Dog(string ownerName, int ownerAge) {
Owner(ownerName, ownerAge);
dogCount++;
}
is equivalent to
Dog::Dog(string ownerName, int ownerAge) :
breed(),
owner()
{
Owner(ownerName, ownerAge); // Create temporary
dogCount++;
}
You probably want instead:
Dog::Dog(string ownerName, int ownerAge) :
breed(),
age(0),
owner(ownerName, ownerAge)
{
dogCount++;
}
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.
I've looked around, and I can't quite figure out where I'm going wrong, as I seem to be following the correct convention when using interfaces, but perhaps I'm overlooking something. The exact error I'm getting is:
undefined reference to `vtable for Icommand'
I've only just begun to seperate my classes and class declarations into separate header files, so perhaps I'm missing a preprocessor directive somewhere.
main.cpp:
#include <iostream>
#include <string>
#include <cstdlib>
#include "Icommand.h"
#include "Command.h"
using namespace std;
void pause();
int main(){
Icommand *run = new Command("TEST");
cout << run->getCommand() << endl;
delete run;
pause();
}
void pause(){
cin.clear();
cin.ignore(cin.rdbuf()->in_avail());
cin.get();
}
Icommand.h:
#ifndef ICOMMAND_H
#define ICOMMAND_H
#include <string>
#include <vector>
class Icommand
{
private:
public:
Icommand(){}
virtual ~Icommand(){}
virtual bool run(std::string object1) = 0;
virtual bool run(std::string object1, std::string object2) = 0;
virtual std::string getCommand() const;
};
#endif // ICOMMAND_H
Command.h:
#ifndef COMMAND_H
#define COMMAND_H
#include <string>
#include <vector>
#include "Icommand.h"
class Command : public Icommand {
private:
std::string command;
std::vector<std::string> synonymns;
Command(); // private so class much be instantiated with a command
public:
Command(std::string command) : command(command){}
~Command(){}
bool run(std::string object1);
bool run(std::string object1, std::string object2);
std::string getCommand() const;
};
#endif // COMMAND_H
Command.cpp:
#include <string>
#include <vector>
#include "Command.h"
bool Command::run(std::string object1){
return false;
}
bool Command::run(std::string object1, std::string object2){
return false;
}
std::string Command::getCommand() const {return command;}
In Icommand.h, replace
virtual std::string getCommand() const;
with
virtual std::string getCommand() const = 0;
to make it pure virtual. Then the compiler can generate a vtable for Icommand. Alternatively, implement Icommand::getCommand.