0

I have received the following code that is only supposed to allow the allocation of a string-variable in the class constructor. I have tried many things but always get the following error:

"undefined reference to 'Patient::Patient(std::string)"

Patient.hpp

#ifndef Patient_hpp
#define Patient_hpp

#include <iostream>
#include <string>

using namespace std;

class Patient{
protected:
    string name;

public:
    Patient(string name);
};
#endif // Patient_hpp

Patient.cpp

#include "Patient.hpp"

Patient::Patient(string name){
    this->name=name;
}

Main.cpp

#include "Patient.hpp"


int main(){
    Patient p("Mary");

    return 0;
}

I would be very grateful for your help.

Best regards

pacours
  • 33
  • 6
  • 1
    TL;DR of the dupe: You need to compile and link `Main.cpp` with `Paitent.cpp`. If you are using g++, see: https://stackoverflow.com/questions/3202136/using-g-to-compile-multiple-cpp-and-h-files – NathanOliver Feb 15 '21 at 14:49
  • 1
    Likely your problem is you are not building `Patient.cpp`. If you are using VSCode remember the default behavior of this code editor to build only the active file into the executable. You have to use the change in the documentation on how to modify tasks.json to fix that. – drescherjm Feb 15 '21 at 14:50
  • Mhm I understand that your suggestion is that the main file does not compile the Patient.cpp-file as well. However, I compile my file via Code::Blocks. I really don't understand how I would approach this as the references are using g++ to my understanding. – pacours Feb 15 '21 at 15:06
  • 1
    in Code::Blocks perhaps you did not add `Patient.cpp` to your project. Being in the same folder as `Main.cpp` does not help. – drescherjm Feb 15 '21 at 15:09
  • Thank you very much. I don't know if I would have ever thought about this without your help. Very much appreciated – pacours Feb 15 '21 at 17:04

0 Answers0