#include<iostream>
using namespace std;
class DanielClass
{
public:
string NameFunction(string first_name, string last_name)
{
return fullname = first_name + " " + last_name;
}
private:
string fullname;
};
int main()
{
string namefirst;
string namelast;
DanielClass NameObj;
cout<<"Enter your first name: ";
cin>>namefirst;
cout<<"Enter your last name: ";
cin>>namelast;
cout<<"Your full name is: ";
cout<<NameObj.NameFunction("" , "");
return 0;
}
答案 0 :(得分:1)
答案 1 :(得分:1)
您没有将自己的姓名传递给NameFunction
。
cout<<NameObj.NameFunction("" , "");
// blank ^^ ^^
应该是:
cout<<NameObj.NameFunction(namefirst , namelast);
答案 2 :(得分:0)
首先,您需要包含标题<string>
#include <string>
而不是陈述
cout<<NameObj.NameFunction("" , "");
你应该写
cout<<NameObj.NameFunction( namefirst , namelast );
至于我,我会按以下方式宣布课程
#include <iostream>
#include <string>
class DanielClass
{
public:
DanielClass( const std::string &first_name, const std::string &last_name )
: fullname( first_name + " " + last_name )
{
}
std::string GetFullName() const
{
return fullname;
}
private:
std::string fullname;
};
主要看起来像
int main()
{
std::string namefirst;
std::string namelast;
std::cout << "Enter your first name: ";
std::cin >> namefirst;
std::cout << "Enter your last name: ";
std::cin >> namelast;
DanielClass NameObj( namefirst, namelast );
std::cout << "Your full name is: ";
std::cout << NameObj.GetFullName() << std::endl;
return 0;
}