Code Fix

初級

error: ISO C++ forbids comparison between pointer and integer の原因と直し方

int型の変数をダブルクォートの文字列リテラル(例: "20")と== で比較しようとしたときに出るエラーです。文字列リテラルはconst char*型であり、int型とは根本的に比較できません。

エラーメッセージの読み方

main.cpp:4:13: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
main.cpp
ファイル名
4
行番号
13
列番号 — この位置でg++が構文解析に失敗しました
ISO C++ forbids comparison between pointer and integer [-fpermissive]
内容 — 期待していたもの、または受け付けられなかったもの

このエラーが出る典型パターン

パターン1

 1  #include <iostream>
 2  int main() {
 3      int age = 20;
 4      if (age == "20") {
                     ^
 5          std::cout << "match" << std::endl;
 6      } else {
 7          std::cout << "no match" << std::endl;
 8      }
 9      return 0;
10  }
main.cpp:4:13: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]

"20"はconst char*型(文字列へのポインタ)であり、int型のageとは比べられません。数値と比較するには数値リテラルの20を使います。

直し方: "20"20 にします。

この問題を解いてみる →

広告
広告スロット(未設定)

パターン2

 1  #include <iostream>
 2  int main() {
 3      int code = 7;
 4      if (code == "7") {
                     ^
 5          std::cout << "ok" << std::endl;
 6      }
 7      return 0;
 8  }
main.cpp:4:14: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]

見た目が同じ「7」でも、ダブルクォートで囲むと文字列(ポインタ)になり、int型とは比較できない別の型になります。

直し方: "7"7 にします。

この問題を解いてみる →

パターン3

 1  #include <iostream>
 2  int main() {
 3      int level = 1;
 4      if (level == "1") {
                      ^
 5          std::cout << "beginner" << std::endl;
 6      }
 7      return 0;
 8  }
main.cpp:4:15: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]

Pythonなどでは単にfalseになる比較でも、C++は型として比較できないためコンパイルの段階で止まります。

直し方: "1"1 にします。

この問題を解いてみる →

よくある誤解

Pythonなど一部の言語では数値と文字列を比較すると単に false になりますが、C++はそもそも型として比較できないためコンパイルの段階で止まります。「実行すれば一致しないだけ」ではなく、実行にすら至りません。

まとめ

error: ISO C++ forbids comparison between pointer and integerは初級でつまずきやすい項目です。上の3パターンを実際に手で直すと、エラーメッセージのどこを読めばよいかが掴めます。

演習をはじめる

関連するエラー

広告
広告スロット(未設定)