Hello, I received undefined reference error at a project with three files: SUM.h, SUM.cpp, main.cpp. Give me "undefined reference to 'SUM::Sumarise()' " error. What can I do?
==Build: Debug in Gabi(compiler:GNU GCC Compiler)
obj\Debug\main.o In function 'main':
D:\C++\main.cpp --- 11 --- undefined reference to 'SUM::Summarise()'
D:\C++\main.cpp --- 12 --- undefined reference to 'SUM::Summarise()'
error: ld returned 1 exit status
==Build failed: 3 error(s). o warning(s), (0 min. 1sec)
I tried it but It do not work, I have the same error. Is it possible be a bad of code::blocks? I will uninstall and install again code::blocks. I do not know what to do.
#ifndef CLASS_SUM_HEADER
#define CLASS_SUM_HEADER
class Sum {
private:
int a {};
int b {};
public:
int get_first() const;
int get_second() const;
void set_first(int val);
void set_second(int val);
int summarise() const;
};
#endif // CLASS_SUM_HEADER
I reinstall code::blocks again and I have same error. What can I do?
The file SUM.h was created File/New/File/C/C++header/ I named the file, clicked ... and indicated the project director.
The file SUM.cpp was created File/New/File/C/C++source/ I named the file, clicked ... and indicated the project director.
What is it wrong?
I used a project. Gabi is name of project.
I did not understand Enoizat's post: I know that a header file has .h extension, why has he written "Sum.hpp" name of header file? I have clicked File/New/File/ C/C++header and the program has created a file with .h extension.
Unfortunately I do not know how can I post some picture in this forum?
Sorry for the delay. Was not feeling well earlier.
I put your projects in CB, but left out the "Sum.cpp" as part of the project which reproduced your error. When I figured out how to add the file to the project, (not familiar with CB), the error went away.
I do not know how long you have been using CB, so I will just say it. On the left in the window marked "Management", under the heading of "source" you should see the files "main.cpp" and "Sum.cpp". If not then you will need to add the "Sum.cpp" file to the project.
If that does not work try creating a new project with a new name.
For what its worth I created the program in VS2017. First with all the files as part of the project and it compiled fine with no errors. When I removed the "Sum.cpp" file from the project I received VS's version of the same error.
This tells me that "Sum.cpp" is not being compiled into the project and at link time the call to "fx.Summarise()" is not being found.
Also in "main" you have the line fx.Summarise(); before the "cout" statement. This function call does nothing. The one in the "cout" statement is all you need because it does use the returned value of the function.
This probably means that you didn't create a project. So from the "File" menu select "New" then "Project...". Then select "Console application" from the selection window, then select a C++ project, lastly enter the project name and select the directory where you want to create this project.
I did not understand Enoizat's post: I know that a header file has .h extension, why has he written "Sum.hpp" name of header file?
The ".h" extension are used with C header files and tend to make one think of C header files when used.
I believe that when C++ was created the ".cpp" extension was used to show the difference between the Cs' ".c" extension and C++s' ".cpp" extension. But history was never my strong subject, so I may have this a bit wrong. I hope the idea of the concept is understandable.
IMHO I like using the ".hpp" extension when working with C++ programs, but as jlb said using either is fine.
Unfortunately I do not know how can I post some picture in this forum?
I do not know of any way to do this either, but I have seen people use outside sources like https://postimages.org/ and then put the link in the message. There are others, but I do not have links for them yet.
Just in case: when you post a link there should be nothing or a space before the "http" and either a space or "Enter" after the last character.
And for code that may be to large to fit in a message there are also:
Well I started with VS2017 because that is what I use the most. Today is the first time I really used CB and some of it is new to me.
I would refer to jlb's last post for directions as I am not quite sure how I did everything yet.
You might still try closing down CB with the problem and open a new instance of the program and start over. This at least leaves your old files to copy and paste into a new project with a new name.
As mentioned I have seen a problem with VS where it would not see or accept the changes and use old files to build a project. I have even had to go find the ".exe" file and manually delete it to make a rebuild work. So sometimes it is easier to start over than trying to fix your problem until you have gained more experience with the IDE.
Short of giving someone remote access to your computer it is a bit of a guessing game to figure out what you did wrong.
It sounds like what jlb said:
This probably means that you didn't create a project.
and I feel that this is a strong possibility.
Oh BTW in Enoizat's post the header guards are worth learning and understanding:
I right clicked on name of project, clicked "properties" and clicked tag "Build targets", and pointed SUM.cpp. Now, my compiler see this file end the error has disappear!!!