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
#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
No comments:
Post a Comment