TopC++ 入門 › エラー辞典 › コンパイルエラー

C++ コンパイルエラー辞典

C++ のコンパイラが吐くエラーは長くて難解。よく見る 24 パターンを「典型メッセージ → 原因 → 修正例」で並べました。キーワード検索・カテゴリ絞り込みでピンポイントに探せます。

すべて 構文(syn) 意味論(sem) リンク(link) テンプレ(tpl)
synE01. expected ';' before ...

典型メッセージ

error: expected ';' after expression

原因

C と同じく文末セミコロン忘れ。特にクラス定義の末尾、struct {...} の後ろ。

ng.cppNG
struct P { int x, y } // ← ここに ; int main() { }
ok.cppOK
struct P { int x, y; }; int main() { }
synE02. expected unqualified-id before '{'

原因

関数の戻り値型を書き忘れた、または前のクラス定義で ; が抜けている。

ng.cppNG
main() { } // 戻り値型が無い
ok.cppOK
int main() { }
semE03. 'cout' was not declared in this scope

原因

<iostream> の include 忘れ、または std:: を書き忘れ。

ng.cppNG
int main() { cout << "hi"; // include + std 不足 }
ok.cppOK
#include <iostream> int main() { std::cout << "hi"; }
semE04. invalid conversion from 'int' to 'T*'

原因

C では暗黙キャストされていた型変換が C++ では拒否される。void*T* なら static_cast で明示。

ng.cppNG
int* p = malloc(4); // void* → int* NG
ok.cppOK
int* p = static_cast<int*>(malloc(4)); // もっと良い: auto p = new int;
semE05. no matching function for call to 'f(...)'

原因

引数の型・個数が合うオーバーロードが無い。const 有無やリテラル型(int vs long long)も見られる。

ng.cppNG
void f(std::string& s); f("x"); // const char* は string& に暗黙変換不可
ok.cppOK
void f(const std::string& s); // const& にする // または std::string s("x"); f(s);
semE06. call of overloaded 'f(...)' is ambiguous

原因

複数のオーバーロードが同程度にマッチ。明示キャストで決める。

ng.cppNG
void f(int); void f(double); f(0L); // int にも double にも同等に変換可能
ok.cppOK
f(static_cast<int>(0L));
semE07. passing 'const T' as 'this' argument discards qualifiers

原因

const オブジェクトから非 const メンバ関数を呼ぼうとした。メンバ関数を const にする。

ng.cppNG
class P{ public: int len() { return 0; } }; void show(const P& p) { p.len(); }
ok.cppOK
class P{ public: int len() const { return 0; } };
semE08. cannot bind non-const lvalue reference to rvalue

原因

一時オブジェクト(rvalue)を T& では受けられない。const T& にするか T&& にする。

ng.cppNG
void f(std::string& s); f(std::string("tmp")); // 一時値
ok.cppOK
void f(const std::string& s);
semE09. use of deleted function 'T::T(const T&)'

原因

コピー禁止クラス(unique_ptr, thread, mutex 等)をコピーしようとした。std::move が必要。

ng.cppNG
auto a = std::make_unique<int>(1); auto b = a; // copy 禁止
ok.cppOK
auto b = std::move(a); // move でOK
semE10. non-static data member initializer not allowed (C++11 未満)

原因

-std=c++11 以降が必要。コンパイルフラグに -std=c++17 を追加。

cliNG
$ g++ a.cpp # C++98 互換になる
cliOK
$ g++ -std=c++17 -Wall a.cpp
semE11. narrowing conversion from 'double' to 'int'

原因

brace init {} は縮小変換を禁止する。static_cast で意図を明示。

ng.cppNG
int x{3.14}; // {} は縮小禁止
ok.cppOK
int x = static_cast<int>(3.14);
semE12. no viable overloaded 'operator<<'

原因

自作クラスの operator<< がまだ定義されていない、または friend 宣言の位置が間違っている。

ok.cppOK
struct P{ int x,y; }; std::ostream& operator<<(std::ostream& os, const P& p){ return os << "(" << p.x << "," << p.y << ")"; }
linkE13. undefined reference to '...'

原因

リンカが関数本体を見つけられない。cpp ファイルをリンク対象に入れ忘れか、ライブラリ -l... の忘れ。

cliNG
$ g++ main.cpp # foo.cpp を渡してない
cliOK
$ g++ main.cpp foo.cpp $ g++ main.cpp -lpthread # 外部 lib
linkE14. multiple definition of '...'

原因

ヘッダに関数の本体を書き、複数 cpp から include された。inline を付けるか cpp に分離。

ng .hNG
int add(int a, int b) { return a+b; }
ok .hOK
inline int add(int a, int b) { return a+b; } // または宣言だけ置き、cpp に本体を書く
linkE15. undefined reference to 'vtable for ...'

原因

virtual 関数のうちヘッダ宣言だけで本体の定義を書き忘れ。最初に非インラインな virtual を 1 つ持たせるのが慣例(key function)。

ok.cppOK
// 純粋仮想でなく宣言したなら本体も必要 virtual void speak(); // header void Animal::speak(){ } // cpp
tplE16. template argument deduction failed

原因

テンプレート引数を推論できない。テンプレート引数を明示する。

ng.cppNG
template<class T> T zero() { return T{}; } auto x = zero(); // T が分からない
ok.cppOK
auto x = zero<int>();
tplE17. static_assert failed: 'type requirement not met'

原因

テンプレートが型に対して要件を課していて、渡した型が満たさない。エラーメッセージに要件名が含まれるはず。

tplE18. need 'typename' before dependent name

原因

テンプレート内で依存名を型として使う時は typename が必要。

ng.cppNG
template<class T> void f() { T::iterator it; } // 型?値?不明
ok.cppOK
template<class T> void f() { typename T::iterator it; }
semE19. no member named 'x' in 'T'

原因

typo、または前方宣言だけで完全型になっていない(ヘッダ include 漏れ)。

semE20. conversion from 'int' to 'T' is ambiguous / explicit

原因

explicit コンストラクタは暗黙変換を許さない。括弧で直接構築するか static_cast

ok.cppOK
explicit Len(int); Len a = 5; // NG (explicit) Len b(5); // OK Len c{5}; // OK
semE21. cannot convert 'X' to 'Y' in initialization

原因

型の不一致。特に「nullptr を整数に」「intbool に暗黙変換」などが警告 or エラー。

semE22. use of undeclared identifier (namespace 名前解決)

原因

名前空間が using されていない。std:: を付け忘れ。

synE23. stray '\357' in program(BOM / 全角空白)

原因

ソースファイルに BOM or 全角スペースが紛れ込んだ。保存形式をUTF-8 (BOM なし)に。

semE24. redefinition of 'class T'

原因

ヘッダガードが無い / #pragma once 抜け。

my.hOK
#pragma once struct T { };