0

I am sure this question has be asked and answered million times but I am really clueless. As a newbie to C++ I have created three textbook files.

Num.h

#ifndef LIB_DATE_H
#define LIB_DATE_H

class Num 
{ 
  private: 
    int num; 
  public: 
    Num(int n); 
    int getNum(); 
};

#endif

Num.cpp

#include "Num.h" 

Num::Num(int n) : num(n) {} 
int Num::getNum() 
{ 
    return num; 
}

main.cpp

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

using namespace std; 

int main()
{ 
  Num n(35); 
  cout << n.getNum() << endl; 
  return 0;
}

Both g++ and clang complain about undefined reference to Num::Num(int) and Num::getNum().

When joining the three files into a single one, no errors are reported by g++; clang is still complaining.

I have managed to google out that this is a problem with linking but I was not able to find any actionable advice to fix it...

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
Macky
  • 125
  • 9
  • 1
    Do you use g++ and clang by typing something at the command line? The problem here is with what you specifically typed, which isn't mentioned here. – Drew Dormann Nov 09 '21 at 21:36
  • If you are still having trouble, then show how you are invoking the compiler – M.M Nov 09 '21 at 21:57
  • I think I am on a track - apparently g++ and clang are not compiling all .cpp files as they should. If I add ```#include Num.cpp``` into ```main.cpp``` file, it runs OK. Apparently I will have to play a bit with vscode configuration files. Apparently not the first one facing the issue - see https://stackoverflow.com/questions/47665886/vs-code-will-not-build-c-programs-with-multiple-ccp-source-files. – Macky Nov 10 '21 at 06:01
  • OK - so it is indeed the case. @πάντα ῥεῖ - if you decide to close the question you should at least provide relevant reference and Drew and M.M did. Closing the question immediately and sending very general links did not help me at all. – Macky Nov 10 '21 at 06:15

0 Answers0