Code Fix

初級error: ISO C++ forbids comparison between pointer and integer の原因と直し方 › パターン1

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

int型の変数をダブルクォートの文字列リテラル(例: "20")と== で比較しようとしたときに出るエラーです。

 1  #include <iostream>
 2  int main() {
 3      int age = 20;
 4      if (age == ____) {
                     ^
 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]

空欄を埋めてコンパイル・実行を通す

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

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

文字列リテラルはconst char*型であり、int型とは根本的に比較できません。

このエラーの解説と類題をまとめて見る →

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