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.
Related
main.cpp
#include <iostream>
#include "shreeman.h"
using namespace std;
int main(){
Shreeman object;
}
shreeman.h
#ifndef SHREEMAN_H
#define SHREEMAN_H
class Shreeman
{
public:
shreeman();
};
#endif // SHREEMAN_H
shreeman.cpp
#include <iostream>
#include "shreeman.h"
using namespace std;
Shreeman::shreeman()
{
cout<<"Hello Hello"<<endl;
}
Why does this code not output "Hello Hello"? I have created an object in the main.cpp file, yet nothing is printed in the console.
In the Shreeman class, shreeman() (lowercase s) is not a valid constructor, it is just a member method. It needs to be renamed to Shreeman() (uppercase S) to be a construcor (the compiler should have warned you about that). C++ is case-sensitive, and a constructor name needs to match the class name exactly, case and all.
main.cpp
#include "shreeman.h"
int main()
{
Shreeman object;
}
shreeman.h
#ifndef SHREEMAN_H
#define SHREEMAN_H
class Shreeman
{
public:
Shreeman();
};
#endif // SHREEMAN_H
shreeman.cpp
#include <iostream>
#include "shreeman.h"
using namespace std;
Shreeman::Shreeman()
{
cout << "Hello Hello" << 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).
i am using eclipse on mac to run c++ program. I am new to c++ and was trying to learn composition by using different classes individually.I am facing the issue in the following line of the code
Main.cpp
#include <iostream>
using namespace std;
#include "Birthday.h"
#include "People.h"
int main() {
cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
Birthday obj(25,3,1993);
obj.print();
People pp(5,obj);
pp.printinfo();
return 0;
}
Birthday.cpp
#include <iostream>
using namespace std;
#include "Birthday.h"
//#include "People.h"
Birthday::Birthday(int d,int m,int y){
// TODO Auto-generated constructor stub
date =d;
month=m;
year=y;
}
void Birthday::print()
{
cout <<date << month<<year<<endl;
}
People.h
#ifndef PEOPLE_H_
#define PEOPLE_H_
//using namespace std;
#include "Birthday.h"
class People {
public:
People(int x,Birthday bb);
void printinfo();
private:
int xx;
Birthday bo;
};
#endif /* PEOPLE_H_ */
People.cpp
#include "People.h"
#include <iostream>
using namespace std;
#include "Birthday.h"
#include<string>
People::People(int x,Birthday bb)
:xx(x),bo(bb)
{
// TODO Auto-generated constructor stub
}
void People::printinfo()
{
cout<< xx<<bo.print(); //I am getting error because of this line , as soon as i comment it program compiles fine.
}
I have tried to use string variable instead of xx variable but it was giving me some other error.So i tried to simplify and learn the concept of compostion before jumping into strings manipulation directly.
cout << xx << bo.print();
bo.print() - function and they not have return value (void)
Just write:
cout << xx;
bo.print();
I'm developing a simulation game in cpp using Visual Studio 2017 for School and in the development phase I got stuck in this situation.
So, what I did was create a new project to try and recreate that issue in the simplest form, so that it would be easier to debug.
Below is the main file and all the associated source codes:
main.cpp
#include "header.h"
#include "Vehicle.h"
#include "Car.h"
int main() {
Vehicle v;
v.addCar(1);
v.addCar(2);
v.addCar(3);
cout << v.getCars()[1].id << endl;
v.getCars()[1].id = 99;
cout << v.getCars()[1].id << endl;
system("pause");
return 0;
}
header.h
#ifndef CLUSTE2R_H
#define CLUSTE2R_H
#pragma once
#include <iostream>
#include <vector>
using namespace std;
#endif
Car.h
#ifndef CLUSTE1R_H
#define CLUSTE1R_H
#pragma once
#include "Vehicle.h"
using namespace std;
class Car : public Vehicle
{
public:
int id;
Car(int id);
~Car();
};
#endif
Car.cpp
#include "Car.h"
Car::Car(int id)
{
this->id = id;
}
Car::~Car()
{
}
Vehicle.h
#ifndef CLUSTER_H
#define CLUSTER_H
#pragma once
#include <vector>
//#include "Car.h"
class Car;
using namespace std;
class Vehicle
{
private:
vector<Car> cars;
public:
Vehicle();
~Vehicle();
vector<Car> getCars();
void addCar(int id);
};
#endif
Vehicle.cpp
#include "Vehicle.h"
#include "Car.h"
#include <vector>
using namespace std;
//class Car;
Vehicle::Vehicle()
{
}
Vehicle::~Vehicle()
{
}
vector<Car> Vehicle::getCars()
{
return this->cars;
}
void Vehicle::addCar(int id)
{
Car c(id);
cars.reserve(cars.size() + 1);
cars.push_back(c);
}
So, what I'm trying to do is to get the following output:
2 \n 99
This is what I'm getting:
2 \n 2
What am I doing wrong? I believe the issue is associated with the main.cpp file. But I'm not quite sure how to achieve what I want in any other way...
Currently, you are returning a new instance of a vector when you call getCars() function from your Vehicle, this means that all changes to the vector will not be applied to the original vector in the class.
To fix this you could just return a reference of the vector(changing the vector<Car> getCars(); to std::vector<Car>& getCars()).
You could also make a local copy of the vector and then setting the vector to the 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.