0

I have encountered the errors LNK2019

"unresolved external symbol "public: __thiscall Rect4::Rect4(double,double)" (??0Rect4@@QAE@NN@Z) referenced in function _main" and "unresolved external symbol "public: double __thiscall Rect4::calcArea(void)" (?calcArea@Rect4@@QAENXZ) referenced in function _main"

I was using Notepad++ to write/ edit the code and using Developer Command Prompt VS 2019 to compile the code. I also tried using the original Notepad to code too and reinstalled VS. But the same error popped up.

Here are the codes to look at. They are simple.

Header

class Rect3 {
public:
    Rect3(double , double);
    double calcArea();
    
private:
    double w;
    double h;
};

Implementation

#include "Rect3.h"

Rect3 :: Rect3(double _w,double _h) {
    w = _w; h = _h;
}
    
Rect3 :: double calcArea() {
    return w*h;
}

Driver

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

using namespace std;

void main() {
    Rect3 rect(4,5);
    cout << rect.calcArea() << endl;
}

Could anyone know/ help solve the issue? Thanks a lot.

Atlas
  • 1
  • 2
    Do you build with the "implementation"? How do you build? What environment are you using? What commands (if using a command-line environment) are you using to build? And if you use an IDE are all source files listed in the actual project? If you use something like VSCode, you should use external tools to handle multi-file projects (like `make` or CMake or Meson etc.). – Some programmer dude Apr 14 '22 at 07:53
  • 6
    Welcome to StackOverflow. Why is the linker looking for Rect4? There's no mention of ```Rect4``` in your code. – ewokx Apr 14 '22 at 07:55
  • oh sorry, Rect4 was coded with Notepad while Rect3 was coded with Notepad++, they still have the same error – Atlas Apr 14 '22 at 08:21
  • > Do you build with the "implementation"? How do you build? What environment are you using? What commands (if using a command-line environment) are you using to build? Unfortunately I don't know what you mean, all that I can say is I compiled with "cl /EHsc test_rect3.cpp" (driver) and run it. – Atlas Apr 14 '22 at 08:23
  • You use a command-line environment, and the command you use to build must list *all* source file (all `.cpp` files). – Some programmer dude Apr 14 '22 at 08:28
  • aaaaaahhh, I got it now. Thank you guys, and sorry for the rookie mistake, haha. – Atlas Apr 15 '22 at 04:19

0 Answers0