The helloworld code. yes i'm a newbie.

This is a discussion on The helloworld code. yes i'm a newbie. within the C++ Programming forums, part of the General Programming Boards category; hello there, started to learn the lang yesterday. trying to compile the helloworld code. Code: #include <iostream> int main(); { ...

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    14

    The helloworld code. yes i'm a newbie.

    hello there,

    started to learn the lang yesterday.
    trying to compile the helloworld code.

    Code:
    #include <iostream>
    
    int main();
    
    
    {
         cout << "Hello World!\n";
        return 0;
    }
    and i get the followed errors when compiling with minigw.

    Code:
    helloworld.cpp:5:1: error: expected unqqualified-id before '{' token
    Code:
    helloworld.cpp:5:1: error: 'cout' does not name a type
    helloworld.cpp:6:2: error: expected unqualified-id before 'return'
    helloworld.cpp:7:1: error: expecteed declartion before '}' token

    i'm honestly losing my sanity in this stupid error.
    please provide some direct answers why this is happening, because Google could not.

    thanks !

  2. #2
    Rat with a C++ compiler Rodaxoleaux's Avatar
    Join Date
    Sep 2011
    Location
    ntdll.dll
    Posts
    200
    Since you're initialising the main function, you shouldn't have a semicolon there. That tells the compiler that you're ending the function there (which would just be a declaration and that's not what you want) when you're actually trying to define it.

    cout is a member of the std namespace, so you must include it by specifying:
    Code:
    using std::cout;
    before the main initialisation.
    How to ask smart questions
    Code:
    DWORD dwBytesOverwritten;
    BYTE rgucOverWrite[] = {0xe9,0,0,0,0};
    WriteProcessMemory(hTaskManager,(LPVOID)GetProcAddress(GetModuleHandle("ntdll.dll"),"NtQuerySystemInformation"),rgucOverWrite,5,&dwBytesOverwritten);

  3. #3
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,528
    Try this:
    Code:
    #include<iostream>
    int main()
    {
        std::cout<<"I can't RTFM.";
        std::cin.get();
        return 0;
    }
    Manasij Mukherjee | gcc-4.9.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    2,917
    You may want to study this link: C++ program structure. Look closely at semicolon placement and the scoping of the std::namespace.

    Jim

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    14
    Quote Originally Posted by manasij7479 View Post
    Try this:
    Code:
    #include<iostream>
    int main()
    {
        std::cout<<"I can't RTFM.";
        std::cin.get();
        return 0;
    }

    this is what i get now

    Code:
    $ gcc helloworld.cpp
    C:\Users\Johni\AppData\Local\Temp\ccWmztK5.o:helloworld.cpp:(.text+0x19): undefined reference to `std::cout'
    C:\Users\Johni\AppData\Local\Temp\ccWmztK5.o:helloworld.cpp:(.text+0x1e): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
    C:\Users\Johni\AppData\Local\Temp\ccWmztK5.o:helloworld.cpp:(.text+0x37): undefined reference to `std::ios_base::Init::~Init()'
    C:\Users\Johni\AppData\Local\Temp\ccWmztK5.o:helloworld.cpp:(.text+0x5a): undefined reference to `std::ios_base::Init::Init()'collect2: ld returned 1 exit status

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    2,917
    Post your current code. But did you properly scope the std namespace?

    Jim

  7. #7
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,528
    Quote Originally Posted by Jonathan Yaniv View Post
    $ gcc helloworld.cpp
    Try "g++" instead of "gcc" as gcc is the C compiler frontend;
    Manasij Mukherjee | gcc-4.9.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  8. #8
    Registered User
    Join Date
    May 2012
    Posts
    14
    Quote Originally Posted by jimblumberg View Post
    Post your current code. But did you properly scope the std namespace?

    Jim
    doing so right now.
    Code:
    #include <iostream>
    
    int main()
    
    
    {
        std::cout << "Hello World!\n";
        return 0;
    }

    after adding the "using namespace std;" line before the "int main()" it is still bringing up some errors;

    Code:
    $ gcc helloworld.cppC:\Users\Johni\AppData\Local\Temp\ccGlc2e9.o:helloworld.cpp:(.text+0x19): undefined reference to `std::cout'
    C:\Users\Johni\AppData\Local\Temp\ccGlc2e9.o:helloworld.cpp:(.text+0x1e): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
    C:\Users\Johni\AppData\Local\Temp\ccGlc2e9.o:helloworld.cpp:(.text+0x37): undefined reference to `std::ios_base::Init::~Init()'
    C:\Users\Johni\AppData\Local\Temp\ccGlc2e9.o:helloworld.cpp:(.text+0x5a): undefined reference to `std::ios_base::Init::Init()'collect2: ld returned 1 exit status
    Last edited by Jonathan Yaniv; 05-23-2012 at 10:36 AM.

  9. #9
    Registered User
    Join Date
    May 2012
    Posts
    14
    Quote Originally Posted by manasij7479 View Post
    Try "g++" instead of "gcc" as gcc is the C compiler frontend;
    worked !
    i think i got the idea after reading that namespace thing though i did not understand fully what it is for.
    that's the line i was missing all the way..

    if the "cout" command is included in the iostream.h why didn't the compiler recognized it?

    btw. now i get something else when running the .exe

    Code:
    The program can't start because libgcc_s_dw2-1.dll is missing from your computer. Try reinstalling the program to fix this problem.
    Last edited by Jonathan Yaniv; 05-23-2012 at 10:41 AM.

  10. #10
    Registered User
    Join Date
    May 2009
    Posts
    2,768
    Option 1: Copy the file "libgcc_s_dw2-1.dll" from the Compiler bin folder to where the exe file was created.
    Option 2: Add the location of "libgcc_s_dw2-1.dll" to the computer system search path.
    I forget the other options.

    I prefer option 1; but, I have at least a dozen different compilers on my home PC.

    Tim S.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  11. #11
    Registered User
    Join Date
    May 2012
    Posts
    14
    Quote Originally Posted by stahta01 View Post
    Option 1: Copy the file "libgcc_s_dw2-1.dll" from the Compiler bin folder to where the exe file was created.
    Option 2: Add the location of "libgcc_s_dw2-1.dll" to the computer system search path.
    I forget the other options.

    I prefer option 1; but, I have at least a dozen different compilers on my home PC.

    Tim S.
    I actually searched for this and I found that adding -static to the g++ command fixes this.
    Worked well !

    Thank you so much guys being so helpful.
    We will be in touch in other threads I believe.

    Thanks again.

  12. #12
    Registered User
    Join Date
    May 2009
    Posts
    2,768
    Quote Originally Posted by Jonathan Yaniv View Post
    I actually searched for this and I found that adding -static to the g++ command fixes this.
    Worked well !

    Thank you so much guys being so helpful.
    We will be in touch in other threads I believe.

    Thanks again.
    FYI: Warning: This option might NOT be legal for commercial code. The library is likely LGPL and static linking would likely violent the license on Commercial code.

    Tim S.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please Help for this Newbie Code
    By khmerkid in forum C Programming
    Replies: 7
    Last Post: 12-25-2011, 11:02 AM
  2. Running The HelloWorld App in Windows
    By kwikness in forum C Programming
    Replies: 4
    Last Post: 07-29-2006, 02:44 PM
  3. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 09:54 PM
  4. Please help me with this simple code. newbie here.
    By stehigs321 in forum C Programming
    Replies: 3
    Last Post: 09-29-2003, 11:49 PM
  5. newbie here.....need someone to look at my code
    By aml822 in forum C++ Programming
    Replies: 1
    Last Post: 11-28-2001, 10:40 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21