0

So I was trying to access a method that is defined in another class and has the prototype in the header. I'm pretty positive I defined it but it keeps popping up undefined reference to SafeCracker.

Main.cpp

#include <iostream>
#include "mystuff.h"
using namespace std;

void BigDog(int KibblesCount);

int main()
{
    cout << SafeCracker(1);
    return 0;
}

mystuff.cpp

#include <iostream>

using namespace std;

string SafeCracker(int SafeID)
{
    return "123456";
}

mystuff.h

using namespace std;

#ifndef MYSTUFF_H_INCLUDED
#define MYSTUFF_H_INCLUDED

string SafeCracker(int SafeID);


#endif // MYSTUFF_H_INCLUDED
  • Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Borgleader Jul 13 '19 at 15:52
  • [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Borgleader Jul 13 '19 at 15:53
  • 1
    *"...trying to access a method that is defined in another class ..."* there are no classes defined in *any* of the source you posted. Produce a *real* [mcve], post the require files for *us* to produce the same result you are, and we can probably help. And fyi, `std::string` requires `` inclusion, and it is exactly *nowhere* in any of your posted source. The links provided above are important, so *read them*. – WhozCraig Jul 13 '19 at 16:25

1 Answers1

0

Here it tells you that you have an undefined reference, so you don't really have a problem with the prototype.
Had you forgotten to include the header file that contains the prototype you would have gotten something like

main.cpp: In function ‘int main()’:
main.cpp:8:13: error: ‘SafeCracker’ was not declared in this scope
     cout << SafeCracker(1);

Your undefined reference is a linker error. The most likely cause would be that you did not use mystuff.cpp when compiling

If you're compiling from the command line, you should give both files as parameters.
If you're using an IDE that calls the compiler, make sure that the file is part of the project.

For example in Code::Blocks right-click on the file name and go "add to project" (If I remember correctly)

It is also possible that you made a typo in the function declaration in mystuff.cpp (that doesn't seem to be the case here though)


Now there is one important thing about your code you should take note of:

It is very bad practice to put a using namespace in a header file.

using namespace std; in a .cpp source file is mostly up to you, and that using statement will only apply to that particular file.
But if you put it in a header file that is meant to be included through #include , the using there will be forced upon any code that includes it.

Here is an example:
main.cpp

#include <iostream>

// including mystuff.h to use that awesome SafeCracker()
#include "mystuff.h"

// I need to use an std::map (basically an associative array)
#include <map>

// the map of my game
class map
{
    int tiles[10][10];
};

int main()
{
    // The std map I need to use
    std::map<int, int> mymappedcontainer;

    // The map of my game I need to use
    map mytiles;

    // The reason why I need to include mystuff.h
    cout << SafeCracker(1);
    return 0;
}

Normally, my class map should not be a problem since the map I included from the standard library is inside the namespace std, so to use it you would need to go std::map.
The problem here is that, since mystuff.h has using namespace std; in it, the symbol map is already used, and that creates a conflict.

You do not now who will use your header files, or if you will use them again a long time from now, and maybe then you will want to use name that is already used in the std namespace.
I advise you to use std:: before things taken from the standard libraries instead (std::string instead of just string for example)


PS: In C++, "class" refers to a class data structure, and the functions you made here are not part of any class. You should say "defined in another file" or "defined in another translation unit" instead

Speedphoenix
  • 204
  • 2
  • 8
  • 19
  • Thank you for the advice! It seems like my code is right, but it doesn't work when I don't include #include "mystuff.cpp" in the main file. Is it possible that I set up codeblocks or the c++ environment wrong? I'm pretty sure I have the files in the same project. – errandstudy Jul 14 '19 at 15:48
  • @errandstudy Going `#include "filename.cpp"` is also a pretty bad practice (don't include souce code that way if you can. Only .h header files) If you use Code Blocks, maybe your files are visible but not part of the current project. Is the file shown under `Sources` or `Header` in the project view on the left of the screen? If you right click on the filename of mystuff.cpp on the tabs bar, does it say add file to active project? (do click if it does) – Speedphoenix Jul 14 '19 at 23:15