0

I have a problem! It shows this error:

"C:\Users\User\AppData\Roaming\JetBrains\CLion 2018.2.4\bin\cmake\win\bin\cmake.exe" --build C:\Users\User\CLionProject\HospitalTest\cmake-build-debug --target HospitalTest -- -j 2
[ 50%] Linking CXX executable HospitalTest.exe
CMakeFiles\HospitalTest.dir/objects.a(main.cpp.obj): In function `main':
C:/Users/User/CLionProject/HospitalTest/main.cpp:9: undefined reference to `CHospitalWard::CHospitalWard()'
C:/Users/User/CLionProject/HospitalTest/main.cpp:23: undefined reference to `CHospitalWard::OnAdd()'
C:/Users/User/CLionProject/HospitalTest/main.cpp:24: undefined reference to `CHospitalWard::OnDelRegNum()'
C:/Users/User/CLionProject/HospitalTest/main.cpp:25: undefined reference to `CHospitalWard::OldestPatient()'
C:/Users/User/CLionProject/HospitalTest/main.cpp:26: undefined reference to `CHospitalWard::OnPrint()'
C:/Users/User/CLionProject/HospitalTest/main.cpp:27: undefined reference to `CHospitalWard::IsInRegNum()'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[3]: *** [CMakeFiles\HospitalTest.dir\build.make:85: HospitalTest.exe] Error 1
mingw32-make.exe[2]: *** [CMakeFiles\Makefile2:72: CMakeFiles/HospitalTest.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:84: CMakeFiles/HospitalTest.dir/rule] Error 2
mingw32-make.exe: *** [Makefile:117: HospitalTest] Error 2

Code:

///////////////////////////////

#pragma once
#include "Patient.h"
#include <string>

using namespace std;

class CHospitalWard
{
private:
    string m_name;
    int m_br;
    CPatient *m;

public:
    CHospitalWard();
    CHospitalWard(string, int);
    ~CHospitalWard(){delete []m;}
    void OnAdd();
    void OnDelRegNum();
    void OnPrint();
    int IsInRegNum();
    void OldestPatient();
    string name_access() {return m_name;}
    int br_access() {return m_br;}
};

There are declared like this:

cout<<"Generating..."<<endl;
    CHospitalWard f;
    int c;
    string s;
walnut
  • 21,629
  • 4
  • 23
  • 59
debugger
  • 1
  • 1
  • 8
    Where is the definition of those functions? – user253751 Feb 20 '20 at 16:15
  • 2
    The error as displayed is in main.cpp which you have not provided the source for. – SPlatten Feb 20 '20 at 16:16
  • 2
    Not directly related to your question, but you will encounter this problem once your code compiles: [What is The Rule of Three?](https://stackoverflow.com/questions/4172722/what-is-the-rule-of-three) – Yksisarvinen Feb 20 '20 at 16:17
  • 2
    Most likely you forgot to add your source file into cmake file – Maxim Sagaydachny Feb 20 '20 at 16:18
  • this is a part of my main.cpp: ` switch(c)` `{` `case 0: break;` `case 1: f.OnAdd(); break;` `case 2: f.OnDelRegNum(); break;` `case 3: f.OldestPatient(); break;` `case 4: f.OnPrint(); break;` `case 5: f.IsInRegNum(); break;` `default: cout<<"Incorrect choice!!!"< – debugger Feb 20 '20 at 16:20
  • @debugger 1) All relevant code must be present in the question itself, and **not** in the comments. 2) Such code doesn't contain definitions of functions mentioned in the error, either. Where are definitions of those functions, mentioned in the error? – Algirdas Preidžius Feb 20 '20 at 16:21
  • @debugger You can edit your question by clicking "edit" under the question or by clicking [here](https://stackoverflow.com/posts/60324240/edit). Please add any additional relevant information directly into the question. – walnut Feb 20 '20 at 16:23
  • @user253751 I declared " CHospitalWard f; int c; string s; " – debugger Feb 20 '20 at 16:36
  • 1
    @debugger Okay. Where is the definition of those functions? – user253751 Feb 20 '20 at 16:39
  • From your comments it seems that you need [a good introductory book on C++](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). Every function needs a definition that contains the statements that should be executed when the function is called. What you have mentioned so far are only *declarations* for functions which do *not* contain any statements. There can be many declarations for a function, but there must be exactly one definition for a function that is used in your program. This is very fundamental and should be explained in any introduction to the language. – walnut Feb 20 '20 at 17:11
  • Please don't remove all the relevant content from your question when you edit it. I have merged the previous revision with your last edit. – walnut Feb 20 '20 at 17:14

1 Answers1

-1

It is possible that you have included this file, but you have not yet written implementations for these functions. You will need to implement all functions in order for the program to compile. The minimum implementation for function void DoNothing(); for example would be,

void DoNothing() {}
LLSv2.0
  • 501
  • 6
  • 20
  • 1
    "_Seems like you're probably missing `#include "CHospitalWard.h"`_" If they would missing it - the resulting error would come from the compiler, stating that undeclared identifier was used, rather than from linker stating that the reference to a symbol is undefined. – Algirdas Preidžius Feb 20 '20 at 16:28
  • @AlgirdasPreidžius good catch. I hadn't even looked that closely. Editing my answer now. – LLSv2.0 Feb 20 '20 at 16:29
  • @AlgirdasPreidžius no it is inside the main.cpp #include "HospitalWard.h" – debugger Feb 20 '20 at 16:38
  • @debugger that is what AP was getting at. They were saying it couldn't have been that you didn't include the file because it would have shown a different error if that were the case. – LLSv2.0 Feb 20 '20 at 16:41
  • @LLSv2.0 I define them already in HospitalWard.h like this: CHospitalWard(); void OnAdd(); void OnDelRegNum(); – debugger Feb 20 '20 at 16:42
  • @debugger "_I define them already in HospitalWard.h like this: CHospitalWard();_" That's not a definition. That's a declaration. Where is the definition? No piece of code you showed us, contain the definitions of those functions. – Algirdas Preidžius Feb 20 '20 at 16:44
  • @debugger Those are declarations, NOT definitions. Note the curly braces in my example – LLSv2.0 Feb 20 '20 at 16:44
  • @AlgirdasPreidžius Have I to define them in .h file? Am I supposed to add {} after the declaration of the upper CHospitalWard(){}; void OnAdd(){}; like this? – debugger Feb 20 '20 at 16:58
  • 2
    @debugger "_Have I to define them in .h file?_" No. You can defined wherever you need to. Note: The functions must have one, and only one definition in your entire application. – Algirdas Preidžius Feb 20 '20 at 17:03
  • @debugger If you don't want them to do anything, you *could* just add {} after your declaration and be done, but that's sort of silly (but it would show you if thats your problem for now and then you can define them later). TYPICALLY, we write declarations in a .h file and implementation in .cpp file. However, you could implement them in the h file if you really want to – LLSv2.0 Feb 20 '20 at 17:09