向函数传递对象参数
Passing object parameters to functions c++
我正在创建一个模拟电话和短信的程序。我试图将信息从类对象'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
// ...
}
相关文章:
- 将值从指针复制到常量对象参数
- 隐式对象参数和此指针
- 将对象传递给 Java C++与使用 JNI 逐个设置对象参数
- 如何通过对象参数从指针矢量中删除对象和指针
- 是否有一种方法可以根据派生的可能性值范围限制对象参数
- 从从可调用参数创建的线程对象参数移动构造 C++11 线程
- 为什么我不能通过传入的类对象参数杀死 pthread。
- 无法异步启动带有对象参数的函数
- 为什么在 std::transform 中使用函数对象参数失败并且需要 lambda 表达式
- 实例对象如何查看函数的对象参数的私有成员?
- 在没有对象参数编译器错误的情况下调用非静态成员函数
- 具有常数对象参数的类函数
- 模板功能没有对象参数专业
- 为什么不能在对象参数前面使用常量?
- 具有不同签名的函数对象参数的函数
- 调用非静态成员函数,没有对象参数错误
- 如何从 C# 调用具有 void* 回调和对象参数的 C++ Dll 中的函数
- 对函数使用对象参数或不使用参数
- c++错误:调用没有对象参数的非静态成员函数
- c++对象参数:多态性,值语义,对象生命周期