Undefined Reference using KDevelop 4.4.1 - c++

I'm a beginner with C++. I wrote the following:
// GradeBook.h
#include <iostream>
#include <string>
using namespace std;
class GradeBook {
public:
GradeBook(string); // constructor that initializes courseName
void setCourseName(string); // function that sets the course name
string getCourseName(); // function that gets the course name
void displayMessage(); // function that displays a welcome message
private:
string courseName; // course name for this GradeBook
};
// GradeBook.cpp
#include <iostream>
#include "GradeBook.h"
using namespace std;
GradeBook::GradeBook(string name)
{
setCourseName(name);
}
void GradeBook::setCourseName(string name)
{
courseName = name;
}
string GradeBook::getCourseName()
{
return courseName;
}
void GradeBook::displayMessage()
{
cout << "Welcome to the grade book for\n" << getCourseName() << "!" << endl;
}
// main.cpp
#include <iostream>
#include "GradeBook.h"
using namespace std;
int main()
{
GradeBook gradeBook1("CS101 Introduction to C++ Programming");
GradeBook gradeBook2("CS102 Data Structures in C++");
cout << "gradeBook1 created for course: " << gradeBook1.getCourseName()
<< "\ngradeBook2 created for course: " << gradeBook2.getCourseName()
<< endl;
}
I am using KDevelop 4.4.1, then I proceed to execute my main.cpp and I got:
/home/brallan/projects/Hola/build> make
Linking CXX executable hola
CMakeFiles/hola.dir/main.o: In function main':
/home/brallan/projects/Hola/main.cpp:8: undefined reference to GradeBook::GradeBook(std::string)'
/home/brallan/projects/Hola/main.cpp:9: undefined reference to GradeBook::GradeBook(std::string)'
/home/brallan/projects/Hola/main.cpp:12: undefined reference to GradeBook::getCourseName()'
/home/brallan/projects/Hola/main.cpp:11: undefined reference to GradeBook::getCourseName()'
collect2: error: ld returned 1 exit status
make[2]: [hola] Error 1
make[1]: [CMakeFiles/hola.dir/all] Error 2
make: [all] Error 2
Failed
If I run the same code from Eclipse Juno CDT, it return me:
gradeBook1 created for course: CS101 Introduction to C++ Programming
gradeBook2 created for course: CS102 Data Structures in C++
Can anyone help me to run it from KDevelop?
UPDATE: Based on the comments, KDevelop isn't compiling other files in the project :s
I guess this is the problem to be solved.

First, add the line #error (or any other syntax error) to the end of GradeBook.cpp. Ensure you get a compilation error for that line when you try to build it. If not, check spelling and capitalization of the file reference from the project or makefile.
If you do get a syntax error, or if you don't but you can't figure out why the file isn't being referenced, try this next: Remove the #error from GradeBook.cpp, and add #include "GradeBook.cpp" to the end of main.cpp. This serves two purposes: It get you going (should now be able to build and run) and it helps narrow the problem (if it works, you know the problem is with referencing GradeGook.cpp, rather than with its contents).

It seems you are not compiling GradeBook.cpp

In the project folder, there is a file called CMakeList.txt and on it are the files that are part of the project. I tried to add the file GradeBook.cpp to add_executable line, but still did not work. However, when I replaced the file names in lower case, and turn modify the line that I described, everything worked properly. I'm not sure what is the mistake if the file name has no upper or similarly if I add it to this list exactly as it is called.
Then, I renamed files gradebook.h and gradebook.cpp and added gradebook.cpp to add_executable line.

Related

C++ CMake not referencing function with .h properly

SOLUTION:
Just don't be a noob and run make in the same directory as cmake
Thanks for your answers!
I'm starting with C++ programming and I'm trying to understand how to properly reference a function with .h and .cpp file. I have following files:
\\func.h
#ifndef FUNC_H
#define FUNC_H
int charout(int a, char b);
#endif
\\func.cpp
#include <iostream>
#include "func.h"
using namespace std;
int charout(int a, char b)
{
cout << a;
cout << b;
return 0;
}
\\main.cpp
#include <iostream>
#include "func.h"
using namespace std;
int main ()
{
int a; char b;
cout << "insert an integer " << endl;
cin >> a;
cout << "insert a letter " << endl;
cin >> b;
charout(a,b);
return 0;
}
I am compiling using CMake (with func.h in folder 'include') with following structure:
# Declare the version of the CMake API for forward-compatibility
cmake_minimum_required(VERSION 2.8)
# Declare the name of the CMake Project
project(manual)
# Add the directory to search for header files
include_directories(include)
# Define an executable target
add_executable(main func.cpp main.cpp)
When I try to make main.cpp I am receiving an error:
make main g++ main.cpp -o main /tmp/cctlsXUG.o: In function
main': main.cpp:(.text+0x80): undefined reference tocharout(int,
char)' collect2: error: ld returned 1 exit status : recipe
for target 'main' failed make: *** [main] Error 1
Can you please take a look and let me know where am I doing a mistake?
I will appreciate your feedback. I'm really stuck with this. Cheers!
UPDATE:
OK I managed to compile it with g++ func.cpp main.cpp - o main when I was trying to compile the same code with gcc I got errors of sort undefined reference to std::cout'
I've found out that gcc does not give an access to std of C++. Can I somehow fix my CMake files to use g++ instead of gcc? The project I have to deliver is supposed to have a CMakeLists.txt included.
Finally the last question: why does CMake works fine with function declaration and main in the same file, but not when I split the function into header, cpp and main?

