Community discussion forum

Newbie on headers

  • 7 months ago

    Hi Guys, my name is Richard and im new to the forum. I was wondering if someone could help me. I am starting to teach myself c++, using c++ for dummies. I am having problems compiling a code with headers. I have taken the code straight from the book and it still won't compile.
     
    This is the code:
     
    main.cpp
     
    #include <cstdlib>
    #include <iostream>
    #include <stdlib.h>
    #include <string>
    #include "safestuff.h"
     
    using namespace std;
     
    int main(int argc, char *argv[])
    {
    cout << "Surprise, surprise!" << endl;
    cout << "The combination is (once again)" << endl;
    cout << SafeCracker(12) << endl;
    system("PAUSE"); 
    return 0;
    }
     
    safestuff.cpp
     
    #include <string>
     
    using namespace std;
     
    string SafeCracker(int SafeID) {
    return "13-26-16";
    }
     
    safestuff.h
     
    string SafeCracker(int SafeID);
     
    has anyboy got any idea what i am doing wrong. any help would be appreciated.

    Thank you.

    Rich

  • Advertisement

    Simply the fastest line-level profiler for .NET ever

    “The low overhead means it has minimal impact on the execution of my program”
    Mark Everest, Development Team Leader, Renault F1 Team Ltd.

    Try out the new ANTS Profiler 4 for yourself. Download your 14-day trial now

  • 7 months ago

    Hi Richard,

    Welcom to developerfusion! Left Hug

    => About your code :

    - You don't need to include 'stdlib.h', just enough to include 'cstdlib', since it's the newer version of 'stdlib.h'. But it's not a problem.

    - The problem is : you should include 'string' in 'safestuff.h', then add the following lines at the first of 'safestuff.h' :

    #include <string>

    using namespace std;

    Please note that, you should include all of required header files in each separate file of your project.

  • 7 months ago

    Good point legendario3,

    But in this case, it's not required to include 'safestuff.h' in 'safestuff.cpp', since there is nothing in header file that is not defined in source file.

Post a reply

Enter your message below

Sign in or Join us (it's free).