Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 与班级一起工作,但可以';不要显示答案。_C++_Class_Operator Overloading - Fatal编程技术网

C++ 与班级一起工作,但可以';不要显示答案。

C++ 与班级一起工作,但可以';不要显示答案。,c++,class,operator-overloading,C++,Class,Operator Overloading,我真的不想写这个问题,但我似乎无法输出名称。这是我们班作业的一部分,也是作业中最难的部分,但我的老师说,只要我们真正了解并理解我们在做什么以及为什么要这样做,我们就可以得到帮助并寻求他人的帮助。所以我问这个问题是为了帮助我理解为什么我的程序不会输出人名 我想做的是;输入一个名字“莱昂纳多·达·芬奇”,名字中有大写和小写字母,以及他们的出生日期“1520”,然后输入代码,将名字转换成小写,只大写单词中的第一个字母,最后输出该名字 我一直坚持的是输出名字。我只是不知道我做错了什么。我问过班上的其他人

我真的不想写这个问题,但我似乎无法输出名称。这是我们班作业的一部分,也是作业中最难的部分,但我的老师说,只要我们真正了解并理解我们在做什么以及为什么要这样做,我们就可以得到帮助并寻求他人的帮助。所以我问这个问题是为了帮助我理解为什么我的程序不会输出人名

我想做的是;输入一个名字“莱昂纳多·达·芬奇”,名字中有大写和小写字母,以及他们的出生日期“1520”,然后输入代码,将名字转换成小写,只大写单词中的第一个字母,最后输出该名字

我一直坚持的是输出名字。我只是不知道我做错了什么。我问过班上的其他人,但他们仍然在第一部分,或者他们在某种程度上被这个问题困住了

class Person{ //Can't change from here.
public:
    Person(string n, int year) : name(n), yearOfBirth(year){}
    string getName();
    friend ostream& operator <<(ostream&, const Person&);
private:
    string name; int yearOfBirth;
}; // to here.

string Person::getName() //output the name
{
    // I think the output should be here but it doesn't work.
    // I have also switched the code in the operator into the getName put it still doesn't work.
    return name;
}

ostream& operator << (ostream& output, const Person& P){ //turns everything into lowercase.
    locale loc;
    string temp = ""; //well be asigning the lowercase letters here; one at a time.
    int ii = 0;
    for (string::size_type i = 0; i < P.name.length(); i++) { //reads the input one at a time.
        if ((ii == 0) || (ii >= 1 && ((P.name[i - 1]) == char(32)))) {(toupper(P.name[i], loc), (temp += toupper(P.name[i], loc))); }//if it was lowercase and the first letter, it turns it into uppercase and assigns it to temp.
        else (tolower(P.name[i], loc), (temp += tolower(P.name[i], loc))); //if it was uppercase it turns it into lowercase and assigns it to temp.
        ii++;
    } //name = temp; //assigns the full lowercase word back to input01.*/
    output << temp << " was born in " << P.yearOfBirth;
    return output;
}

int main(){
    Person::Person("lEonArDo DA vINCi", 1520);
    // I have tried putting a output statement here as well
    return 0;
}
class Person{//无法从此处更改。
公众:
Person(字符串n,int year):姓名(n),出生年份(year){
字符串getName();
friend ostream&运营商
不能造就一个人。你想要的更多的是

Person leonardo("lEonArDo DA vINCi", 1520);
这使得名为
leonardo
的变量类型为
Person
,并使用
“leonardo DA vINCi”对其进行初始化,然后可以使用1520
leonardo
访问此
Person
。例如,获取并打印姓名

std::cout << leonardo.getName() << std::endl;

<代码> STD::CUT请考虑编辑你的问题要更加简洁。所有关于你的班级、老师、同学等的信息是不必要的。你所要求的不是很清楚。<代码> GETNAME()不应该负责写入任意的
std::ostream
,只需返回存储的内容即可。
操作员银行!我完全错过了这一点。非常感谢。这是一个很大的帮助。
std::cout << leonardo.getName() << std::endl;
std::cout << leonardo <<std::endl;