1

I was just wondering if anybody can help me with this and I am new to C++ and I am having a problem when I am separating the classes into different files.

The error I am getting when I compile the SepMain.cpp file is:

/usr/bin/ld: /tmp/cciHobHn.o: in function `main':
SepMain.cpp:(.text+0x23): undefined reference to `Sep::Sep()'
collect2: error: ld returned 1 exit status

The error I am getting when I compile the Sep.cpp file is:

/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/Scrt1.o: in function `_start':
(.text+0x24): undefined reference to `main'
collect2: error: ld returned 1 exit status

The error I am getting when I compile the Sep.h file is:

bash: /home/ishan/Coding/C++/Learn/tempCodeRunnerFile: Permission denied

so my codes have three files:

Sep.cpp

#include "Sep.h"        
#include <iostream>

using namespace std;   
Sep::Sep()
{
    cout<<"hello"<<endl;
}

Sep.h

#ifndef SEP_H
#define SEP_H

class Sep
{
    public:
        Sep();
};

#endif

SepMain.cpp

#include "Sep.h"
#include <iostream>

using namespace std;

int main()
{
    Sep ir;
    return 0;
}
Ishan Rana
  • 11
  • 2
  • One does not execute a .cpp or a .h file, instead one compiles the c++ files into an executable and executes _that_. – acraig5075 Apr 28 '21 at 06:10
  • You say that like you compile each file separately and try to execute that. Then - of course - SepMain.cpp does not know the constructor definition and Sep.cpp does not have a main function. You have to compile both files and link them together to form an actual application. Your compiler should be able to do that – IWonderWhatThisAPIDoes Apr 28 '21 at 06:15
  • @IWonderWhatThisAPIDoes can you tell how to compile both files and link them together to form an actual application – Ishan Rana Apr 28 '21 at 06:37
  • Unfortunately not, that stuff is compiler-dependent. What compiler are you using? – IWonderWhatThisAPIDoes Apr 28 '21 at 06:43
  • @IWonderWhatThisAPIDoes The compiler is gcc version 9.3.0 and I am using it in ubuntu – Ishan Rana Apr 28 '21 at 07:13
  • I don't know much about that, so I'm going to refer you [here](https://www.cs.utah.edu/~zachary/isp/tutorials/separate/separate.html) – IWonderWhatThisAPIDoes Apr 28 '21 at 07:25
  • @IWonderWhatThisAPIDoes Thanks man for telling me to compile both files together and I did some more googling and I found out how to fix this problem In the terminal I had to type: g++ SepMain.cpp Sep,cpp -std=c++11 ./a.out – Ishan Rana Apr 28 '21 at 08:04

0 Answers0