-2

I have compiled person.h and person1.cpp files successfully. But when I add player1.h and player1.cpp files in my code I get an error.

My main function is:

#include <iostream>
#include "person1.cpp"

using namespace std;

int main(){

    Person person1("Metehan Gencer", 100, "Bahcelievler Mah. Hayyam1 Sok. Huzur Apt.");
    cout << "person : " << person1 << endl;

    return 0;
}

person1.h

#ifndef PERSON1_H_INCLUDED
#define PERSON1_H_INCLUDED

#include <string>

#include <string_view>

using namespace std;

// Base Class
class Person
{
    friend ostream& operator<<(ostream&, const Person& person);
public:
    Person() = default;
    Person(string_view fullname, int age, const string address);
    ~Person();

    // Getters
    string get_full_name()const{
        return m_full_name;
    }

    int get_age()const{
        return m_age;
    }

    string get_address()const{
        return m_address;
    }

public:
    string m_full_name{"None"};
protected:
    int m_age{0};
public:
    string m_address{"None"};
};

#endif // PERSON1_H_INCLUDED

person1.cpp

#include <iostream>
#include "person1.h"
using namespace std;

Person::Person(string_view fullname, int age, const string address)
    : m_full_name{fullname}, m_age{age}, m_address{address} {}

ostream& operator<<(ostream& out, const Person& person){
    out << "Person [Full name : " << person.get_full_name() << ", Age: " << person.get_age() << ",          Address: " << person.get_address() << "]";
    return out;
}

Person::~Person()
{}

The Code running as this. But when I add player1.cpp and player1.h files I get an error like this: fatal error: person1.h: No such file or directory| ||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

player1.h

#ifndef PLAYER1_H_INCLUDED
#define PLAYER1_H_INCLUDE D

//#include <person1.h>

// Player will do public inheritance form Person
class Player : public Person
{
    friend ostream& operator<<(ostream& out, const Player& player);
public:
    Player();
    ~Player();

    // See the access we have to inherited members from Person
    void play(){
        m_full_name = "John Snow";  // OK
        m_age = 55;                 // OK
        m_address = "ASDFGH;SADFSDG;JHYUKH0";
    }
private:
    int m_career_start_year{0};
    double m_salary{0.0};
    int health_factor{0};   // Let's say that it's 0~10
};

#endif // PLAYER1_H_INCLUDED

player1.cpp

#include <iostream>
#include <person1.h>
#include <player1.h>
using namespace std;

Player::Player() {}

ostream& operator<<(ostream& out, const Player& player){
    out << "Player[Full name : " << player.get_full_name() << ", age : " << player.get_age() << ",   address : " << player.get_address() << "]";
        return out;
}

Player::~Player() {}
  • 1
    Don't use to #include application headers. That syntax is reserved for system headers. – Botje Aug 30 '23 at 09:22
  • 2
    1. At one point you include a `.cpp` file instead of a header. That is practically always a mistake. 2. Please check [What is the difference between #include and #include "filename"?](https://stackoverflow.com/questions/21593/what-is-the-difference-between-include-filename-and-include-filename/21594#21594) – Homer512 Aug 30 '23 at 09:27
  • Please do not `#include "person1.cpp"`. Just include `person1.h` and compile with `person1.cpp` and `main.cpp` – Louis Go Aug 30 '23 at 09:27
  • 2
    And please don't pollute your headers with `using namespace std;`. That infects every source file that includes them. – Toby Speight Aug 30 '23 at 09:28
  • You need to show what commands you're using to build your program. – Toby Speight Aug 30 '23 at 09:29
  • Thank you so much for your help. Fixed problem. – engineerMete Aug 30 '23 at 09:30
  • 2
    Consider making an answer then, after making sure that your question has all the info which others would have needed to give you that answer. I.e. edit question, then "Post Your Answer". – Yunnosch Aug 30 '23 at 09:44

0 Answers0