Thread: Working with multiple source file

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    102

    Working with multiple source file

    Currently I'm following a tutorial on C++ All-in-One for Dummies @PG114 -> Dividing Between Source-Code Files. I'm trying to run a console program that are made up of 2 source file:
    • main.cpp
    • source2.cpp

    The problem that I'm facing is whenever when I try to compile it, it will always show me an error message: " undefined reference to 'BigDog(int)". source2.cpp seems doesn't working.

    I can assure that the code are error free as I copy and paste from the book. So what's the problem with it. I'm using Codeblock.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Did you forget about the header file?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2011
    Posts
    102
    This's what the book call me to do:
    First code
    Code:
    #include <iostream>
    
    using namespace std;
    
    void BigDog(int KibbleCount);
    
    int main(){
        BigDog(3);
        return 0;
    }
    Second code
    Code:
    #include <iostream>
    
    using namespace std;
    
    void BigDog(int KibblesCount){
            cout << "I'm a lucky Dog" << endl;
            cout << "I have " << KibbleCount << "pieces of food" << endl;
    }
    Do I need header? Even I try header on another tutorial it's even not working. Sorry if i don't provide a screenshot because my internet is too slow. Gonna wait till next month.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Oh, okay. In this case you don't need a header because you are directly forward declaring the BigDog function. This forward declaration would otherwise be placed in a header to be included.

    Anyway, this means that the problem is that you compiled the source files separately without linking them to form the final executable program. Since you are using Code::Blocks for your IDE, it means that you probably did not add both source files to the same project.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Aug 2011
    Posts
    102
    Thank you. Found the solution. I didn't include them to debug and release. That's why. Thank you.

  6. #6
    Registered User
    Join Date
    Aug 2011
    Posts
    102
    Hi. I just came another problem on another tutorial that involved a header. Why I'm getting error again even I copy exactly and I had linked the file properly.
    Code 1
    Code:
    #include <iostream>
    
    using namespace std;
    
    string SafeCracker(int SafeID){
        return "Helllo" ;
    }
    Code 2
    Code:
    #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;
    }
    header:
    Code:
    using namespace std;
    
    #ifndef SAFESTUFF_H_INCLUDED
    #define SAFESTUFF_H_INCLUDED
    
    string SafeCracker(int SafeID);
    
    #endif // SAFESTUFF_H_INCLUDED
    Error message that I got for header file: error: 'string' does not name a type

  7. #7
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    "using namespace" inside a header file is bad. And why is it specified before the header guard?!

    Anyway, you need to #include <string> in both source files.
    Devoted my life to programming...

  8. #8
    Registered User
    Join Date
    Aug 2011
    Posts
    102
    I tried #include <string or string.h> and it still not working. Anyway, this chapter isn't focus on "using namespace" and it's on Dividing Between Source-Code Files. So I think it's not very important. Later the book will clear up everything.

  9. #9
    Registered User
    Join Date
    Aug 2011
    Posts
    102
    I tried the file that's created by the author and it work. I paste my code which i type recently into the author project , it work like charm. idk what's happen. I think maybe it's due to header setting. Something like that.

  10. #10
    Registered User
    Join Date
    Aug 2011
    Posts
    102
    ok it's solved. Probably same problem like the recent one. Anyway thanks everyone.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Basically:
    • In header files, do not use using declarations or using directives except within a restricted scope. Consequently, you would typically fully qualify names used in a header file.
    • Include a header (or otherwise forward declare) if you are going to use a name.


    Thus, your header should be like this:
    Code:
    #ifndef SAFESTUFF_H_INCLUDED
    #define SAFESTUFF_H_INCLUDED
    
    #include <string>
    
    std::string SafeCracker(int SafeID);
     
    #endif // SAFESTUFF_H_INCLUDED
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Mar 2012
    Posts
    110
    Quote Originally Posted by ncode View Post
    ok it's solved. Probably same problem like the recent one. Anyway thanks everyone.
    There is nothing that is as brutally honest as a compiler. I don't know about you, but I never dismiss typo.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 10-04-2011, 10:17 PM
  2. facing problem with multiple source file porgram.
    By learning"c" in forum C Programming
    Replies: 10
    Last Post: 05-15-2011, 12:08 PM
  3. multiple source files
    By ericad in forum C Programming
    Replies: 10
    Last Post: 01-24-2010, 09:24 AM
  4. Working with multiple source files
    By abh!shek in forum C Programming
    Replies: 17
    Last Post: 06-03-2008, 11:23 AM
  5. Replies: 1
    Last Post: 05-01-2003, 02:52 PM