r/programminghelp • • 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;

}

4 Upvotes

1 comment sorted by

2

u/Goobyalus Jul 20 '22

You refer to Employer before it is declared.

You could forward-declare class Employer before class person so you could refer to Employer inside person, but then you would get an incomplete type error.

You can define class Employer before class Person, and forward-declare class person; before class Employer so that Employer can refer to the friend class person, and person's method can take an Employer instance as an argument.