endless pulse

どっくんどっくんふるえる毎日を過ごしています

C++入門

本格的に勉強しよう!というわけじゃないけど、ほんとにほんとの少しだけC++を触ったらそっこー混乱したので(自分が)面白過ぎてメモ。(少し前のメモだけど、せっかくなので記載しますw と言ってもCで書けなくてC++で書けるような何かを書いた試しは未だにないのだけど…)

#include <iostream.h>
int main() {
  cout << "hoge" << endl;
  return 0;
}

コンパイルしたらいっぱい怒られたw

In file included from /usr/include/c++/4.2.1/backward/iostream.h:31,
                 from sample5-5-3.cc:2:
/usr/include/c++/4.2.1/backward/backward_warning.h:32:2: warning: #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 <iostream> instead of the deprecated header <iostream.h>. To disable this warning use -Wno-deprecated.

このページに思いっきりかいてあったんだけど、どうやらヘッダファイルの「.h」はいらない? そんなことも知らない\(^o^)/
「.h」だけ消してみたらまた怒られた。

sample5-5-3.cc: In function ‘int main()’:
sample5-5-3.cc:11: error: ‘cout’ was not declared in this scope
sample5-5-3.cc:11: error: ‘endl’ was not declared in this scope

これもさっき書いたページに書いてあったのだけど、「std::cout」ではなくて「cout」として使いたいなら、この名前空間の関数使いますよーという指定がいるらしい。ほうう…。。おかしいな、去年も入出力ぐらいする小さい小さいのなら書いたはずなのに…すっかり忘れすぎていて泣きたいw
というわけでできあがりました。

#include <iostream>
using namespace std;

int main() {
  cout << "hoge" << endl;
  return 0;
}

もーほんとにちょっとー。