[英]I need help in structure in c++
該程序是采用 object 名稱“st”的結構將采用年齡,然后是名字和姓氏比標准
但它說這個錯誤
(main.cpp:33:10:錯誤:無效使用非靜態成員 function 'void Student::age(int)')
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
struct Student{
static string f,l;
static int a,s;
void age(int ag);
void first_name(string fi)
{
f=fi;
}
void last_name(string la)
{
l=la;
}
void standard(int st)
{
s=st;
}
};
void Student :: age( int ag)
{
a=ag;
}
int main() {
Student st;
cin >> st.age >> st.first_name >> st.last_name >> st.standard;
cout << st.age << " " << st.first_name << " " << st.last_name << " " << st.standard;
return 0;
}
現在真的不清楚你想用你的代碼實現什么。
首先,您的問題是因為嘗試將一些輸入放入采用 arguments 的成員函數中,您需要將您的輸入放入臨時 arguments 並傳遞它們,您還應該重命名您的成員set_age
等以指示set_first_name
他們在做什么。
Student st;
int age;
std::string first_name;
std::string last_name;
int standard;
std::cin >> age >> first_name >> last_name >> standard;
st.set_age(age);
st.set_first_name(first_name);
st.set_last_name(last_name);
st.set_standard(standard);
然后你試圖 output 使用相同的函數而不再次調用它們,但即使你這樣做了,它們也會返回void
,所以什么也沒有。 您需要一組不同的成員函數來訪問這些變量。
class Student{
int age;
/* rest of the code */
int get_age() const {
return age;
}
};
int main() {
Student student;
student.set_age(10);
std::cout << student.get_age() << '\n';
}
看起來你也不知道static
在 class 中是什么意思,現在你所有的Student
class 實例可能會共享年齡,名字,姓氏和標准,
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.