Unresolved external symbol error when using multiple directories with c++ project

jjs 1 Reputation point
2021-03-09T03:34:47.84+00:00

Hello, I'm having an issue where VS is throwing an unresolved external symbol error and I'm not sure how to resolve this.

Error: LNK2019 unresolved external symbol "public: __thiscall coppercable::coppercable(void)" (??0coppercable@@QAE@XZ) referenced in function _main NetworkStackMain C:\Users\JoshS\OneDrive\NetworkStack\NetworkStack\NetworkStackMain\NetworkStackMain.obj 1

After a few searches this seems to commonly be due to the linker being unable to find this function declaration. I attempted to add the directories of the file containing the function the error is referring to, coppercable::coppercable(), but this did not help.

Here is the location of my VS project files, per the error

![75644-image.png]2

And here is the location of the file containing the function declaration, note the definition is in a cpp file in /source instead of /header

75663-image.png

Code in main:

#include <iostream>  
#include "cable.hpp"  
#include "coppercable.hpp"  

using namespace std;  

int main()  
{  
    cable* cable1 = new coppercable();  
    cable1->setCableDiameter(0.2893);  
    cable1->setCableLength(5);  
    cout << cable1->getAwgRating();  
}  

Header file with function declaration:

#include <iostream>  
using namespace std;  

class coppercable : public cable  
{  
    public:  
        coppercable();  
        ~coppercable();  
        double getPowerLoss() override;  
};  

Source file with function definition:

#include "../header/coppercable.hpp"  
#include "../header/copperwire.hpp"  
#include <iostream>  
#include <math.h>  
using namespace std;  

coppercable::coppercable()  
{  

}  

And for project properties, I've tried adding multiple directories including the exact directory where the source and header files are which contain the functions declaration/definition

75589-image.png

Has anyone experienced this kind of issue based on this scenario? Any pointers or ideas is appreciated, thank you!

C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,540 questions
Visual Studio Debugging
Visual Studio Debugging
Visual Studio: A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.Debugging: The act or process of detecting, locating, and correcting logical or syntactical errors in a program or malfunctions in hardware. In hardware contexts, the term troubleshoot is the term more frequently used, especially if the problem is major.
943 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Viorel 112.5K Reputation points
    2021-03-09T06:30:17.947+00:00

    Probably you must add coppercable.cpp and maybe other .cpp files to your project. Right-click Source Files or project node in Solution Explorer, go to Add, Existing Item and select the file.

    The longer way: create a Static Library or DLL, which can be used in many main projects.

    0 comments No comments

  2. Dylan Zhu-MSFT 6,406 Reputation points
    2021-03-09T07:49:58.383+00:00

    Hi jjs,

    The LNK2019 often means that your declaration is not defined. You can try to add the directories of source files in VC++ Directories/Source Directories.
    75678-image.png
    Best Regards,
    Dylan


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our **documentation to enable e-mail notifications if you want to receive the related email notification for this thread.**

    0 comments No comments