C++でコンパイルするとwarningが出ます

このQ&Aのポイント
  • C++でコンパイルするとwarningが出る問題が発生しています。
  • warningメッセージには古いヘッダーを使用しているという警告が含まれています。
  • 解決策として、C++標準のセクション17.4.1.2で見つかる32のヘッダーを使用することが推奨されています。
回答を見る
  • ベストアンサー

C++でコンパイルするとwarningが出ます

C++初心者です。 C++でコンパイルすると以下のようなwarningメッセージが出るのですが、どういう意味なんでしょうか?解決策があれば教えてください。 プログラム(sample.cpp) #include <iostream.h> main(){ cout << " sample \n"; } warningメッセージ In file included from /usr/include/c++/3.3.1/backward/iostream.h:31, from sample.cpp:1: /usr/include/c++/3.3.1/backward/backward_warning.h:32:2: warning: #warning This file includes at least one deprecated or antiquated header. Please consider usin g one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the <X> header for the <X.h> header for C++ includes, or <s stream> instead of the deprecated header <strstream.h>. To disable this warning use -Wno-deprecated

質問者が選んだベストアンサー

  • ベストアンサー
  • rinkun
  • ベストアンサー率44% (706/1571)
回答No.1

#include <iostream.h> は時代遅れなので新しい形式を使うべきという警告ですね。 今なら #include <iostream> を使うべきでしょう。 ただ新しい形式では名前空間stdを使っているので std::cout << " sample \n"; のように名前空間を指定するか、使用前に using namespace std; を宣言することが必要になります。

okamonta
質問者

お礼

とてもわかりやすい説明ありがとうございました。 解決しました。

