Having a decent amount of trouble trying to seperate source files.

So I'm trying to do an example in C++ for Dummies about making a header file and I keep having problems compiling it. I'm using Dev C++ and I have three separate files one of them is a header

main.cpp:
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <stdlib.h>
#include <string>
#include "safestuff.h"
int main (int argc, char *argv[])
{
    cout << "Surprise, surprise!" <<endl;
    cout << "The combination is (once again)" <<endl;
    cout << SafeCracker(12) << endl;
    return 0;
}


Which is giving me these errors:
safestuff.h `string' does not name a type
`cout' undeclared (first use this function)
`endl' undeclared (first use this function)
`SafeCracker' undeclared (first use this function)

The header file safestuff.h
string SafeCracker (int SafeID);
Won't even do anything.

And the file that contains the function safestuff.cpp
1
2
3
4
#include <string>
string SafeCracker(int SafeID) {
       return "13-26-16";
       }


Gives me another "`string' does not name a type."

I'm pretty new at this, and have a really basic understanding right now.
I'm doing exactly what the book says to do, but it just doesn't seem to work.






Last edited on
Don't you just love it when book examples fail? Makes me wonder how people actually get paid to write that crap -- do they even bother testing it?

Anyway the problem here is you're outside of std namespace. Do any one of the following:

1) change all occurances of string to std::string

2) put using std::string; after #include <string> in both .cpp files

3) put using namespace std; after #include <string> in both .cpp files

Newbies tend to like #3 because it saves a lot of "std::" typing in the long run, although it's arguably bad practice. All 3 solutions should work.

edit:

It also probably should be telling you to #include "safestuff.h" in safestuff.cpp, although I guess that isn't 100% necessary.

EDIT again -

crap I skipped right over your cout/endl errors. The above #1 and #2 solutions will only work to solve the string errors. #3 will fix all errors.

to fix those other errors the solutions are similar:

1) replace cout with std::cout and replace endl with std::endl
or
2) put using std::cout; and using std::endl; at the top of your cpp file (after the includes) or at start of your main() function (between lines 6 and 7)
or
3) same solution as #3 above. using namespace std; above the includes fixes all these errors. But defeats the whole point of using namespaces.
Last edited on
Normally I add using namespace std;, just because it makes it easier. I had tried adding it to the files before posting this but now its giving me the error
[Linker error] undefined reference to `SafeCracker(int)'
in main.cpp

and
[Linker error] undefined reference to `WinMain@16'
in safestuff.cpp

followed by
ld returned 1 exit status
in both of them.

In regards to your first edit, the next example in the book cleans it up by changing a few things like that, but it also said to include the header file in the header file which made my compiler have a conniption.
Last edited on
undefined reference to `SafeCracker(int)'


I'm not familiar with Dev C++, but if it works like other IDEs, you add several files to your "project" and then when you build your project it compiles and links all of your files.

This error indicates that the linker cannot find the body of your SafeCracker function. The most likely reason for this is that "safestuff.cpp" is not part of your project.

Are you making two seperate projects and trying to build each of them independently? Because if so that won't work. Both .cpp files should be in the same project.


edit:

but it also said to include the header file in the header file

haw... is this "C++ for Dummies" or "C++ by Dummies"?

Jesting aside -- I already have my doubts as to the quality of this book. When code examples don't compile and it tells you to make gaping errors (like having a header include itself... wtf?) it's not a good sign. You might have even more serious issues in later chapters.

I'd recommend something better if I knew anything. I guess something is better than nothing.
Last edited on
I'll try that, thanks for the help by the way.

Ah finally, thanks again. I've got it all worked out now.

Edit:
About C++ for Dummies. A guy I know who is a computer science major told me that it was a little bit shoddy, but so far I've been able to pull out most of the errors that I've seen in the examples.
The whole thing with including the header file in the header file seemed a little bit dumb to me when I saw it the first time anyway. Also, a couple times there has been a bullet underneath the example where the author is like, "oh I screwed that up."
Last edited on
I love the Internet! Thanks guys. I was having the same problem.

After I "
"put using namespace std; after #include <string> in both .cpp files" "

the program compiled without any problems.

Regarding books, I just bought
Sam's Teach Yourself C++ in One Hour a Day

It has a 2009 copyright (6th edition) so hopefully most of the bugs have been edited. The Sam's book seems to have a better section on STL. I am interested in MySQL++ which uses STL.

Scott
Don't get me started on these 'Teach yourself <insert programming language here> in 24 hours' type books.

The Title is totally bogus and misleading. There is NO WAY anyone could learn C++ in that time.
C++ for Dummies has a Microsoft bias. It uses Dev-C++ as the IDE. With Ubuntu, I use the Code Blocks IDE for Linux. Code Blocks automatically puts "using namespace std;" in the source code.
C++ for Dummies has a Microsoft bias. It uses Dev-C++ as the IDE.
What? That's what you call a "Microsoft bias"? That's child's play. To really be biased, it should use VC++, always include windows.h, and use only TCHAR * as strings. Let's not forget calling Alloc() from MFC instead of using new or malloc().
OK. Maybe not too MS biased. The C++ Dummies All-In-One Desk Reference has "7 Books in 1." Only Book 6 (C++.Net) and Book 7 (Visual Studio 6.0 and MFC) arer totally Microsoft oriented.
Topic archived. No new replies allowed.