simple c++ code does not compile (linker command failed with exit code 1)

I'm new to C++ and studying the Dietel book. On the book, it has some example codes for classes and interfaces
Gradebook.h
#ifndef GradeBook_h
#define GradeBook_h
#endif /* GradeBook_h */
#include <string>
class GradeBook
{
public:
explicit GradeBook( std::string );
void setCourseName( std::string );
std::string getCourseName() const;
void displayMessage() const;
private:
std::string courseName;
};
Gradebook.cpp
#include <iostream>
#include "GradeBook.h"
using namespace std;
GradeBook::GradeBook( string name )
{
courseName = name;
}
void GradeBook::setCourseName( string name )
{
courseName = name;
}
string GradeBook::getCourseName() const
{
return courseName;
}
void GradeBook::displayMessage() const
{
std::cout << "Welcome to the grade book for " << getCourseName() << std::endl;
}
main.cpp
#include <iostream>
#include "GradeBook.h"
using namespace std;
int main()
{
GradeBook gradeBook1("CS 101 Introduction to C++ Programming");
GradeBook gradeBook2("CS 102 Data Structures in C++");
cout << "gradeBook1 : " << gradeBook1.getCourseName() << endl;
cout << "gradeBook2 : " << gradeBook2.getCourseName() << endl;
}
So, I am trying to compile this on my mac terminal using g++ main.cpp -o example.out. But it seems that this constantly gives me an error saying that
Undefined symbols for architecture x86_64:
"GradeBook::GradeBook(std::__1::basic_string, std::__1::allocator >)", referenced from:
_main in main-0602c7.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I have tried getting rid of most of the function declarations and function implementations except for the constructor and the member variable, but it seems to be giving me the same error still.
I think I copied the code exactly from the book, but I do not understand what I am doing wrong. Any help would be appreciated.
You'll have to compile all sources, so add your GradeBook class implementation too
g++ main.cpp GradeBook.cpp -o example.out
~~~~~~~~~~~~~
As already mentioned in above example, you have to mention both source files (main.cpp and GradeBook.cpp) while compiling the code.
This will work.
However, there is one more potential problem in your code.
#ifndef GradeBook_h
#define GradeBook_h
#endif /* GradeBook_h */
There is no code inside ifndef-endif guard. You need to put the complete code in .h file inside ifndef-endif. Otherwise when you work on a larger project and GradeBook.h is getting included from multiple places, you might get redeclaration error.

Simple Object-Oriented Cat has not been declared

