【问题标题】:Debugging a simple program in C++用 C++ 调试一个简单的程序
【发布时间】:2015-07-27 07:49:15
【问题描述】:

我正试图找出我的程序出了什么问题。它没有输出我想要的输出。

为什么不显示任何错误但不显示输出?

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

【问题讨论】:

  • cout

标签: c++ class


【解决方案1】:

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

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

Here is an example

【讨论】:

    【解决方案2】:

    你没有将你的名字传递给NameFunction

    cout<<NameObj.NameFunction("" , "");
    //                 blank   ^^   ^^
    

    应该是:

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

    【讨论】:

      【解决方案3】:

      首先你需要包含标题&lt;string&gt;

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

      【讨论】:

        猜你喜欢
        • 2014-11-18
        • 2012-11-02
        • 2016-08-28
        • 1970-01-01
        • 2020-03-07
        • 1970-01-01
        • 2021-06-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多