Thread: Can't Compile a Simple Program

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    5

    Cool Can't Compile a Simple Program

    I am new to C++ and am tryind to compile a simple program under Linux. The code is in the form of:

    #include <stdio.h>
    int main()
    {
    cout << "Hell World";
    return 0;
    }



    When trying to compile, I get the error messages as follows:

    test.cpp: In function 'int main()':
    test.cpp: 'cout' undeclared (first use this function)
    text.cpp: (Each undeclared identifier is reported only once for each function it appears in.)



    What have I done incorrectly ?

    Pete

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Try this instead
    Code:
    #include <iostream>
    using namespace std;
     
    int main() 
    { 
      cout << "Hell World"; 
      return 0; 
    }
    Or if you have a cheap compiler that doesn't like namespaces, replace the first two lines with #include <iostream.h>

    stdio.h contains the i/o functions for C, C++ uses iostream instead.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    5
    G'Day Prelube,

    So what does 'namespace' actually do ??

    Pete

  4. #4
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    If you are just begining, it is too complicated to explain to you. But I'll try anyways.

    If you want to use different things with the same name, you can put them in different namespaces to avoid the conflict. That whole using line tells the compiler that u r using the std (standard) namespace.

  5. #5
    Señor Member
    Join Date
    Jan 2002
    Posts
    560
    I'd like to know what namespaces are too. I've noticed you don't put in the .h in the include files. Go ahead, use technical terms.

  6. #6
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    Hell World
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  7. #7
    Registered User
    Join Date
    Feb 2002
    Posts
    5

    Smile

    G'Day SilentStrike,

    Yea...... sometimes I wonder :-)

    But is really ment to be "Hello"

    Pete

  8. #8
    Registered User
    Join Date
    Feb 2002
    Posts
    5

    Question

    I managed to get it going but instead of using:

    cout << "Hello World";

    I had to use:

    puts("Hello World");

    before it would compile - why is this so ?

    Pete

  9. #9
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    Did you try Prelude's code? If that didn't work, perhaps you have only a C compiler rather than a C++. Tell us a bit more about your environment.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  10. #10
    Registered User
    Join Date
    Feb 2002
    Posts
    5
    G'Day Adrian,

    Yea, you could be right there - yes I did try the code but kept on getting the same results until I used 'puts'.

    I am using Linux (Mandrake 8.1) and the compiler I'm using is "gcc".

    Pete

  11. #11
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    I don't use either I'm afraid, but have a browse at the link below. It says it will compile C++ amongst other things. You might need to set some option or other to make it compile C++.

    http://gcc.gnu.org/

    Does your source file have a .c or a .cpp extension? If .c, you might try changing it to .cpp.

    *** EDIT ***

    Try using g++ rather than gcc!!!

    http://www.cprogramming.com/cboard/s...threadid=10470
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  12. #12
    Unregistered
    Guest
    let's say you have a project that has two different user defined header files. In each header file is a fucntion called loadData(). Both copies of loadData() have the same return type, and the same parameters, but the function bodies differ. When you call loadData() in main() how is the compiler going to know which version you intend to use, as the prototypes are the same, even if the body of the functions aren't?. Well, one way might to make the two prototypes of loadData() different somehow. Or you could take loadData() out of the two headers, modify it to cover both function bodies, and place it in a third header file, or make it global,or whatever. Last, but not least, would be to create a new level of organization, called namespace, such that when you want to use the version from namespace A you so indicate and when you want to use the version from namespace B you so indicate. I don't know all the details of declaring namespaces, and I can't find it readily in my online reference, but I think you use them something like this:

    namespace std;//all the routine I/O handling is in here
    namespace A;//user created namespace, holds headers, etc.
    namespace B;//user created namespace, holds headers, etc.

    std::cout << "What's up doc?" << std::endl;
    A::loadData();
    B::loadData();

    I think that gives you the idea, but the details we both need to look up.

  13. #13
    Registered User
    Join Date
    Feb 2002
    Posts
    32

    Simple Program

    i am new to C++ to but i would try somthin like\

    #include <iostream.h>
    void main( void )
    {

    cout << "Hell World ";
    return 0;
    }

  14. #14
    Unregistered
    Guest

    ...

    The reason the original code didnt work is because he was useing cout with stdio.h you need to use iosream.h for cout and stdio.h for printf and scanf and such

  15. #15
    Unregistered
    Guest
    yeah... I'm new too... am I able to do the following:

    #include <stdio.h>
    #include <iostream.h>
    int main()
    {
    cout << "Hell World";
    return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with simple, simple program.
    By LightsOut06 in forum C Programming
    Replies: 5
    Last Post: 09-01-2005, 08:31 PM
  2. Problem with simple XOR program
    By spike_ in forum C++ Programming
    Replies: 8
    Last Post: 08-17-2005, 12:09 AM
  3. simple frontend program problem
    By gandalf_bar in forum Linux Programming
    Replies: 16
    Last Post: 04-22-2004, 06:33 AM
  4. How To Embed Data In Program While Compile?
    By ooosawaddee3 in forum C++ Programming
    Replies: 1
    Last Post: 09-09-2002, 10:14 AM
  5. how do i compile a program that deals w/classes?
    By Shy_girl_311 in forum C++ Programming
    Replies: 5
    Last Post: 11-11-2001, 02:32 AM