関連するQ&A

  • lunuxでコンパイル

    普段はリヌックスの環境を使っているのですが、ウィンドウズでしか動かないプログラムがあり、プログラムを実行するたびに、マシンを切り替えていて不便を感じています。 http://prefixspan-rel.sourceforge.jp/のC++のファイルをリヌックスでコンパイルしたいのですが、g++ prefixspan.cpp では In file included from /usr/include/c++/4.4/backward/strstream:47, from prefixspan.cpp:30: /usr/include/c++/4.4/backward/backward_warning.h:28:2: warning: #warning This file includes at least one deprecated or antiquated header which may be removed without further notice at a future date. Please use a non-deprecated interface with equivalent functionality instead. For a listing of replacement headers and interfaces, consult the file backward_warning.h. To disable this warning use -Wno-deprecated. prefixspan.cpp: In function ‘int main(int, char**)’: prefixspan.cpp:492: error: ‘atoi’ was not declared in this scope となります。C++に関しては詳しくないのですが、これは直せるものなのでしょうか?

  • iostreamについて

    今までOSはTurbo Linux 7を使っていましたが、最近RedHatLinux9にかえました。 それで、今までは普通に出来ていたことが出来なくなったので質問します。どちらかと言うとプログラム系の亊ですけど、Windowsではこんなことは起きないのでここで質問します。 C++でプログラムを作っているのですけど、 プログラム中で<iostream.h>,<fstream.h>をインクルードしていて、コンパイルすると /usr/include/c++/3.2.2/backward/fstream.h:31 から include されたファイル中, stereo_draw.cpp:4 から: /usr/include/c++/3.2.2/backward/backward_warning.h:32:2: 警告: #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the <X> header for the <X.h> header for C++ includes, or <sstream> instead of the deprecated header <strstream.h>. To disable this warning use -Wno-deprecated. と言うエラーメッセージが出るのです(stereo_draw.cppは私の作ったソースファイル)。 英語が得意ではないので訳があやしいですが、 『このファイルは推奨してない、または古いヘッダーをインクルードしています。C++スタンダードセクション17.4.1.2中の32ヘッダーのいづれかを使うことを推めます。例えば<X.h>の代わりにC++インクルード用の<X>ヘッダーをインクルードするとか、非推奨の<strstream.h>の代わりに<sstream>を使うなど』 と言っているのではないかと思います。でも、セクション17.4.1.2の32個のヘッダーなんて知らないです。それに、Turboの時はこんなメッセージは出ませんでした。 これを解決する方法をどなたか知っていたら教えて下さい。

  • backward_warning.hによる警告

    こんにちは。 独自で作成した、C:\Emacs_for_Windows\My_C++_Header_for_Bash.hというファイルパスを持つヘッダファイルをインクルードしているC++のソースファイルのtest.cppを、Cygwinのbash上で、g++コマンドを使い、 g++ test.cpp のようにビルドしたところ、以下のような警告が出ました。 ------------------------------------------------------------ bin/../lib/gcc/i686-pc-cygwin/4.3.4/include/c++/backward/strstream:51 から include されたファイル中, C:\Emacs_for_Windows\My_C++_Header_for_Bash.h:35 から, test.cpp:2 から: /bin/../lib/gcc/i686-pc-cygwin/4.3.4/include/c++/backward/backward_warning.h:33:2: 警告: #warning This file includes at least one deprecated or antiquated header which may be removed without further notice at a future date. Please use a non-deprecated interface with equivalent functionality instead. For a listing of replacement headers and interfaces, consult the file backward_warning.h. To disable this warning use -Wno-deprecated. ------------------------------------------------------------ Borland C++ Compiler 5.5 のbcc32コマンドでは、こういった警告は出ません。 /bin/../lib/gcc/i686-pc-cygwin/4.3.4/include/c++/backward/backward_warning.h:33 を参考に、backward_warning.hの33行目以降をみると、以下のようになっていました。 ------------------------------------------------------------ #ifdef __DEPRECATED 33 #warning \ 34 This file includes at least one deprecated or antiquated header which \ 35 may be removed without further notice at a future date. Please use a \ 36 non-deprecated interface with equivalent functionality instead. For a \ 37 listing of replacement headers and interfaces, consult the file \ 38 backward_warning.h. To disable this warning use -Wno-deprecated. ------------------------------------------------------------ どうやら「__DEPRECATED」が定義されているので、こういった警告が出ているようなのですが、 __DEPRECATEDの意味が分かりません。 警告の指示通り、-Wno-deprecatedを付けたままC++開発を続けて問題ないのでしょうか? ちなみに、C:\Emacs_for_Windows\My_C++_Header_for_Bash.hの内容は以下の通りです。 ------------------------------------------------------------ #ifndef MYHEADER_H #define MYHEADER_H //C言語のヘッダファイル #include <stdio.h> #include <stdlib.h> #include <ctype.h> #include <string.h> #include <time.h> #include <math.h> #include <stddef.h> //C++言語のヘッダの標準識別子 #include <iostream> #include <fstream> #include <cctype> #include <cstring> #include <cstdlib> #include <ctime> #include <cstdio> #include <new> #include <iomanip> #include <strstream> #include <typeinfo> //STLに関するヘッダ #include <string> #include <vector> #include <deque> #include <list> #include <set> #include <map> #include <algorithm> #include <functional> #include <utility> #include <numeric> #include <cassert> #include <iterator> //namespaceの解決 using namespace std; #endif ------------------------------------------------------------ 以上の件について何かご存知の方がいらっしゃれば、是非教えて頂きたいと思います。 では、よろしくお願い致します。

  • clock関数のコンパイルエラーで

    こんにちは。 C++プログラムの実行時間計測リスト #include<iostream.h> #include<time.h> int main(void){ clock_t start_time=clock(); int i; //ココから //計測したい処理を書く //ココまで cerr<<clock()-start_time<<"マイクロ秒"<<endl; return 0; } をRedHat9上でコンパイルしようとすると $gcc -o clock clock.cc /usr/include/c++/3.2.2/backward/iostream.h:31 から include されたファイル中, clock.cc:1 から: /usr/include/c++/3.2.2/backward/backward_warning.h:32:2: 警告: #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the <X> header for the <X.h> header for C++ includes, or <sstream> instead of the deprecated header <strstream.h>. To disable this warning use -Wno-deprecated. /tmp/ccoZe32a.o(.text+0x1c): In function `main': : undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)' /tmp/ccoZe32a.o(.text+0x3b): In function `main': : undefined reference to `std::cerr' /tmp/ccoZe32a.o(.text+0x40): In function `main': : undefined reference to `std::basic_ostream<char, std::char_traits<char> >::operator<<(long)' : collect2: ld はステータス 1 で終了しました となってしまいます。これはどうすればコンパイルできるようになるのでしょうか?

  • C++, strcpy の warning.

    VC2005 Express Edition を使ってます。 #include <iostream> #include <string> using namespace std; int main() {   char str[10] = "hello";   strcpy(str, "HELLO"); } ↑ こちらをコンパイルすると hello.cpp .\hello.cpp(8) : warning C4996: 'strcpy' was declared deprecated C:\Program Files\Microsoft Visual Studio 8\VC\include\string.h(73) : see declaration of 'strcpy' という警告が出るのはどうしてでしょうか? #include <string.h> も試したのですが同じ警告でした。

  • gccを使ってのリンク時のライブラリの指定法

    gccを使って、c++の勉強を始めようとしています。 hello.cpp(どこにでもある一行のプログラム)のコンパイルはできましたが、リンクができません。 私の機械では iostream は /usr/include/c++/3.3.2 にあります。( iostream.hは /usr/include/c++/3.3.2/backward にあります。) また、makefileにこの情報を書くには、どのように書けばよいのでしょうか。

  • コンパイルできません

    #include<iostream> using namespase std; int main(){ double feet; do{ cout<<"フィート数を入力する(0で終了):"; cin<<feet; cout<<feet*12<<"インチ\n"; }while(feet!=0.0); return 0; } をVisual Studio 2008 コマンドプロンプトでコンパイルしようとしたら、このようなメッセージが出てコンパイルできません。何故なのでしょうか?よろしくお願いします。 sample1-3-2.cpp C:\Program Files\Microsoft Visual Studio 9.0\VC\INCLUDE\xlocale(342) : warning C 4530: C++ 例外処理を使っていますが、アンワインド セマンティクスは有効にはなりま せん。/EHsc を指定してください。 sample1-3-2.cpp(2) : error C2146: 構文エラー : ';' が、識別子 'std' の前に必要で す。 sample1-3-2.cpp(2) : error C2873: 'namespase' : シンボルを using 宣言の中で使用 することはできません。 sample1-3-2.cpp(2) : error C4430: 型指定子がありません - int と仮定しました。メ モ: C++ は int を既定値としてサポートしていません sample1-3-2.cpp(2) : error C2365: 'std' : 再定義; 以前の定義は '名前空間' でした 。 sample1-3-2.cpp(7) : error C2065: 'cout' : 定義されていない識別子です。 sample1-3-2.cpp(8) : error C2065: 'cin' : 定義されていない識別子です。 sample1-3-2.cpp(9) : error C2065: 'cout' : 定義されていない識別子です。

  • octaveのコンパイルオプション

    MATLABのクローンのoctaveを, C++のライブラリとして利用したいと考えているのですが, コンパイルが上手くいきません. プログラムは以下のような単純なものです. --------- begin of hello.cpp ------------------ #include<iostream> #include<octave/config.h> #include<octave/Matrix.h> int main(){ Matrix m(2,2,1.0); cout<<"hello octave !"<<m; return 0; } --------- end of hello.cpp -------------------- 次のようなコンパイルオプションでコンパイルしています. > g++ -I/usr/include/octave-2.1.35/ -L/usr/lib/octave-2.1.35 \ -loctave -lcruft -loctinterp -lreadline -lcurses -llapack \ -lblas -ldl -lg2c -L/home/pippin/name/.redhat/SOURCES/octave-2.1.35/kpathsea \ -lkpathsea hello.cpp すると,次のようなエラーメッセージを返されます. /tmp/ccuvk2IV.o: In function `main': /tmp/ccuvk2IV.o(/text+0x2c): multiple definition of `main' /usr/lib/gcc-lib/i386-redhat-linux/2.96/libg2c.a(main.o)(.text+0x0): first definition here /usr/bin/ld: Warning: size of symbol `main' changed from 58 to 174 in /tmp/ccuvk2IV.o collect2: ld returned 1 exit status エラーメッセージから解するに,libg2c.aでもmain関数が定義されているため, hello.cppのmain関数とかぶるという様な事だと思うんですが,ライブラリに main関数が定義されているというのも変な話です. ちなみに,コンパイルした環境は, OS: redhat linux 7.3 octave version 2.1.35 gcc version 2.96 このタイプのエラーを回避する方法を知っておられる方は, 回答の方よろしくお願いします.

  • C言語のバグの警告文について

    こんばんわ。 C++でプログラムを作成中なのですが警告文の意味がわからないので教えていただきたいです。 警告文 SAMPLE.c: 181 : warning: passing argument 4 of fwrite' makes pointer from integer without a cast /usr/include/stdio.h:688: note: expected 'struct FILE * __restrict__' but argument is of type 'int' どうぞよろしくお願いいたします。

  • OpenGLがコンパイルできません。

    OpenGLをコンパイルしようとしてCygwinで 「gcc -o sample (プログラムの名前).c -I/usr/local/include -lglut32 -lglu32 -lopengl32」 と入力すると 「In file included from /usr/include/GL/glut.h:132, from (プログラムの名前).c:1: /usr/include/w32api/GL/glu.h:230: error ハクヒ。・ィ・鬘シ before '*' taken」 と表示されてコンパイルできません。「ハクヒ。・ィ・鬘シ」の部分は文字化けだと思います。このエラーメッセージから何が問題か分かる方がいらっしゃいましたら回答していただけると嬉しいです。CygwinはNetinstallからすべてのものをインストールしたので足りないものはないと思います。よろしくお願いします。

専門家に質問してみよう