Here is a video i made with visuals of my issue
https://www.youtube.com/watch?v=KqvQivVfAdI
I am following an online tutorial to learn C++. Everything has been going great so far. I am using code::blocks and the person teaching is using Eclipse.
I have this specific code he told me to write, and it isn't working with code::blocks. I'm getting an error in Cat.cpp:
'Cat' has not been declared
Is there something I have to do differently since I am in code::blocks?
His video, if interested...
Here is my code:
main.cpp
#include <iostream>
#include "Cat.h"
using namespace std;
int main(){
Cat cat1;
cat1.speak();
cat1.jump();
return 0;
}
Cat.h
#ifndef CAT_H_INCLUDED
#define CAT_H_INCLUDED
class Cat{
public:
void speak();
void jump();
};
#endif // CAT_H_INCLUDED'
Cat.cpp
#include <iostream>
#include "Cat.h"
using namespace std;
/*Error on this line*/
void Cat::speak(){
cout << "Meouwwww1!!! " << endl;
}
/*probably on this on too */
void Cat::jump(){
cout << "Jumping to top of bookcase1" << endl;
}
#include <iostream>
using namespace std;
code put together
#ifndef CAT_H
#define CAT_H
class Cat
{
public:
Cat();
void speak();
private:
};
#endif // CAT_H
void Cat::speak()
{
cout << "Meouwww2!!!" << endl;
}
int main(){
Cat cat1;
cat1.speak(); /*error happening here now
error: id returned 1 exit status*/
return 0;
}
picture of errors
https://gyazo.com/bb3574ea504f2357c7ea2987facc9874
error: id returned 1 exit status
No. The actual error is
undefined reference to Cat::Cat()
error: ld returned 1 exit status
There is no any "id" which can return any exit status. There is ld, which is a linker. Its job is to create the executable file, after your files are compiled. It takes all your compiled files, and links them together. If there is a function which you call in your files, but it is not defined in your files, the linker searches for it in standard libraries, or in any additional libraries which you provided, and adds the function (and functions it calls) to your executable.
Here the problem is, you implicitly call for constructor for your class Cat, and you declare it, but you hadn't provide it. Obviously, the linker cannot find constructor for your class in standard libraries.
There are two ways to deal with it. One is to provide the constructor, i.e.
Cat::Cat() {
}
Alternatively, you may remove the line
Cat();
from your code. In this case, the default constructor will be generated authomatically. (For each class, the constructor without parameters is authomatically generated, unless any constructor is provided.)
You need to add the constructor and destructor for your class Cat.
In the header for a quick fix you could do:
Cat(){}
~Cat(){}

Error when trying to separating class into .h, .cpp

This is a minimal program that I made to understand this problem better.
ADT.h
#ifndef ADT_H
#define ADT_H
class ADT {
public:
void print();
};
#endif
ADT.cpp
#include <iostream>
#include "ADT.h"
using namespace std;
void ADT::print()
{
cout << "This program works." << endl;
}
testADT.cpp
#include <iostream>
#include "ADT.h"
using namespace std;
int main(void)
{
ADT sa;
sa.print();
return 0;
}
I compiled it with the vim/minGW compiler my school provided me like so:
g++ testADT.cpp
Which produced the following error:
C:\Users\King\AppData\Local\Tempcc6eoWAP.o:testADT.cpp(.text+0x15 reference to 'ADT::print()'
collect2.exe error: ld returned 1 exit status
Can you explain this error message and indicate the error in my code?
You didn't post the error, but I see that you're missing the semicolon after void print()in the header.
EDIT: That's a linker error. Each source file should be compiled into an object file; then the object files linked:
g++ -c -oADT.o ADT.cpp
g++ -c -otestADT.o testADT.cpp
g++ -oADT ADT.o testADT.o
You can also do it in one line as in michaeltang's answer, but then you can't recompile the sources individually (the 2 step method scales better).
You should also compile ADT.cpp
g++ -o testadt testADT.cpp ADT.cpp

Compiler/linker error "undefined reference"

Hi I am just starting to learn C++. I bought this big C++ for Dummies book and have been going through it. Its been really interesting so far but now I am stuck. I have been googling this problem, but to no avail. I am using I am using codeblocks 10.05 with GNU GCC.
I keep getting an error that says:
In function 'main':
undefined reference to 'SafeCracker(int)'
The code isn't complicated. I am just new and am extremely frustrated. I don't want to skip over this part; I want to know what is going on.
Main:
#include <iostream>
#include "safestuff.h"
using namespace std;
int main()
{
cout << "Surprise, surprise!" << endl;
cout << "The combination is (once again)" << endl;
cout << SafeCracker(12) << endl;
return 0;
}
Function:
#include <iostream>
using namespace std;
string SafeCracker(int SafeID)
{
return "13-26-16";
}
Header:
using namespace std;
#ifndef SAFESTUFF_H_INCLUDED
#define SAFESTUFF_H_INCLUDED
string SafeCracker(int SafeID);
#endif // SAFESTUFF_H_INCLUDED
You are not compiling the second file you listed along with the first one. Try compiling directly with gcc to understand this.
assuming your files are named:
main.cpp
SafeCracker.cpp
safestuff.h
This is what you are doing
gcc main.cpp
While you should be doing this
gcc main.cpp SafeCracker.cpp
Also, SafeCracker.cpp should be including the header file as well, just for clarity. Any reasons why you have them separated?
On another note, from seeing Daniel Hu's answer, <iostream> is automatically including <string> for you. You should not depend on this functionality, and should instead include <string> in each file that uses strings.
(From comment below)
You're probably trying to build your main.cpp as a stand-alone file. This will leave SafeCracker.cpp uncompiled. What you need is create a project in Codeblocks and add all three files to it (both *.cpp files as well as the *.h file).
I think it's because you did not #include <string>
C++ has to import the string library to use strings or else everything is treated as char arrays.