# Errors with creating header files

**URL:** <https://forum.codewithmosh.com/t/errors-with-creating-header-files/21098>\
**Category:** C++\
**Created:** [June 27, 2023, 12:20am UTC](https://forum.codewithmosh.com/t/errors-with-creating-header-files/21098 "2023-06-27T00:20:07Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![qwertyuiop](https://avatars.discourse-cdn.com/v4/letter/q/db5fbb/32.png) [@qwertyuiop](https://forum.codewithmosh.com/u/qwertyuiop)\
**Post date:** [June 27, 2023, 12:20am UTC](https://forum.codewithmosh.com/t/errors-with-creating-header-files/21098/1 "2023-06-27T00:20:08Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![POMPOMBALLS](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/pompomballs/32/6610_2.png) [@POMPOMBALLS](https://forum.codewithmosh.com/u/POMPOMBALLS)\
**Post date:** [June 29, 2023, 12:28pm UTC](https://forum.codewithmosh.com/t/errors-with-creating-header-files/21098/2 "2023-06-29T12:28:33Z")

</div>

> [@qwertyuiop](#):
>
> int main() {  
> rectangle rectangle;

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,

> [@qwertyuiop](#):
>
> **main.cpp:**  
> #include “rectangle.hpp”  
> #include  
> using namespace std;

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

---

<div class="post-metadata">

**Author:** ![Filip](https://avatars.discourse-cdn.com/v4/letter/f/919ad9/32.png) [@Filip](https://forum.codewithmosh.com/u/Filip)\
**Post date:** [July 31, 2023, 7:39pm UTC](https://forum.codewithmosh.com/t/errors-with-creating-header-files/21098/3 "2023-07-31T19:39:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![POMPOMBALLS](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/pompomballs/32/6610_2.png) [@POMPOMBALLS](https://forum.codewithmosh.com/u/POMPOMBALLS)\
**Post date:** [August 1, 2023, 4:02pm UTC](https://forum.codewithmosh.com/t/errors-with-creating-header-files/21098/4 "2023-08-01T16:02:22Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![iambetaraybill](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/iambetaraybill/32/7463_2.png) [@iambetaraybill](https://forum.codewithmosh.com/u/iambetaraybill)\
**Post date:** [December 12, 2023, 2:41pm UTC](https://forum.codewithmosh.com/t/errors-with-creating-header-files/21098/5 "2023-12-12T14:41:58Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![c-dot-gonz](https://avatars.discourse-cdn.com/v4/letter/c/898d66/32.png) [@c-dot-gonz](https://forum.codewithmosh.com/u/c-dot-gonz)\
**Post date:** [September 22, 2024, 9:03pm UTC](https://forum.codewithmosh.com/t/errors-with-creating-header-files/21098/6 "2024-09-22T21:03:54Z")

</div>

I’ve received the same error in VSC:

```auto
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:

```auto
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:

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

```

Rectangle.hpp:

```auto
#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:

```auto
#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:

```auto
#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.

---

<div class="post-metadata">

**Author:** ![c-dot-gonz](https://avatars.discourse-cdn.com/v4/letter/c/898d66/32.png) [@c-dot-gonz](https://forum.codewithmosh.com/u/c-dot-gonz)\
**Post date:** [September 23, 2024, 4:35am UTC](https://forum.codewithmosh.com/t/errors-with-creating-header-files/21098/7 "2024-09-23T04:35:47Z")

</div>

After digging around online, I found a [YouTube video](https://www.youtube.com/watch?v=Wx5hac9LDdA) that solved the issue for me. I had to add “#include “Rectangle.cpp”” in my main.cpp file.

---

<div class="post-metadata">

**Author:** ![andyfrankk](https://avatars.discourse-cdn.com/v4/letter/a/c37758/32.png) [@andyfrankk](https://forum.codewithmosh.com/u/andyfrankk)\
**Post date:** [September 25, 2024, 5:02am UTC](https://forum.codewithmosh.com/t/errors-with-creating-header-files/21098/8 "2024-09-25T05:02:31Z")

</div>

Hi, can you try this

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

```auto
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…[.](https://www.rirosports.com/)…

---

<div class="post-metadata">

**Author:** ![POMPOMBALLS](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/pompomballs/32/6610_2.png) [@POMPOMBALLS](https://forum.codewithmosh.com/u/POMPOMBALLS)\
**Post date:** [September 25, 2024, 7:33am UTC](https://forum.codewithmosh.com/t/errors-with-creating-header-files/21098/9 "2024-09-25T07:33:46Z")

</div>

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

Watch [this](https://www.youtube.com/watch?v=H4s55GgAg0I).

---

<div class="post-metadata">

**Author:** ![c-dot-gonz](https://avatars.discourse-cdn.com/v4/letter/c/898d66/32.png) [@c-dot-gonz](https://forum.codewithmosh.com/u/c-dot-gonz)\
**Post date:** [September 25, 2024, 9:10am UTC](https://forum.codewithmosh.com/t/errors-with-creating-header-files/21098/10 "2024-09-25T09:10:34Z")

</div>

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:

> [@Organizing Functions in Files in VSC](https://forum.codewithmosh.com/t/organizing-functions-in-files-in-vsc/24537/8):
>
> Nvm all, my friend helped me solve it. It was complications with my tasks.json file, not launch.json. Here’s my code for the poor soul trying to solve this in the future: main.cpp #include \<iostream\> #include "utils/greet.hpp" using namespace std; int main(){ greet("Alan"); return 0; } greet.cpp under my “utils” subfolder: #include \<iostream\> using namespace std; void greet(string name){ cout \<\< "Hello " \<\< name; } greet.hpp under “utils” subfolder: #ifndef UTILS\_GREET …

Which looks like it got the code from here:

> **[Get Started with C++ and MinGW-w64 in Visual Studio Code](https://code.visualstudio.com/docs/cpp/config-mingw)**
>
> Configuring the C++ extension in Visual Studio Code to target g++ and gdb on a MinGW-w64 installation

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](https://www.youtube.com/watch?v=8IWFeUkZ4bo) had a user running the code from the Run and Debug menu so I decided to try it and it’s what works.

---

<div class="post-metadata">

**Author:** ![simin](https://avatars.discourse-cdn.com/v4/letter/s/9fc29f/32.png) [@simin](https://forum.codewithmosh.com/u/simin)\
**Post date:** [September 26, 2024, 6:24am UTC](https://forum.codewithmosh.com/t/errors-with-creating-header-files/21098/11 "2024-09-26T06:24:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![POMPOMBALLS](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/pompomballs/32/6610_2.png) [@POMPOMBALLS](https://forum.codewithmosh.com/u/POMPOMBALLS)\
**Post date:** [September 26, 2024, 8:18am UTC](https://forum.codewithmosh.com/t/errors-with-creating-header-files/21098/12 "2024-09-26T08:18:29Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![POMPOMBALLS](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/pompomballs/32/6610_2.png) [@POMPOMBALLS](https://forum.codewithmosh.com/u/POMPOMBALLS)\
**Post date:** [September 28, 2024, 4:00pm UTC](https://forum.codewithmosh.com/t/errors-with-creating-header-files/21098/13 "2024-09-28T16:00:11Z")

</div>

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](https://code.visualstudio.com/docs/cpp/config-mingw#_modifying-tasksjson)

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

---

<div class="post-metadata">

**Author:** ![c-dot-gonz](https://avatars.discourse-cdn.com/v4/letter/c/898d66/32.png) [@c-dot-gonz](https://forum.codewithmosh.com/u/c-dot-gonz)\
**Post date:** [September 28, 2024, 4:58pm UTC](https://forum.codewithmosh.com/t/errors-with-creating-header-files/21098/14 "2024-09-28T16:58:04Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![POMPOMBALLS](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/pompomballs/32/6610_2.png) [@POMPOMBALLS](https://forum.codewithmosh.com/u/POMPOMBALLS)\
**Post date:** [September 29, 2024, 12:01pm UTC](https://forum.codewithmosh.com/t/errors-with-creating-header-files/21098/15 "2024-09-29T12:01:32Z")

</div>

More explanation:

> [@c-dot-gonz](#):
>
> 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

> <https://stackoverflow.com/questions/77646301/whats-the-difference-between-pressing-f5-and-clicking-the-run-button-at-the-to>

> <https://stackoverflow.com/questions/77652090/whats-the-difference-between-the-debug-c-c-file-run-code-and-run-c-c>

---

<div class="post-metadata">

**Author:** ![POMPOMBALLS](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/pompomballs/32/6610_2.png) [@POMPOMBALLS](https://forum.codewithmosh.com/u/POMPOMBALLS)\
**Post date:** [September 29, 2024, 12:07pm UTC](https://forum.codewithmosh.com/t/errors-with-creating-header-files/21098/16 "2024-09-29T12:07:20Z")

</div>

Hmm…

I cannot say what went wrong here.

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

> [@c-dot-gonz](#):
>
> I really don’t see why this response was worth posting days after I found and reported a solution

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](https://forum.codewithmosh.com/u/discobot) and its tutorial.
