c ++この宣言にはストレージクラスまたは型指定子がありません



C This Declaration Has No Storage Class



#include using namespace std #include //Add this file as long as it involves string print output! ! ! struct student { string name int age int score } //1, struct structure name variable name struct student s1 s1.name = 'mike'//Error: This statement has no storage class or type specifier! ! ! s1.age = 12//Error: This statement has no storage class or type specifier! ! ! s1.score = 90//Error: This statement has no storage class or type specifier! ! ! int main() { system('pause') return 0 }

上記のコードはエラーを報告します:
画像
エラープロンプト:s1このステートメントにはストレージクラスまたは型指定子がありません!

情報を確認し、次の理由を見つけてください。



クラス内関数は、本体の外部でのみグローバル変数とオブジェクトを定義でき、ステートメントを実行したり、関数を呼び出したりすることはできません。

間違った理由: 16/17/18の3行のステートメントは、関数本体で実行する必要があります。 ! !関数の外に配置しないでください!!!



#include using namespace std #include //As long as the string print output is involved, add this file! ! ! struct student { string name int age int score } int main() { //1, struct structure name variable name struct student s1 s1.name = 'mike'//Error: This statement has no storage class or type specifier! ! ! s1.age = 12//Error: This statement has no storage class or type specifier! ! ! s1.score = 90//Error: This statement has no storage class or type specifier! ! ! system('pause') return 0 }

16/17/18の3行のステートメントは、エラーを報告せずに関数本体に配置する必要があります。

画像
手段: 本体外の関数は、変数や関数定義の宣言などでしか実行できず、ステートメントを実行したり、関数を呼び出したりすることはできません。 ! !