【解決方法】C++ コンストラクタ エラーが発生するのはなぜですか? また、統計に追加しようとしても機能しないのはなぜですか?

プログラミングQA


C++
#include <iostream>
#include <string>
#include <cmath>
#include <stdlib.h>
#include <random>
#include <ctime>

using namespace std;

int iseed = (int)time(0);
srand(iseed);
int d6 = (rand() %6) + 1;
string name;
string race;
int str = d6+d6+6;
int dex = d6+d6+6;
int con = d6+d6+6;
int inte = d6+d6+6;
int wis = d6+d6+6;
int cha = d6+d6+6;
int hp = ((con - 10) / 2)  + d6;
bool simpleWeaponProficiency = false;
bool martialWeaponProficiency = false;
bool exoticWeaponProficiency = false;
bool while_1 = true;

void characterCreation() {
    while (while_1){
        while_1 = false;
        cout << "Your name?: ";
        cin >> name;
        cout << name << ", your sure?" << "\n";
        string nameChoice;
        cin >> nameChoice;
        if (nameChoice == "yes") {
            break;
        } else {
            while_1 = true;
        }
    }
    cout << str << "\n";
    cout << "Alright, " << name << ". where do you hail from?" << "\n";
    cout << "The Human Cities? The Elven Forests perhaps? or the Mighty Dwarven Kingdoms?" << "\n";
    string origin;
    cin >> origin;
    if (origin == "the human cities" or "human cities" or "human") {
        race = "human";
        str + 1;
        dex + 1;
        con + 1;
        inte + 1;
        wis + 1;
        cha + 1;
        cout << race << "?" << "\n";
    }
    cout <<  "Strength:" << str << "\n";
    
}

int main() {
    cout << "Welcome to my adventure game!" << "\n" << "Please type everything in lowercase." << "\n";
    cout << "-----------------------------" << "\n";
    characterCreation();
}

コードがあります。 実行するたびに、次のものが生成されます。

C++
main.cpp:11:6: error: expected constructor, destructor, or type conversion before ‘(’ token
   11 | srand(iseed);
      |      ^
main.cpp: In function ‘void characterCreation()’:
main.cpp:49:13: warning: statement has no effect [-Wunused-value]
   49 |         str + 1;
      |         ~~~~^~~
main.cpp:50:13: warning: statement has no effect [-Wunused-value]
   50 |         dex + 1;
      |         ~~~~^~~
main.cpp:51:13: warning: statement has no effect [-Wunused-value]
   51 |         con + 1;
      |         ~~~~^~~
main.cpp:52:14: warning: statement has no effect [-Wunused-value]
   52 |         inte + 1;
      |         ~~~~~^~~
main.cpp:53:13: warning: statement has no effect [-Wunused-value]
   53 |         wis + 1;
      |         ~~~~^~~
main.cpp:54:13: warning: statement has no effect [-Wunused-value]
   54 |         cha + 1;
      |         ~~~~^~~
make: *** [<builtin>: main.o] Error 1

これらのステートメントが何にも影響を与えていない理由も、上部のエラーを修正する方法もわかりません。 助けてくれる人に感謝します。

私が試したこと:

グーグルや他のフォーラムをうんざりさせます。 さらに、いくつかのドキュメント。

解決策 1

どのメソッドにも含まれていないため、すべての実行可能コード (コンパイル時の定数値による変数の初期化を除く) は、関数本体内にある必要があります。

「影響なし」エラーは、まさに彼らが言うことを意味します: ステートメントは効果がありません (結果が保存されていないか、どこにも使用されていないため)。

コーディング中に毎日、おそらく 1 日に何度も構文エラーが発生することを予期する必要があります。経験の豊富さに関係なく、誰もがそうです。 変数やキーワードのスペルを間違えることがあります。 文字列やコード ブロックを閉じるのを忘れることがあります。 猫があなたのキーボードの上を歩いて、とても奇妙なことをタイプすることがあります。 メソッド呼び出しに必要なパラメーターの数を忘れてしまうことがあります。

我々はすべての間違いを犯します。

そして、私たちは皆そうしているので、構文エラーを修正する必要があります。他の人が修正してくれるのを待つよりも、方法を学んで自分で修正する方がはるかに迅速です! したがって、エラー メッセージの読み方と、コンパイラが間違っていると言っていることに照らして記述されたコードを解釈する方法を学ぶことに少し時間を費やしてください。

だからこれを読んでください: 問題を解決するコードの書き方、初心者向けガイド パート 2: 構文エラー[^] – 次回コンパイル エラーが発生したときに役立つはずです。

コメント

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