のんびりしているエンジニアの日記

ソフトウェアなどのエンジニア的な何かを書きます。

もうこわくないエラーに引っかかった時の対処法のすすめ

Sponsored Links

皆さんこんにちは
お元気ですか。私は腹痛です。

エラーはコーディングしている時、環境設定している時、様々ありますね。
さすがにライブラリとかまで使っていると、もう見てられないぐらいコードやエラーの可能性が膨れ上がっています。

そこで、私の解決方法をご紹介したいと思います。

エラーを読み、理解しましょう。

当然ですが、エラーを読みましょう。
最近のコンパイラは優しいので自分でコーディングした時のミスは
優秀なコンパイラがエラーを出してくれます。

例えば…以下の様な関数があるとします。

#include <iostream>
#include <string>

using namespace std;

int add(int a,int b){
	return a + b;
}

int main(int argc, char const *argv[]){
	string aa = 0232;
	add("aaa","sbbb");
	return 0;
}

これをコンパイルすると…

error.cpp:11:9: error: no viable conversion from 'int' to 'string' (aka 'basic_string<char, char_traits<char>, allocator<char> >')
        string aa = 0232;
               ^    ~~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/string:1191:5: note: candidate constructor not viable: no known conversion from
      'int' to 'const std::__1::basic_string<char> &' for 1st argument
    basic_string(const basic_string& __str);
    ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/string:1200:31: note: candidate constructor not viable: no known conversion from
      'int' to 'const value_type *' (aka 'const char *') for 1st argument
    _LIBCPP_INLINE_VISIBILITY basic_string(const value_type* __s);
                              ^
error.cpp:12:2: error: no matching function for call to 'add'
        add("aaa","sbbb");
        ^~~
error.cpp:6:5: note: candidate function not viable: no known conversion from 'const char [4]' to 'int' for 1st argument
int add(int a,int b){

長いのですが、エラー箇所まで教えてくれます。

最初のエラーですが、no viable conversion from 'int' to 'string' 、
intからstringに変換できません。と言っています。文字列突っ込んでるからねぇ…そりゃいけません。直しましょう。

error.cpp:12:2: error: no matching function for call to 'add'
要は呼び出す関数がありませんといっております。addは作ったけど、どうして!?。とよくみるとaddの引数はintです。これでは、使うことができません。

#include <iostream>
#include <string>

using namespace std;

int add(int a,int b){
	return a + b;
}

int main(int argc, char const *argv[]){
	string aa = "aaaaa";
	add(1,2);
	return 0;
}

直すとこんな形になります。エラーコードは重要です。どんな言語でもぶっちゃけエラーコードが読めればいけるものです。しっかり読み、理解できるようにしましょう。

しかし、人間がエラーコードを書いているので、理解不能なエラーメッセージが出ることも多々あります。
そんなときには次の対処法を実践してみましょう。

2.Google先生で検索
ライブラリを利用していると意味不明なことがよく起こります。
そんな時はまず、Google先生で検索をかけましょう。きっと
さて、ここで検索を行うのですが、色々な検索ワードを用意しておきます。

①エラーそのものを検索キーワードにする
②エラーの一部をキーワードにする
③それでも無理な場合は適当な言葉をキーワードにする

例を使って説明します。
error.cpp:12:2: error: no matching function for call to 'add'

まずは、①
error: no matching function for call to 'add'
を調べてみましょう。
これで、検索を行うと英語のサイトが検索されるはずです。StackOverflowが引っかかることが多いのですが、5割はこれで解決できるはずです。

次に②
error: no matching function for call to 'add'

addはおそらく普遍的なワード(置き換え可能なワード)なので、
no matching function for call to

で検索をかけてみましょう。
案外、引っかかることがあります。

③異なるワードで調べてみる
error.cpp:12:2: error: no matching function for call to 'add'

まず、皆さんの知りたいことはなんでしょうか?
このよくわからない(と思われる)文章の意味であることが多いと思います。
「error: no matching function for call to 意味」や実際に動いていないので「error: no matching function for call to 動かない」
また、google の関連キーワードを見てみましょう

どれも引っかからなかった場合・・・・?
諦めてドキュメントを調べます。ただ、殆どの場合はこの方法で調べることができるはずです。

3.本家本元を調べる
ライブラリ自体を調べたりします。これはもう最終手段です。どうしても無理な時にご利用ください。
本家を調べるのは大変なのです…。