我需要 c++ 结构方面的帮助
I need help in structure in c++
该程序是采用一个对象名称为"st"的结构,将采用年龄,然后取名字和姓氏比标准
但它说的是这个错误
(main.cpp:33:10:错误:无效使用非静态成员函数"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;
}
现在真的不清楚你想用你的代码实现什么。
首先,你的问题是因为试图将一些输入放入接受参数的成员函数中,你需要将你的输入放入临时参数中并传递它们,你还应该将你的成员函数重命名为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);
然后你尝试使用相同的函数输出它们而不再次调用它们,但即使你这样做了,它们也会返回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
在一个班级里意味着什么,现在你所有的Student
类实例都会共享年龄、first_name、last_name和标准,这可能不是你所想的。
相关文章:
- 需要帮助设置在C++中使用的Potrace
- 在指针的帮助下,文本文件中单词的频率
- 计算每个节点的树高,帮助我解释这个代码解决方案
- 如何在Qbutton的帮助下更改Q对话框的宽度
- 需要帮助将结构数组传递给函数
- GCC和Clang在与__builtin_constant_p相关的static_assert方面有所不同
- C++需要帮助从用户那里获得一个整数,并确保它在另外两个整数之间
- 需要帮助在 c++ 中将字符串转换为字符 ----错误 "const char *" 类型的值不能用于初始化 "char" 类型的实体
- 有人可以帮助我处理正则表达式吗?
- C++调用具有 *this 属性的单个帮助程序函数
- C++:需要帮助了解运算符重载错误
- 我需要 c++ 结构方面的帮助
- 我需要 c++ 结构方面的帮助
- 我需要 c++ 方面的帮助
- 需要 C++ 模板动态内存分配方面的帮助
- 在结构指针方面需要帮助
- Qt Creator 5.3在理解帮助文档方面需要帮助
- 在获取CPU频率方面需要一些帮助
- 需要c++代码方面的帮助
- 我需要一些货币计划方面的帮助