Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Can't compile simple C++ prog?

  1. #1
    Join Date
    May 2005
    Beans
    6

    Question Can't compile simple C++ prog?

    greetings,

    im trying to learn c++ and was working through a tutorial and it wanted me to make a simple "hello world" program. when i went to compile it gave errors.

    Code:
    #include <iostream>
    
    int main()
    {
      cout<<"HEY, you, I'm alive!  Oh, and Hello World!";
      return 0;    
    }
    and i got these errors:
    Code:
    root@error:/home/mark/Programming# g++ helloworld.cpp
    helloworld.cpp: In function ‘int main()’:
    helloworld.cpp:5: error: ‘cout’ was not declared in this scope
    Code:
    root@error:/home/mark/Programming# gcc helloworld.cpp
    helloworld.cpp: In function ‘int main()’:
    helloworld.cpp:5: error: ‘cout’ was not declared in this scope
    Code:
    root@error:/home/mark/Programming# bcc helloworld.cpp
    ld86: cannot open input file crt0.o
    i have build-essential installed, also have bcc installed. i cant seem to find any info on such an error, the tutorials didnt speak of it either. what now? i know i cant continue learning c++ if i cant even compile a simple hello world prog.

  2. #2
    skymt is offline May the Ubuntu Be With You!
    Join Date
    Feb 2006
    Beans
    Hidden!
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Can't compile simple C++ prog?

    You must be using an old book. It's "std::cout" now. You'll understand once you get to namespaces.
    Are you Listening?
    Are you a Hero?
    Are you Sleeping?

  3. #3
    Join Date
    Sep 2006
    Location
    Fort Lauderdale, FL
    Beans
    47
    Distro
    Ubuntu 6.10 Edgy

    Re: Can't compile simple C++ prog?

    alternately, you could just add
    Code:
    using std;
    after the #include.

  4. #4
    Join Date
    Sep 2006
    Beans
    Hidden!

    Re: Can't compile simple C++ prog?

    Code:
    #include <iostream>
    using namespace std;
    
    int main() {
      cout << "Hello world!" << endl;
    }
    Or, alternatively:
    Code:
    #include <iostream>
    using std::cout;
    using std::endl;
    
    int main() {
      cout << "Hello world!" << endl;
    }
    If you're just starting out with C++, I recommend Bruce Eckel's Thinking in C++ series of books. He has generously made them available for free download here.

  5. #5
    Join Date
    Oct 2006
    Beans
    1

    Re: Can't compile simple C++ prog?

    did you use gcc to compile the program. Use g++ to compile and it will work

  6. #6
    Join Date
    Oct 2006
    Location
    Atlanta, Georgia
    Beans
    1,869

    Re: Can't compile simple C++ prog?

    Quote Originally Posted by vinay_lakhanpal View Post
    did you use gcc to compile the program. Use g++ to compile and it will work
    Nah, he just forgot to 'add' the std libary

  7. #7
    Join Date
    Nov 2006
    Beans
    5

    Re: Can't compile simple C++ prog?

    i'll start asking sorry for this but i still don't understand my problem.. im running under ubuntu dapper 6.06.1 and when i try to compile a simple c++ program i get this errors:

    gcc: prova: Nessun file o directory
    prova.c: In function ‘main’:
    prova.c:8: error: stray ‘\226’ in program
    prova.c:8: error: stray ‘\128’ in program
    prova.c:8: error: stray ‘\156’ in program
    prova.c:8: error: ‘Un’ undeclared (first use in this function)
    prova.c:8: error: (Each undeclared identifier is reported only once
    prova.c:8: error: for each function it appears in.)
    prova.c:8: error: syntax error before ‘solo’
    prova.c:8: error: stray ‘\’ in program
    prova.c:8: error: stray ‘\226’ in program
    prova.c:8: error: stray ‘\128’ in program
    prova.c:8: error: stray ‘\157’ in program
    prova.c:11: error: stray ‘\226’ in program
    prova.c:11: error: stray ‘\128’ in program
    prova.c:11: error: stray ‘\156’ in program
    prova.c:11: error: ‘Sono’ undeclared (first use in this function)
    prova.c:11: error: syntax error before ‘il’
    prova.c:11: error: stray ‘\’ in program
    prova.c:11: error: stray ‘\226’ in program
    prova.c:11: error: stray ‘\128’ in program
    prova.c:11: error: stray ‘\157’ in program
    prova.c:13: error: stray ‘\226’ in program
    prova.c:13: error: stray ‘\128’ in program
    prova.c:13: error: stray ‘\156’ in program
    prova.c:13: error: stray ‘\226’ in program
    prova.c:13: error: stray ‘\128’ in program
    prova.c:13: error: stray ‘\157’ in program
    prova.c:15: error: stray ‘\226’ in program
    prova.c:15: error: stray ‘\128’ in program
    prova.c:15: error: stray ‘\156’ in program
    prova.c:15: error: stray ‘\’ in program
    prova.c:15: error: stray ‘\226’ in program
    prova.c:15: error: stray ‘\128’ in program
    prova.c:15: error: stray ‘\157’ in program
    yes unfortunately its italian but u should still understand well the problem.. the code in prova.c is:

    #include <stdio.h>
    #include <stdlib.h>


    main()
    {
    int pid;
    printf(“Un solo processo finora\n”);
    pid = fork();
    if (pid==0)
    printf(“Sono il figlio\n”);
    else if (pid > 0)
    printf(“Sono il padre, pid figlio=%d”,pid);
    else
    printf(“Situazione di errore \n”);
    }
    and yes.. i have installed gcc and the build-essential package..

    please help!!

    thanks a lot

  8. #8
    Join Date
    Aug 2006
    Beans
    71

    Re: Can't compile simple C++ prog?

    What are you using to write the file?
    You should be using a text-editor like kate,vi,gedit etc. not a
    program like OpenOffice.

  9. #9
    Join Date
    Nov 2006
    Beans
    5

    Re: Can't compile simple C++ prog?

    i use vi for writing the program.. sometimes i also edit it with gedit..

    never used openoffice.. u suggest to rewrite it again with gedit?

    any help about how to fix this.. i really need to get it to work for the school exams

  10. #10
    Join Date
    Sep 2006
    Beans
    Hidden!

    Re: Can't compile simple C++ prog?

    mstation,

    It looks like whatever program you are using to write the program is adding weird characters, “ should be ". It might have something to do with your locale or input method.

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •