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