@Kitsuji

Are you sure you want to delete the question?

Leaving a resolved question undeleted may help others!

c++でエラーがでる。error: use of undeclared identifier 'cin'

Q&A

Closed

解決したいこと

macosでc++を始めました。基本的なコードを書いたらエラーが出てしまいました。
解決方法を教えてください。

発生している問題・エラー

Main.cpp:6:5: error: use of undeclared identifier 'cin'
    cin>>n;
    ^
Main.cpp:7:5: error: use of undeclared identifier 'cout'; did you mean 'count'?
    cout<<n<<endl;
    ^~~~
    count
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/__algorithm/count.h:25:5: note: 'count' declared here
    count(_InputIterator __first, _InputIterator __last, const _Tp& __value) {
    ^
Main.cpp:7:5: error: reference to overloaded function could not be resolved; did you mean to call it?
    cout<<n<<endl;
    ^~~~
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/__algorithm/count.h:25:5: note: possible target for call
    count(_InputIterator __first, _InputIterator __last, const _Tp& __value) {
    ^
Main.cpp:7:14: error: use of undeclared identifier 'endl'
    cout<<n<<endl;
             ^
4 errors generated.

該当するソースコード

#include <bits/stdc++.h>
using namespace std;

int main(){
    int n;
    cin>>n;
    cout<<n<<endl;
}

補足

https://zenn.dev/minguu42/articles/20220722-how-to-use-gcc-on-mac
上記のブログの言うとおりにしたら、エラーコードが出なくなりました。

0 likes

4Answer

paiza.ioで実行したところエラーは発生しませんでした。
コンパイラは何をお使いですか?

0Like

Comments

  1. @Kitsuji

    Questioner

    gccをインストールしました。

こちらはコンパイル出来ますか?

#include <iostream>

int main (){
    std::cout << "HelloWorld" << std::endl;
    return 0;
}

同じようなエラーが、、、違ったらごめんなさい。

0Like

Comments

  1. @Kitsuji

    Questioner

    コンパイルできました。
    ブログも見させてもらいました。でも、内容にある、ステップ2の 【「『bits』という名前のフォルダが存在し、さらにその中にstdc++.hというファイルが存在するもの」を探す】 のところで、「bits」というフォルダを見つけられませんでした。出力されたパスをopenコマンドを使ってfinderで開いて一つずつ探しました。なにかインストールできていないのでしょうか。

  2. コンパイル出来てよかったです。

    もしも見つけられない場合、自分で.h作って環境変数で指定するという方法もあるようです。

macOSにもg++はあるのでそれ使えばいいのですが,初期状態でGCC版でないものが入ってたりしてちょっと勝手が違うので,VSCode公式のガイドを置いておきます.

0Like

This answer has been deleted for violation of our Terms of Service.

Your answer might help someone💌