r/programminghelp • u/HotCommunication4447 • Jul 20 '22
C++ I'm having a problem
I could use a little help. I keep getting an error in my code, with my third class. It says unknown type name 'employer'. Do you have any solutions?
This is my code.
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
using namespace std;
int option;
void menu(){
cout<<"Welcome to the address book Menu"<<endl;
cout<<"1.Add personal Information"<<endl;
}
class Personalinfo{
private:
string name;
int age;
int number;
vector<string> Records;
public:
friend class person;
};
class person{
public:
void add(Personalinfo Ob){
ofstream nijonfile;
nijonfile.open("Objects.cpp");
cout<<"Enter your name"<<endl;
cin>>Ob.name;
cout<<"Your name is" << Ob.name <<endl;
nijonfile << Ob.name <<endl;
Ob.Records.push_back(Ob.name);
for(int i=0; i<Ob.Records.size();i++){
cout<<"Your info is" << Ob.Records[i] <<endl;
}
}
void luffy(Employer HP){
cout<< HP.hours <<endl;
}
};
class Employer{
private:
int hours = 11;
public:
friend class person;
};
int main(){
Personalinfo ob1;
person ob2;
ob2.add(ob1);
Employer HP1;
}
2
u/Goobyalus Jul 20 '22
You refer to
Employerbefore it is declared.You could forward-declare
class Employerbeforeclass personso you could refer toEmployerinsideperson, but then you would get an incomplete type error.You can define
class Employerbeforeclass Person, and forward-declareclass person;beforeclass Employerso thatEmployercan refer to the friend classperson, andperson's method can take anEmployerinstance as an argument.