I have tried every combination of #include statements that I can think of, and nothing is working. I am trying to write a basic inheritance program but i keep getting the error error: expected class-name before '}' token and I just do not know what to do about it anymore. I've tried having my main() include the .cpp file of the Executive class, however this error shows up. The program includes 5 types of employees all inherited from the Employee class, and I'm assuming that they are all the same error:
#include <iostream>
#include <string>
#include "Employee.cpp"
#include "Manager.cpp"
#include "Executive.cpp"
#include "Technical.cpp"
#include "Software.cpp"
#include "Test.cpp"
using namespace std;
int main()
{
Employee emp[3];
Executive emp0("John", "Doe", "VP", 100000.0, 1000000.0, 2000.0);
Software emp1("Vincent", "Giuliana", "Project Leader", 150000.0, 200000.0, 1000.0);
Test emp2("Lauren", "Wallis", "Overseer of Testing", 95000, 115000);
emp[0] = emp0;
emp[1] = emp1;
emp[2] = emp2;
for(int i=0; i<3; i++)
emp[i].displayInformation();
emp0.displayInformation();
emp1.displayInformation();
emp2.displayInformation();
return 0;
}
My Employee.h header file is as follows:
#ifndef EMPLOYEE_H_INCLUDED
#define EMPLOYEE_H_INCLUDED
#include <string>
#include <iostream>
using namespace std;
class Employee
{
private:
string fName, lName, jobTitle;
double baseSalary, salary;
public:
Employee();
Employee(string fName, string lName, string jobTitle, double baseSalary);
void calculateSalary(double baseSalary);
void displayName();
void displayBSalary();
void displayJobTitle();
void displayInformation();
...
getters
...
...
setters
...
};
#endif // EMPLOYEE_H_INCLUDED
My Employee.cpp is:
#include <string>
#include <iostream>
#include "Employee.h"
using namespace std;
Employee::Employee()
{
fName = "";
lName = "";
jobTitle = "";
baseSalary = 000000;
}
...
void Employee::setBSalary(double bs) //sets base salary as parameter
{
baseSalary = bs;
}
The top of the Executive.h header class:
#ifndef EXECUTIVE_H_INCLUDED
#define EXECUTIVE_H_INCLUDED
#include <string>
#include <iostream>
//#include "Employee.h"
using namespace std;
class Executive : public Employee
{
private:
string fName, lName, jobTitle;
double baseSalary, salary, bonus, stockOption;
public:
...
};
#endif // Executive_H_INCLUDED
And last but not least, the Executive.cpp file...
#include
#include
#include "Executive.h"
using namespace std;
Executive::Executive()
{
fName = fN;
lName = lN;
jobTitle = jt;
baseSalary = bs;
bonus = b;
stockOption = so;
}
...
void Executive::setSO(double so) //sets stock option as parameter
{
stockOption = so;
}
I think that I have tried to include each header in each file and still, nothing. Any help would be appreciated, and I thank anyone very much in advance!
You must
#include "Employee.h"
in Executive.h, because the compiler must see the declaration of Employee, when a class inherits from it. So, just remove the comments from the #include
Related
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;
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'm having a problem with method declaration. I'm really new to C++ so I was hoping somebody could help. I'm including my header and part of. my source file
#ifndef HEADER_H
#define HEADER_H
#include <fstream>
using namespace std;
class employee
{
public:
void readData(ifstream & inf);
void Compute (int bp);
void print (ofstream & outf) const;
employee();
private:
int ID;
int Job_class;
int Years;
int Ed;
float sal;
};
#endif
Now the source file, I'm getting the error at void employee :: readData(ifstream&inf)
Member Declaration not found. What am I missing here
#include <iostream>
#include <fstream>
#include <iomanip>
#include "employee.h"
using namespace std;
void employee ::readData(ifstream& inf)
{
inf >> ID >> Job_class >> Years >> Ed;
I have the following class and when I try to compile I get an error stating that it is not a type.
What am I doing wrong?
Owner.h
#ifndef OWNER_H
#define OWNER_H
#include <iostream>
#include <string>
#include "email.h"
#include "phone.h"
using namespace std;
class Owner
{
public:
Owner();
Email ownerEmails();
private:
int intID;
string strFirstName;
string strLastName;
string strAddress1;
string strAddress2;
string strCity;
string strState;
int intZip;
};
#endif // OWNER_H
Owner.cpp
#include <iostream>
#include <string>
#include "owner.h"
using namespace std;
Owner::Owner()
{
}
Email Owner::ownerEmails()
{
Email e;
return e;
}
email.h
#ifndef EMAIL_H
#define EMAIL_H
#include "owner.h"
#include <iostream>
#include <string>
using namespace std;
class Email
{
public:
Email();
Email(int intID);
void setOwner(Owner o);
void setEmail(string email);
void setType(string type);
Owner getOwnerID();
private:
string getEmail();
string getType();
int intID;
Owner owner;
string strEmail;
string strType;
};
#endif // EMAIL_H
Remove #include "email.h" and add a forward declaration of class Email before you declare class Owner in owner.h:
#ifndef OWNER_H
#define OWNER_H
#include <iostream>
#include <string>
//#include "email.h"
#include "phone.h"
using namespace std;
// forward
class Email;
class Owner
{
...
};
#endif // OWNER_H
Guessing based on the given information
Email is nested in a namespace or other class/struct
email.h is spelled wrong and you unintentionally overlooked the error that email.h could not be found (perhaps Email.h)
The include guards are wrong (possibly OWNER_H in email.h)
Making no assumptions about your interpretation of the error messages ...
Email is a template class
There's a close-bracket missing somewhere in email.h
There is no Email type defined anywhere in email.h or phone.h
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();