Errors with creating header files

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?

Change your class name from rectangle from Rectangle. When creating object it cannot find the identifier for your type rectangle. It is like declaring int int;

Also,

Change the second line to #include <iostream> if it is not an error while copying.

Hello,
I have same problem. Did you solved it? if you did, please can you help me?

I have posted the solution. If it is something else then ask a new question or post the new error after trying the solution (if any).

I also got the same issue while running code using VS code. So the code runner is not providing the implementation class while compiling and so its not linking it. To solve this error you manually run using the below command from terminal instead of run icon :
g++ -o Main main.cpp rectangle.cpp && Main.exe
This will compile and show the output on terminal.

I’ve received the same error in VSC:

undefined reference to `Rectangle::getArea()'

I’ve verified that my code is correctly capitalized as pompomballs suggested and tried running the terminal command iambetaraybill provided. When I run that command, I get this:

At line:1 char:36
+ g++ -o Main main.cpp rectangle.cpp && Main.exe
+                                    ~~
The token '&&' is not a valid statement separator in this version.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordExcept  
   ion
    + FullyQualifiedErrorId : InvalidEndOfLine

I get that whether I use one “&” or “&&”, and removing them throws this:

g++.exe: fatal error: input file 'Main.exe' is the same as output file

Rectangle.hpp:

#ifndef ADVANCED_RECTANGLE_H
#define ADVANCED_RECTANGLE_H

class Rectangle {
public:
    int width;
    int height;
    void draw();
    int getArea();
};

#endif // ADVANCED_RECTANGLE_H

Rectangle.cpp:

#include "Rectangle.hpp"
#include <iostream>

using namespace std;

void Rectangle::draw() {
    cout << "Drawing a rectangle!" << endl;
    cout << "Dimensions: " << width << ", " << height << endl;
}

int Rectangle::getArea() {
    return width * height;
}

main.cpp:

#include "Rectangle.hpp"
#include <iostream>

using namespace std;

int main() {
    Rectangle rectangle;
    rectangle.width = 10;
    rectangle.height = 20;

    cout << rectangle.getArea();
    
    return 0;
}

Any guidance would be appreciated. Let me know if I can provide any additional info.

After digging around online, I found a YouTube video that solved the issue for me. I had to add “#include “Rectangle.cpp”” in my main.cpp file.

Hi, can you try this

If you’re using g++, run this in the terminal:

g++ main.cpp rectangle.cpp -o program
./program

This compiles and links both files (main.cpp and rectangle.cpp) to create and run the program

I hope this will work for you….

Check pinned comments and their replies for the same video. You do not include each file to resolve a linker error.

Watch this.

I appreciate you pointing that out. It’s a shame that it’s a joke video.

However, that linked video doesn’t really provide a solution either.

I remembered that I had encountered this exact issue way earlier in the course, and it was resolved modifying my tasks.json per this thread:

Which looks like it got the code from here:

Except my tasks.json is still showing that correct arguments for working within the directory or utils directory, and when I go back to trying to import greet.hpp and utilizing it, I am still getting “undefined reference” messages.

I did even more digging and found that, as long as the tasks.json is configured correctly, running the code will throw the error but running it through the Run and Debug menu in VSC will build and run everything correctly. Nowhere have I found where this is explicitly stated, but this video had a user running the code from the Run and Debug menu so I decided to try it and it’s what works.

Hi, I start to learn C++, do you know how can i find one partner to start with eachother?

Ask your friends if they are available locally (should work best) or create a post asking if anyone is willing to learn alongside and ask for direction or resources whenever needed.

If you watched the video I linked, it should have been clear about what linking is and how the error is being generated. You either did not watch it completely or you missed the whole explanation looking for the specific answer.

The video uses Visual Studio and you are using Visual Studio Code. Both have different ways of handling multiple files. Both have different configurations. Either change your development application or find a resource to make the application function properly.

Modify tasks.json VS Code to build multiple C++ files

The message did not post as I pressed enter instead of clicking reply, thus the delay.

I don’t appreciate your tone here and I really don’t see why this response was worth posting days after I found and reported a solution unless your desire was to be condescending. I did watch the video through to the end and understood the information being presented, but it was not directly applicable to VSC for the reasons you stated. Additionally, if you had actually read my reply, you’d know that I had already modified tasks.json with those values, even linking to the exact same page you just provided.

More explanation:

Hmm…

I cannot say what went wrong here.

Saying your solution is not a solution?
Or that the linked section is the solution needed.

A post exists beyond the poster. You are not the original poster, still you found this post and the other post.

Corrections are made to put the learners in right direction. If someone made a mistake, I would try to correct it. If I made a mistake, someone would try to correct me.

That is how online communities and forums work. Or at least this one does. As mentioned by the moderating discobot and its tutorial.