In part 3 of the C++ course in the creating objects video, I’m having trouble with creating the class in visual studio code.
main.cpp:
#include “rectangle.hpp”
#include
using namespace std;
int main() {
rectangle rectangle;
rectangle.width = 10;
rectangle.height = 20;
rectangle.draw();
cout << rectangle.getArea();
return 0;
}
rectangle.cpp:
#include “rectangle.hpp”
#include
using namespace std;
void rectangle::draw() {
cout << “Drawing a rectangle” << endl;
cout << "Dimensions: " << width << ", " << height << endl;
}
int rectangle::getArea() {
return width * height;
}
rectangle.hpp:
#ifndef RECTANGLE_H
#define RECTANGLE_H
class rectangle {
public:
int width;
int height;
void draw();
int getArea();
};
#endif
This is my code and this is the error that comes up when I run it.
C:/msys64/mingw64/bin/…/lib/gcc/x86_64-w64-mingw32/12.2.0/…/…/…/…/x86_64-w64-mingw32/bin/ld.exe: C:\Users-------\AppData\Local\Temp\cck5lW42.o:main.cpp:(.text+0x23): undefined reference to rectangle::draw()' C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\--------\AppData\Local\Temp\cck5lW42.o:main.cpp:(.text+0x2f): undefined reference to
rectangle::getArea()’
collect2.exe: error: ld returned 1 exit status
What I think is happening is that I am unable to compile all of the files together at once. Does anyone have any fixes?