C++ 我的字符串不是';t从私有变量复制
除了get和set name函数外,我的编码中的所有功能都正常工作。当我调用getName时,它打印为空白。我尝试了几种不同的解决方案,但唯一有效的方法是将全名字符串保存在main中,并从那里调用它。这就好像它不允许我调用变量,因为它们是私有的 这是我的.cpp文件C++ 我的字符串不是';t从私有变量复制,c++,string,get,set,C++,String,Get,Set,除了get和set name函数外,我的编码中的所有功能都正常工作。当我调用getName时,它打印为空白。我尝试了几种不同的解决方案,但唯一有效的方法是将全名字符串保存在main中,并从那里调用它。这就好像它不允许我调用变量,因为它们是私有的 这是我的.cpp文件 #ifndef STUDENT_H #define STUDENT_H #include <iostream> #include <string> #include "Student.h" using n
#ifndef STUDENT_H
#define STUDENT_H
#include <iostream>
#include <string>
#include "Student.h"
using namespace std;
int main()
{
//for student name
string firstName;
string lastName;
string fullName;
//student name
cout << "Please enter your students first name" << endl;
cin >> firstName;
cout << "firstName = " << firstName << endl;
cout << "Please enter your students last name" << endl;
cin >> lastName;
cout << "lastName = " << lastName << endl;
aStudent.setName(firstName, lastName);
fullName = aStudent.getName();
cout << "Your students name is : ";
cout << fullName << endl;
}
#endif
\ifndef学生
#定义学生
#包括
#包括
#包括“Student.h”
使用名称空间std;
int main()
{
//学生姓名
字符串名;
字符串lastName;
字符串全名;
//学名
不能直呼其名;
库特
你肯定看到了问题所在。提示,赋值将右边的东西复制到左边的东西。你的代码结构很混乱,头文件应该只有方法声明。哇,我肯定花了太多时间看完整的程序而错过了类似的东西。谢谢你,太尴尬了!
void Student::setName(std::string firstName, std::string lastName)
{
firstName = fName; // you're assigning the local firstName to your class instance's
lastName = lName; // variable; reverse this and try again
}
// ...
void Student::setName(std::string firstName, std::string lastName)
{
fName = firstName;
lName = lastName;
}
void Student::setName(std::string firstName, std::string lastName)
{
firstName = fName;
lastName = lName;
}
void Student::setName(std::string firstName, std::string lastName)
{
firstName = fName; // you're assigning the local firstName to your class instance's
lastName = lName; // variable; reverse this and try again
}
// ...
void Student::setName(std::string firstName, std::string lastName)
{
fName = firstName;
lName = lastName;
}