我正在创建一个模拟电话和短信的程序。我试图将类对象'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
答案 0 :(得分:3)
使用以下代码定义强制转换运算符:
operator int() const {return phonenumberval;}
...我想您尝试使用此代码调用:
phonenumberval = phone1. PhoneNumber;
......但这不是它的工作方式。要调用强制转换操作符,您必须执行以下操作:
phonenumberval = (int)phone1;
在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
// ...
}