-2

I am have created two classes, Room and Customer. Room has Customer as data member and Customer also has Room as data member. I have created seperate cpp files for each class. When i compile my main.cpp, it says " undefined reference to `Room::Room()' ".

Room.h file

class Customer;

    class Room{

    private :  
             Customer *obj1;
    public:
    Room();
    }

Customer.h file

    class Room;

    class Customer{

    private:
       Room *obj;
      };

Room.cpp file

    Room::Room(){ obj1->setName(a); // this line has the problem}

main.cpp file

#include "Room.h"
#include <iostream>
using namespace std;
int main()
{
    Room a;
}

Note: In customer class i have written the code for setName function along with constructors.

drescherjm
  • 10,365
  • 5
  • 44
  • 64
HackerMan
  • 47
  • 1
  • 8
  • 4
    You didn't actually compile Room.cpp – user253751 Sep 04 '20 at 19:19
  • I think you've simplified the example too much and left out important information. You always want to provide an example that exhibits the behaviour you want solved. In this case you should give a program that compiles and has exactly one linker error. Because we don't have this, we can't tell you exactly what the problem is. This makes the question "Unclear" and "Not Useful". – user4581301 Sep 04 '20 at 19:56
  • @drescherjm I am using written the code on notepad in ubuntu. – HackerMan Sep 05 '20 at 03:35
  • @user253751 How to do that? I am compiling on terminal, if i write g++ -o exe1 main.cpp Room.cpp, there is new error Undefined ,reference to Customer:: Customer (), if i write g++ -o exe1 main.cpp Customer.cpp, there is a similar error. – HackerMan Sep 05 '20 at 03:37

1 Answers1

-1
  Room::Room(){ obj1->setName(a); // this line has the problem}

you object obj1 is null you must instantiate your object with new(create new object)