Menu

problem with multiple files: dev on vista

codor kim
2008-04-21
2012-09-26
  • codor kim

    codor kim - 2008-04-21

    hello me the idiot here today have a second obstacle in learning c++ with dev-c++ on windows vista.

    i'm learning how to break up a project into multiple source files.

    my book (jeff cogswell) gives this sample:

    first file: main.cpp


    include <cstdlib>

    include <iostream>

    using namespace std;

    void BigDog(int KibblesCount);
    int main(int argc, char *argv[])
    {
    BigDog(3);
    system("PAUSE");
    return EXIT_SUCCESS;
    }


    second file: mystuff.cpp where a function is deposited:


    include <iostream>

    include <stdlib.h>

    include <string>

    void BigDog(int KibblesCount) {
    cout << "I am a lucky dog" << endl;
    cout << "I have " << KibblesCount << " pieces of food" << endl;
    }


    i guess the result should be a console with


    I'm a lucky dog
    I have 3 pieces of food


    printed on it. but when i compiled it the line "cout << "i'm a lucky dog"... is highlighted in red and i got this error:


    (mystuff) In function 'void BigDog(int)':
    (line 7, mystuff) 'cout undeclared (first use this function)
    (each undeclared identifier is reported only once for each function it appears in)
    (mystuff) 'endl' undeclared (first use this function)
    (makefile.win) [build error]n\make.exe ***[mystuff.o] error1


    so what's the problem? (i don't understand the comment)

     
    • codor kim

      codor kim - 2008-04-24

      hello thanks you two. your advice is correct. as soon as i added "using namespace std;" to mystuff.cpp the whole program worked.

       
      • Wayne Keen

        Wayne Keen - 2008-04-24

        Do you understand why it works? Programming is not about magic words, there is
        a reason for the commands. We want very much for you to know the why as well at
        the what.

        Wayne

         
    • Wayne Keen

      Wayne Keen - 2008-04-21

      Please post your compile log, as asked in the "Basic 3" section of the
      "Please Read Before Posting A Question" thread. (Take a moment to look
      around in there as well).

      It looks like you forgot to import the standard namespace into mystuff.cpp,
      but you might also be compiling C++ code as C code, which your log will tell us.

      Wayne

       
    • cpns

      cpns - 2008-04-21

      If you understood the reason for the "using namespace std ;" line you would understand why mystuff.cpp will not compile. Note that moving all of std:: to :: can cause problems and is not advised. I use it for speed in trivial test code, but in a significant code body it will cause problems with symbol clashes, and if you don't know what is happening, the resulting error messages can be quite bizarre. Move only what you need.

      Unrelated point: It would be more usual to put the prototype declaration of BigDog() into a header file. Then you would include it in both source files which allows the compiler to check that both declaration, definition, and usage are consistent. This is especially important in C++ which supports function overloading. I hope your book gets on to that!

      Clifford

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.