LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Cannot say hello to the world due to 'code 1' (https://www.linuxquestions.org/questions/linux-newbie-8/cannot-say-hello-to-the-world-due-to-code-1-a-4175531460/)

lckwswws 01-18-2015 08:17 PM

Cannot say hello to the world due to 'code 1'
 
/************************************************************
*
* Project 0: My First Program in C++
*
* Author: xxx xxxx
* Date: 18 January 2015
*
* This is the canonical first program for C++.
* Its purpose is to show that one knows how to create a program in
* one's particular programming environment.
*
************************************************************/

#include <bjarne/std_lib_facilities.h>

int main()
{
cout << "Hello, world!\n";

return 0;
}

I compiled the hello world program as my prof instructed.
then I used C+c C+c, delete 'make -k' and replaced with 'g++ -o proj0 -std=c++11 proj0.cc', choose to save the file, and it goes 'compilation exited abnormally with code 1'

note that I do all these by making SSH connection with computer in lab with a linux system :)

FYI, the whole thing is:

-*- mode: compilation; default-directory: "~/private/cs1/proj0/" -*-
Compilation started at Sun Jan 18 21:15:54

g++ -o proj0 -std=c++11 proj0.cc
In file included from /usr/include/c++/4.9.2/locale:41:0,
from /usr/include/c++/4.9.2/iomanip:43,
from /usr/local/include/bjarne/std_lib_facilities.h:220,
from proj0.cc:14:
/usr/include/c++/4.9.2/bits/locale_facets_nonio.h:1869:5: error: template-id do_get<> for String std::messages<char>::do_get(std::messages_base::catalog, int, int, const String&) const does not match any template declaration
messages<char>::do_get(catalog, int, int, const string&) const;
^
/usr/include/c++/4.9.2/bits/locale_facets_nonio.h:1869:62: note: saw 1 template<> , need 2 for specializing a member function template
messages<char>::do_get(catalog, int, int, const string&) const;
^

Compilation exited abnormally with code 1 at Sun Jan 18 21:15:55

lemon09 01-19-2015 02:57 AM

I don't exactly understand if you are a beginner in C++ why do you have to code like that?
Most importantly .h type headers are not encouraged in C++ especially when you are using 2011 standard.

It might be so that the iostream is not included. Please try the following code:
Code:

/************************************************************
*
* Project 0: My First Program in C++
*
* Author: xxx xxxx
* Date: 18 January 2015
*
* This is the canonical first program for C++.
* Its purpose is to show that one knows how to create a program in
* one's particular programming environment.
*
************************************************************/

#include <bjarne/std_lib_facilities.h>
#include <iostream.h>

int main()
{
    cout << "Hello, world!\n";
    return 0;
}

Please use standard techniques.

rtmistler 01-19-2015 12:45 PM

Well, this works for me:
Code:

#include <iostream>

int main()
{
    std::cout << "Hello, world!\n";
    return 0;
}

I tried "<iostream.h>" and found that it rejected the .h extension.

For some odd reason it required me to add the "std::" before the cout call which led me to re-check that cout is part of iostream. It is. I then further commented out the include file and it didn't compile.

I've done C++ sparingly and do understand that it sometimes requires the declaration of the top level group, or section to use a library function, but if the include directives are properly used, then you don't need to do that.

But to me the points to consider are the following:
  1. First get the fundamental working, you don't need std_lib_facilities, be that your file or your instructor's file, or a general course include file; although it should not be a problem either. But to eliminate that, don't use it at first and then figure out if it happens to be a problem. AND just in counting lines from your post, it APPEARS that line #14 IS the std_lib_facilities include statement, so ...
  2. I have g++ version 4.6.3, I don't know what you have, but mine did not recognize the -std= argument whether I used a single dash or double dash, it just does not recognize that option in the form you have there. My compilation line was "g++ -o proj proj.cc" and that worked fine.
  3. If you can get it working, you should print out what you got working, you've obviously put in some effort to understand this, and then show it to the instructor and explain that you apparently typed it all in as per template or example and it didn't work; however you did get it to work by doing ... and show them the result where it did work.
  4. Because this appears to be introductory C++ and right at the start, they're basically supposed to give you examples that if you type them in correct and follow their instructions to the letter, they'll work.
  5. My points are that you're not far off here, whatever that include file is for std_lib_facilities, I'm betting you were instructed to put that line there, but it appears that it is part of the problem right now.

lemon09 01-19-2015 01:23 PM

Quote:

I tried "<iostream.h>" and found that it rejected the .h extension.

For some odd reason it required me to add the "std::" before the cout call which led me to re-check that cout is part of iostream. It is. I then further commented out the include file and it didn't compile.
'.h' type of files, as I mentioned earlier are not preferable.
cout is in std namespace and instead of the syntax that you provided above one can also add the follwing lines:
Code:

using namespace std;
-std is a valid option for passing the standard. You may check out the man page.

Finally I would agree with the fact that the program shown could have been coded in a better way. You really need to use std_lib for this.


All times are GMT -5. The time now is 10:02 PM.