0

The solution consists of a Main project containing a main.cpp file and a separate Shape project consisting of a Shape.h and Shape.cpp.

When I define a class function inline in the header file and reference it in the main file the code executes, however defining it in a separate .cpp file results in LNK1120 and LNK2019 errors.

The folder structure is as follows:

+-- MainProj
|   +-- main.cpp
+-- Shape
|   +-- Shape.h
|   +-- Shape.cpp

I have tried adding the path containing Shape.cpp to includes directory but the issue persists.

main.cpp

#include <iostream>
#include "Shape/Shape.h"

using namespace std;

void main()
{
    Shape s(2, 3);
    cout << s.Area() << endl;
}

The following code works. Shape.h

#pragma once
class Shape
{
public:
    Shape(float l, float w) : length(l), width(w)
    {
    }

    float Area()
    {
        return length * width;
    }
protected:
    float length;
    float width;
};

The following code results in the link errors below. Shape.h

#pragma once
class Shape
{
public:
    Shape(float l, float w);
    float Area();
protected:
    float length;
    float width;
};

Shape.cpp

#include "Shape.h"

Shape::Shape(float l, float w) : length(l), width(w)
{
}
float Shape::Area()
{
    return length * width;
}

Severity Code Description Project File Line Suppression State Error LNK1120 2 unresolved externals Main C:\CPP\Main\x64\Debug\Main.exe 1

Severity Code Description Project File Line Suppression State Error LNK2019 unresolved external symbol "public: float __cdecl Shape::Area(void)" (?Area@Shape@@QEAAMXZ) referenced in function main MainProj C:\CPP\MainProj\main.obj 1

Severity Code Description Project File Line Suppression State Error LNK2019 unresolved external symbol "public: __cdecl Shape::Shape(float,float)" (??0Shape@@QEAA@MM@Z) referenced in function main MainProj C:\CPP\MainProj\main.obj 1

CiunasBothar
  • 81
  • 2
  • 8
  • Welcome to StackOverflow. You shared your code and formatted your post quite well! But it is unlikely that the compiler fabricated the word "Rectangle" out of thin air just to give you an error message. So it seems you are leaving something out of the picture. Be sure that your [Minimal, Complete, Verifiable Examples](https://stackoverflow.com/help/minimal-reproducible-example) are always from a clean slate and don't carry residual problems from the larger example you were working with. You can always EDIT your post and it becomes a candidate for reopening if your problem is not solved. – HostileFork says dont trust SE Aug 27 '19 at 21:54
  • 1
    @CailínBainne: .h includes are a "compile time thing". Use `-I /path/to/headers` from the command line, or `Properties/VC++ Directories/General/Include Directories` from MSVS. You're getting a link error. Use `-L` from command line, or `Properties/VC++ Directories/Source Directories`. Look here: https://learn.microsoft.com/en-us/cpp/build/reference/vcpp-directories-property-page?view=vs-2019 – paulsm4 Aug 27 '19 at 21:58
  • @HostileFork Apologies, my attempt to tighten up the code resulted in the original error codes being left in. Question updated with correct error codes. – CiunasBothar Aug 27 '19 at 22:12
  • 1
    @CailínBainne Perhaps this will help if the other links are non obvious: ["How to solve the error LNK2019: unresolved external symbol - function?"](https://stackoverflow.com/questions/19886397/)... – HostileFork says dont trust SE Aug 27 '19 at 23:19

0 Answers0