【解決方法】なぜこれが "非静的メンバー関数" エラー?

プログラミングQA

[ad_1]

テキスト ベースのアドベンチャー ゲーム C++ を作成しています
コードは次のとおりです。

C++
#include <iostream>
#include <string>
#include <cmath>
#include <stdlib.h>
#include <random>
#include <ctime>
#include <ctype.h>
#include "bits/stdc++.h"

using namespace std;

class Player {

    public:
        int d6_1;
        int d6_2;
        int d6_3;
        int d6_4;
        int d6_5;
        int d6_6;
        double pi = 2 * acos(0.0);
        string playerClass;
        string name;
        string race;
        int strength = 10;
        int dexterity = 10;
        int constituion = 10;
        int intelligence = 10;
        int wisdom = 10;
        int charisma = 10;
        string meleeWeapon = "fists";
        string rangedWeapon;
        string magic;
        
        void statAssign() {
            for (int i = 0; i < 7; i++) {
                srand(time(NULL));
                int d6 = (rand()/pi);
                switch(i) {
                    case 1:
                        d6 = d6_1;
                        cout << abs(d6_1%6+1) << "\n";
                        d6=0;
                        break;
                    case 2:
                        d6 = d6_2;
                        cout << abs(d6_2%6+1) << "\n";
                        d6=0;
                        break;
                    case 3:
                        d6 = d6_3;
                        cout << abs(d6_3%6+1) << "\n";
                        d6=0;
                        break;
                    case 4:
                        d6 = d6_4;
                        cout << abs(d6_4%6+1) << "\n";
                        d6=0;
                        break;
                    case 5:
                        d6 = d6_5;
                        cout << abs(d6_5%6+1) << "\n";
                        d6=0;
                        break;
                    case 6:
                        d6 = d6_6;
                        cout << abs(d6_6%6+1) << "\n";
                        d6=0;
                        break;
                }
        }
    }
};

int main() {
    Player player1;
    player1.statAssign;
    cout << "Type everything in lowercase" << "\n";
    cout << "What is your name, traveler?" << "\n";
    bool nameRestart = true;
    while (nameRestart == true) {
        nameRestart = false;
        cout << "> ";
        cin >> player1.name;
        cout << player1.name << "? Are you sure that's your true name?" << "\n";
        string nameCheck;
        cin >> nameCheck;
        if (nameCheck == "no") {
            nameRestart = true;
        } else {
            break;
        }
    }
    cout << "Where do you hail from " << player1.name << "?" << "\n";
    cout << "The Human Cities? Perhaps the Elven Forests? or the Mightly Dwarven Kingdoms?" << "\n";
    string raceChoice;
    cin >> raceChoice;
    /*
    switch(raceChoice) {
        case human:
            player1.race = "human";
            player1.strength++;
            player1.constituion++;
            player1.charisma++;
            
    }
    */

}

これは私の目には問題ないように見えますが、実行すると次のエラーが表示されます。

C++
main.cpp: In member function ‘void Player::statAssign()’:
main.cpp:38:21: warning: variable ‘d6’ set but not used [-Wunused-but-set-variable]
   38 |                 int d6 = (rand()/pi);
      |                     ^~
main.cpp: In function ‘int main()’:
main.cpp:77:13: error: invalid use of non-static member function ‘void Player::statAssign()’
   77 |     player1.statAssign;
      |     ~~~~~~~~^~~~~~~~~~
main.cpp:35:14: note: declared here
   35 |         void statAssign() {
      |              ^~~~~~~~~~
make: *** [<builtin>: main.o] Error 1

誰かがこれを手伝ってくれるなら、どうぞよろしく!

私が試したこと:

グーグルとドキュメンテーションをうんざりさせます。

解決策 1

メンバー関数を呼び出すときは、関数呼び出し構文を使用する必要があります ()

例えば

C++
player1.statAssign();

clang は、より役立つ診断を提供します。

main.cpp:77:13: error: reference to non-static member function must be called; did you mean to call it with no arguments?
    player1.statAssign;
    ~~~~~~~~^~~~~~~~~~

[ad_2]

コメント

タイトルとURLをコピーしました