向函数传递对象参数

Passing object parameters to functions c++

本文关键字:对象 参数 函数      更新时间:2023-10-16

我正在创建一个模拟电话和短信的程序。我试图将信息从类对象'phone'传递到我的dialNum和txtNum函数,以计算用户何时将两个电话号码分配为发起和接收电话,但我很难弄清楚该怎么做。

这是我到目前为止的代码:

#ifndef PHONENUMBER_H
#define PHONENUMBER_H
#include <iostream>
#include <iomanip>
using namespace std;
class PhoneNumber {
public:
   PhoneNumber(const PhoneNumber &);
   friend ostream &operator<<( ostream&, const PhoneNumber & );
   friend istream &operator>>( istream&, PhoneNumber & );
   void dialNum();
   void txtNum();
   void displaySummary ();
   operator int() const {return phonenumberval;}
 //  Constructor(int, int);
private:
   char areaCode[ 5 ]; // 3-digit area code and null
   char exchange[ 5 ]; // 3-digit exchange and null
   char line[ 5 ];  // 4-digit line and null
   int phonenumberval;
   static int ntext;
   static int nlive;
}; // end class PhoneNumber
#endif

PhoneNumber::PhoneNumber (const PhoneNumber &phone1){
    phonenumberval = phone1. PhoneNumber;
}
ostream &operator<<( ostream &output, const PhoneNumber &num )
{
   output << "(" << num.areaCode << ")" << num. exchange << "-" << num.line;
   return output; // enables cout << a << b << c;
} // end function operator<<
istream &operator>>( istream &input, PhoneNumber &num )
{
   input.ignore(0);
   input >> setw( 4 ) >> num.areaCode;
   input.ignore( 1 ); // skip ( and space
   input >> setw( 4 ) >> num.exchange;
   input.ignore();
   input >> setw( 5 ) >> num.line;  // input line
   return input;
}
void PhoneNumber::dialNum(){
    int num1;
    int num2;
    cout << "Enter originating phone: " << endl;
    cin >> num1;
    cout << "Enter recieving phone: " << endl;
    cin >> num2;
    cout << " Calling number " << num2 <<"...call made."<< endl;
    cout << "The const int phonenumberval = " << phonenumberval;
}
void PhoneNumber::txtNum(){
    string txt;
    int textphone1;
    int textphone2;
    cout << "Enter orginating phone: " << endl;
    cin >> textphone1;
    cout << "Enter receiving phone: " << endl;
    cin >> textphone2;
    cout << "Enter text message to send." << endl;
    cin >> string txt;
    cout << "Sending message to " << textphone2 << " .....message sent." <<     endl;
}
void PhoneNumber::displaySummary(int a, int b){
    nlive = int b;
    ntext = int a;
    cout << "You made " << nlive << " calls and " << ntext << " texts." << endl;
}
}
int main()
{
   PhoneNumber phone, phone2; // create object phone
   char answer;
   int callCounter = 0;
   int textCounter = 0;
   cout << "Enter phone number in the form (NNN) NNN-NNNN:n";
   cin >> phone;
   cout << "Enter phone number in the form (NNN) NNN-NNNN:n";
   cin >> phone2;
   cout << "The phone number entered was: ";
   cout << phone << endl;
   cout << phone2 << endl;
 do {
 cout << "Enter c to make a a call, t to text, s for summary information, or x to exit. " << endl;
    cin >> answer;

        if (answer == 'c'){
            phone.dialNum();
            ++callCounter;
        }
        else if (answer == 't'){
            cout << "who cares" << endl;
            ++textCounter;
        }
        else if (answer == 's'){
            phone.displaySummary(callCounter, textCounter);  
        }
 } while (answer != 'x');
    cout << "You made " << callCounter << " calls and " << textCounter << " texts." << endl;
    return 0;
 } // end main

当我试图传递对象时,我得到了以下错误:

In copy constructor 'PhoneNumber::PhoneNumber(const PhoneNumber&)':
32:30: error: invalid use of 'PhoneNumber::PhoneNumber'
 In function 'int main()':
67:16: error: no matching function for call to 'PhoneNumber::PhoneNumber()'
67:16: note: candidate is:
30:1: note: PhoneNumber::PhoneNumber(const PhoneNumber&)
30:1: note:   candidate expects 1 argument, 0 provided
67:23: error: no matching function for call to 'PhoneNumber::PhoneNumber()'
67:23: note: candidate is:
30:1: note: PhoneNumber::PhoneNumber(const PhoneNumber&)
30:1: note:   candidate expects 1 argument, 0 provided

问题1:用户自定义强制转换操作符

用下面的代码定义一个强制转换操作符:

operator int() const {return phonenumberval;}

…我猜你试着用下面的代码调用:

phonenumberval = phone1. PhoneNumber;

…,但这不是它的工作方式。要调用强制转换操作符,必须执行以下操作:

phonenumberval = (int)phone1;

问题2:用户定义构造函数

在c++中,一旦提供了用户定义的构造函数(通过声明复制构造函数PhoneNumber(const PhoneNumber &);来实现),就失去了该语言提供的默认构造函数。在您的示例中,您丢失了默认构造函数PhoneNumber();,这意味着您不能在不给构造函数提供参数的情况下创建新的PhoneNumber

只要在PhoneNumber类中添加一个新的构造函数,应该就可以了。

class PhoneNumber {
public:
    PhoneNumber(); // This is the default constructor
    PhoneNumber(const PhoneNumber &); // This is your copy constructor
// ...
};
// ...
int main()
{
    PhoneNumber phone, phone2; // This will call the default constructor, which has to be declared first
    // ...
}