#include <iostream>
#include <string>
#include <fstream>
#include <vector>
using namespace std;
class Admin {
static void editUser() {
vector<User> usr = FileManager::createVector(); //errors are here
...
}
};
class FileManager {
public:
static vector<User> createVector() {
string name;
string surname;
string code;
float miles;
float balance;
vector<User> users;
ifstream getUsers("users.txt");
while (getUsers >> name >> surname >> code >> miles >> balance) {
User temp(name, surname, code, miles, balance);
users.push_back(temp);
}
return users;
}
};
This is a piece of code I'm writing and I get these 2 errors:
error C2653: 'FileManager' : is not a class or namespace name
error C3861: 'createVector': identifier not found
The thing is I've looked all over the internet and I really can't see what is wrong, my head hurts a lot, and time is limited. I really didn't want to ask here because you probably have more important questions to answer. Any help is appreciated.
You should either define FileManager before Admin class or use forward declaration to make it visible for compiler.
Related
#include <iostream>
#include <string>
using namespace std;
struct player
{
string name;
int money = 100;
int wins = 0;
};
int main()
{
string user;
cin >> user;
player user;
user.name = user;
}
What is the proper syntax for this in C++? I'm trying to declare the object name as the one given by the user. How would you do this? Would a class be easier to do this? Any tips/advice is much appreciated!
variables names are meaningless at runtime in compiled langages like c++. If you want to refer to a player using his or her provided username at runtime you need a datastructure that will remember the name as as string, like a hashtable or a map (I would recommend a map, those are easier to use) http://www.cplusplus.com/reference/map/map/
best is classes because of security
as by default ,
classes are private in nature but struct are public in nature
#include <iostream>
#include <string>
using namespace std;
class player
{
public:
string name;
int money;
int wins;
public(string name){
this->name = name;
this->money = 100;
this->wins = 0;
}
};
int main()
{
string user;
cin >> user;
player *user = new player(user);
}
Not really sure what the error is here. This is standard file opening that I've used all the time before. The right things are being included. And it's just a regular ifstream. What is wrong with this?
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
int main(){
struct item{
string item;
string type;
int price;
}
ifstream board;
board.open("messageBoard.txt");
}
wow! no one can notice that??!!
int main(){
struct item{ //
string item; // error C2580: redefinition of class name 'item'
string type;
int price;
} // missing a semicolon here `;`
you are using the class name as another identifier so you get a compile-time error redefinition
so you can make them different:
struct Item //
{
string item; // now it's ok Item is not item
string type;
int price;
};
I'm trying to learn about inheritance. I've tried to make a class called ProductionWorker that is derived from Employee. I'm trying to follow the model given by this website. However, it seems like the inheritance isn't working because I get errors in the main function saying that name, number, and date are not set in this scope. What is wrong with the code?
Program.cpp:
#include <iostream>
#include <string>
#include "employee.h"
using namespace std;
int main() {
ProductionWorker worker;
cout<<"What is the employee name?"<<endl;
cin>>name;
worker.setName(name);
cout<<"What is the employee number?"<<endl;
cin>>number;
worker.setNumber(number);
cout<<"What is the employee hire date?"<<endl;
cin>>date;
worker.setDate(date);
cout<<"Employee Information:"<<endl;
cout<<worker.getName()<<endl;
cout<<worker.getNumber()<<endl;
cout<<worker.getDate()<<endl;
cout<<worker.getShift()<<endl;
cout<<worker.getPayRate()<<endl;
return 0;
}
employee.h:
#ifndef EMPLOYEE_H_
#define EMPLOYEE_H_
#include <string>
#include <iostream>
using namespace std;
class Employee{
protected:
string name;
int number;
string date;
public:
Employee(string a="", int b=0, string c=""){
name=a;
number=b;
date=c;
}
void setName(string);
void setNumber(int);
void setDate(string);
string getName();
int getNumber();
string getDate();
};
class ProductionWorker: public Employee{
private:
int shift;
double pay;
public:
ProductionWorker(int d=1, double e=10.0, string a="", int b=0, string c=""):Employee(a, b, c){
shift=d;
pay=e;
}
int getShift();
double getPayRate();
};
employee.cpp:
#include <string>
#include <iostream>
#include "employee.h"
using namespace std;
//EMPLOYEE
void Employee::setName(string a){
name=a;
}
void Employee::setNumber(int b){
number=b;
}
void Employee::setDate(string c){
date=c;
}
string Employee::getName(){
return name;
}
int Employee::getNumber(){
return number;
}
string Employee::getDate(){
return date;
}
//PRODUCTION WORKER
int ProductionWorker::getShift(){
return shift;
}
double ProductionWorker::getPayRate(){
return pay;
}
I think that you should declare name, number and date before you use it, in Program.cpp. You try to get input into symbol that compiler doesn't know yet.
Your error is nothing to do with inheritance:
int main() {
ProductionWorker worker;
cout<<"What is the employee name?"<<endl;
cin>>name;
You're reading into a variable called name but no such variable is in scope (i.e. declared in the main() function itself or globally). Though name is a member of the Employee class, you cannot use it as such in this way. (Consider: how would the compiler know which instance of Employee that you were wanting to set the name of? And, of course, if you could read directly into the instance variable, there would be no need to call worker.setName(...) afterwards).
You should just declare name as a local variable, by specifying its type:
int main() {
string name;
ProductionWorker worker;
cout<<"What is the employee name?"<<endl;
cin>>name;
Now cin>>name; reads into the local variable name that is declared within the main() function. (I took the liberty of fixing your indentation). In a similar way, you need declarations for date and number.
You haven't declared the variables that cin needs to read into.
Just add
string name;
int number;
string date;
At the top of function main.
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
I've spent quite a few hours researching and trying to figure out why I'm getting this error. Basically the three files that have to do with the inheriting are CollegeMember.h, Employee.h, and EmpAcademicRecord.h. Employee. is inheriting from CollegeMember.h and EmpAcademicRecord.h is inheriting from Employee.h. Like this CollegeMember <- Employee <- EmpAcademicRecord. The error occurs in EmpAcademicRecord.h. Heres the three files.
CollegeMember.h
#include <cstdlib>
#include <iostream>
#include<ctype.h>
#include<string.h>
#include "Employee.h"
#include "Student.h"
using namespace std;
// ****************************************************************************
// Class Definitions follow
typedef char* String;
// The CollegeMember class
class CollegeMember
{
protected:
int ID_Number;
string FirstName, LastName;
string AddressLine1, AddressLine2, StateProv, Zip;
string Telephone;
string E_Mail;
string answer, answer2, answer3, answer4;//used as sort of booleans for use with validation
// member functions
public:
CollegeMember ( ); // constructor
CollegeMember(const CollegeMember&); //overloaded constructor
void Modify (CollegeMember Member);
void InputData(int x);
string Summary ( ); //summary
string PrintMe(); //fully describes
}; // End of CollegeMember class declaration
Employee.h
#include <cstdlib>
#include <iostream>
#include<ctype.h>
#include<string.h>
#include "EmpAcademicRecord.h"
#include "EmpEmploymentHistory.h"
#include "EmpExtraCurricular.h"
#include "EmpPersonalInfo.h"
#include "EmpPublicationLog.h"
using namespace std;
// ****************************************************************************
// Class Definitions follow
typedef char* String;
// The Employee Class
class Employee: protected CollegeMember
{
float Salary;
protected:
string Department, JobTitle;
// Member Functions
public:
Employee ( ); // constructor
void Modify (Employee ThisEmp);
void InputData(int x);
void SetSalary (float Sal) // Specified as an in-line function
{ Salary = Sal;}
float GetSalary ( ) {return Salary;} // Specified as an in-line function
string Summary ( ); //summary
string PrintMe(); //fully describes
}; // End of Employee class declaration
EmpAcademicRecord.h
#include <iostream>
#include <cstdlib>
#include<ctype.h>
#include<string.h>
using namespace std;
typedef char* String;
class EmpAcademicRecord: protected Employee{ //error occurs on this line
protected:
int ReferenceNumber;
string Institution;
string Award;
string start;
string end;
public:
EmpAcademicRecord();
void InputData (int x);
void Modify(EmpAcademicRecord ThisRec);
void Summary();
};
Any help with this would be greatly appreciated.
That sort of error is usually caused by the type not being defined when you try to use it.
In this case, it appears that you may have included EmpAcademicRecord.h without having first included Employee.h (the includes at the top of the former do not show the latter).
In other words, at the point where the compiler sees:
class EmpAcademicRecord: protected Employee { //error occurs on this line
it has no idea what the Employee class is.
It may be a simple matter of adding:
#include "Employee.h"
to the top of that file, it's a little difficult to be certain since we don't have the code files. In any case, it's certainly a good first step.
Since you have EmpAcademicRecord.h being included by Employee.h, that will probably result in an infinite recursion.
You could fix that with include guards, but I can't see why you need that particulat inclusion. EmpAcademicRecord depends on Employee, not the other way around.