【发布时间】:2020-03-26 21:37:41
【问题描述】:
这是我的代码
#include <iostream>
#include <string>
using namespace std;
class Hamoud{
private:
char name[50];
int yearbirthday;
public:
float tall;
int age;
void Getnameandyear(string name)
{
std::array<char, 50> ;
cout<<"enter your name: ";
getline(nama);
int year;
year = yearbirthday;
cout<<"enter your name: ";
getchar(name);
cout<<"Enter your year of birth";
cin>>year;
}
void display()
{
cout<<"your name is: "<<name;
cout<<"your year of birth is : "<<year;
}
};
int main ()
{
Hamoud info;
cout<<"enter your tall ";
cin>>info.tall;
cout<<"your tall is : "<<info.tall;
cout<<"enter your age: ";
cin>>info.age;
cout<<"your age is: "<<info.age;
info.Getnameandyear();
info.display();
}
但我在函数 getnameandyear 中也遇到了错误,在显示函数中也是如此... 我知道要访问类的私有成员,我们必须在公共创建一个函数,这将帮助我们间接访问...... 但是,我被困在最后几个步骤.. 知道如何解决这个问题吗?
【问题讨论】:
-
nama[50] = name[50]不会按照你的想法去做。它实际上是UB。更喜欢std::array<char, 50>甚至std::string。 -
我试过 std::string 但也没用?
-
“我试过 std::string”:显示该代码,否则很难提供帮助。同时为两个不同的东西选择
nama和name对代码的可读性没有帮助。 -
请不要在 cmets 中发布代码,而是edit 问题。
-
为什么函数
Getnameandyear(string name)需要一个参数?您正在使用它没有参数。
标签: c++ function class private public