6

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
5
  • You don't know what you're not? You're not the president of space, that's one thing. You're also not linking all your files together. Commented Oct 26, 2012 at 0:18
  • 2
    Show us how you're building it - something is wrong with your linking stage. Commented Oct 26, 2012 at 0:19
  • 1
    That error message means the compiler is unable to find the definition of SafeCracker (that is, the actual code for the function). Have you added the file that contains the function to your code blocks project? Commented Oct 26, 2012 at 0:22
  • It is part of the code blocks project. I don't really know how it is being built code blocks has been handling that part. Is there something I need to do in the compiler settings? Commented Oct 26, 2012 at 0:25
  • as a side note, you're using the string data type but not including the string class. You should put "#include <string>" (without quotes) either above or under "#include <iostream>". Some compilers include header files automatically for you when they detect one is missing but this is not a standard so it shouldn't be relied upon. Commented Oct 26, 2012 at 2:08

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

7 Comments

No good reason why I was going along with what I was shown to be honest. I am not entirely sure how to compile directly I will have to look into that
Ok. Well it is probably going to build on that..by build I mean your tutorial. What operating system are you running on? You can type the commands I gave you into a terminal/console window (when you are in the same directory as the code) to run them. Codeblocks is just running these commands as well, it is a good idea to know how it is all working behind the scenes of your ide.
Im using Windows 7 64-bit Home Premium
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).
holy hell that did it. I created them in the project but i guess they weren't added. I am really need to look into the background work of C::B. Thank you all this has been driving me nuts.
|
1

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.

2 Comments

@jett -- Never rely on that. #include what you use. Besides, SafeCracker.cpp should not be including iostream for the simple reason that that file is not using iostream. Don't #include what you don't use. That file is defining the function SafeCracker. The function is declared in some header as returning a std::string. Not a pointer, not a reference. Just std::string. That header is where the #include <string> belongs.
@DavidHammen I agree you should include what you use. I referenced that in my answer above. I was just stating that this is not the solution to the problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.