LoginSignup
2
0

More than 1 year has passed since last update.

C++でエラって詰んだ時の備忘録

Last updated at Posted at 2021-07-17

2時間ぐらい理由不明で詰んでた時の対処法を載せるだけの備忘録

定義してあるはずのものが未定義とエラーが出る

class hoge{
    int a;

public:
    void fuge(){
        a = 5; //aは未定義
    }
}

解決策

・VSの再起動
・ビルドではなくリビルドをする

これで治らなければほかのを試す

静的アサーションに失敗 and 関係なさそうなエラーいっぱい

a.h
#include "b.h"

class a{
    b* pb;

     /*省略*/
}
b.h
#include "a.h"

class b{
    a* pa;

    /*省略*/
}

解決策

ヘッダー同士でincludeしあっているのが原因なので、片方includeを外してクラスの宣言だけ書いておく。

a.h
#include "b.h"

class a{
    b* pb;

     /*省略*/
}
b.h
class a;

class b{
    a* pa;

    /*省略*/
}

そもそも設計がよくない可能性もあるけどここでは触れないです

オブジェクトにメンバ関数と互換性のない型修飾子があります

class abc {
    int a;
    void fuga() {
        //なんか
    }
    void hoge() const {
        fuga();
        a = 0;
    }
}

解決策

hogeconstなので関数内で、constではない関数呼び出しや変数の変更は不可
std::vector::atとかのconstありなしでオーバーロードされてる関数は、constありしか呼べない。push_backなどはそもそもintelisenseにも表示されないのでそこで頑張って気づけ自分。

class abc{
    int a;
    void fuga() {
        //なんか
    }
    void hoge() {
        fuga();
        a = 0;
    }
}

最後に

思い出したり、出くわしたら追記する
正しい言葉ではなかったりするかもしれませんが自分がわかればおkなのです。
間違ってたりしたら教えてください。

2
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0