我的字符串不是从私有变量复制

tru*_*ype 0 c++ string get set

除了get和set name函数之外,我的编码中的所有内容都有效.当我调用getName时,它打印为空白.我尝试了一些不同的解决方案,但唯一有效的方法是将mainName字符串保存在main中,然后从那里调用它.这几乎就好像它不会让我调用变量,因为它们是私有的.

这是我的.cpp文件.

#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
Run Code Online (Sandbox Code Playgroud)

这是我的函数和类,.h文件.

#include <iostream>
#include <string>
#include <conio.h>

using namespace std;
class Student
{

private:
string fName;
string lName; 

public:
string getName();
void setName(string firstName, string lastName);

};

string Student::getName()
{
return fName + " " + lName;
}
void Student::setName(std::string firstName, std::string lastName)
{
firstName = fName;
lastName = lName;
}
Run Code Online (Sandbox Code Playgroud)

Ben*_*ley 8

void Student::setName(std::string firstName, std::string lastName)
{
    firstName = fName;
    lastName = lName;
}
Run Code Online (Sandbox Code Playgroud)

当然你在那里看到了问题.提示,赋值将右边的东西复制到左边的东西上.