简单程序[HELP] c ++

时间:2015-05-15 11:40:45

标签: c++ class

嘿,伙计们请告诉我我的节目有什么问题。它没有输出我想要的输出。 如果你能向我解释为什么它没有显示任何错误但是没有显示输出,我将非常高兴。

#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;
}

3 个答案:

答案 0 :(得分:1)

您需要将strings传递给函数才能使其正常工作:

cout<<NameObj.NameFunction(namefirst ,namelast);

Here is an example

答案 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;
}