I am writing a simple program that contains a rectangle class with get/set methods and a main program that allows the user to change the dimensions of the rectangle with two cin's. I think I have avoided all common pitfalls I have seen associated with LNK2019. My Rectangle.h file, Rectangle.cpp, and Main.cpp are all Included In Project and not Excluded From Build, and all my methods are defined in Rectangle.cpp. Can anyone help trace the source of the error?
Rectangle.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#pragma once
class Rectangle
{
public:
Rectangle(void);
Rectangle(int,int);
~Rectangle(void);
//method prototypes
int getHeight(void);
void setHeight(int);
int getWidth(void);
void setWidth(int);
int getArea(void);
};
#include <iostream>
#include "Rectangle.h"
usingnamespace std;
Rectangle r1, r2;
int dim1,dim2;
int main()
{
r1 = Rectangle();
r2 = Rectangle(12, 14);
cout << "The width of rectangle 1 is " << r1.getWidth() << endl;
cout << "The height of rectangle 1 is " << r1.getHeight() << endl;
cout << "The area of rectangle 1 is " << r1.getArea() << endl;
cout << "The width of rectangle 2 is " << r2.getWidth() << endl;
cout << "The height of rectangle 2 is " << r2.getHeight() << endl;
cout << "The area of rectangle 2 is " << r2.getArea() << endl;
cout << "Please input integer to be used as new height and new width of rectangle 1 and 2 respectively.\n";
cin >> dim1;
cout << "\nPlease input integer to be used as new width and new height of rectangle 1 and 2 respectively.\n";
cin >> dim2;
r1.setHeight(dim1);
r2.setWidth(dim1);
r1.setWidth(dim2);
r2.setHeight(dim2);
cout << "\nNew dimensions of rectangle 1 are: width - " << r1.getWidth() << " height - " << r1.getHeight() << endl;
cout << "New are of rectangle 1 is " << r1.getArea() << endl;
cout << "New dimensions of rectangle 2 are: width - " << r2.getWidth() << " height - " << r2.getHeight() << endl;
cout << "New area of rectangle 2 is " << r2.getArea() << endl;
return 0;
}
Errors:
1 2 3 4 5 6
Error 1 error LNK2019: unresolved external symbol "public: int __thiscall Rectangle::getHeight(void)" (?getHeight@Rectangle@@QAEHXZ) referenced in function _main
Error 2 error LNK2019: unresolved external symbol "public: void __thiscall Rectangle::setHeight(int)" (?setHeight@Rectangle@@QAEXH@Z) referenced in function _main
Error 3 error LNK2019: unresolved external symbol "public: int __thiscall Rectangle::getWidth(void)" (?getWidth@Rectangle@@QAEHXZ) referenced in function _main
Error 4 error LNK2019: unresolved external symbol "public: void __thiscall Rectangle::setWidth(int)" (?setWidth@Rectangle@@QAEXH@Z) referenced in function _main
Error 5 error LNK2019: unresolved external symbol "public: int __thiscall Rectangle::getArea(void)" (?getArea@Rectangle@@QAEHXZ) referenced in function _main
Error 6 error LNK1120: 5 unresolved externals
You forgot to put the full name of your method definitions, e.g. int Rectangle::getHeight(void).... The compiler assumes rightfully that Rectangle::getHeight and getHeight are two separate functions. Therefore, you never defined Rectangle::getHeight and the others.
Also, height and width are not member variables of your class. They are global variables within the implementation's unit. Simply move your declarations of those variables to the class declaration in the header file (private or public is up to you).