操作员<<不匹配
no match for operator <<
我正在用Deitel的书学习c++。我试图编译操作符重载的例子,但它失败了。你能给我解释一下吗?下面是代码:
类原型:
// PhoneNumber class definition
#include <iostream>
#include <string>
#ifndef PHONENUMBER_H
#define PHONENUMBER_H
class PhoneNumber
{ // Start class PhoneNumber
friend std::ostream & operator<<( std::ostream &output, const PhoneNumber &number );
friend std::istream & operator>>( std::istream &input, PhoneNumber &number);
private:
// 3-digit area code
std::string areaCode;
// 3-digit exchange
std::string exchange;
// 4-digit line
std::string line;
}; // End Class PhoneNumber
#endif // PHONENUMBER_H
类定义:
// Overloaded stream insertion and stream extraction operators
// for class PhoneNumber
#include <iomanip>
#include "phonenumber.h"
using namespace std;
// overloaded stream insertion operator; can't be
// a member function if we would like to invoke it with
// cout << somePhoneNumber;
ostream & operator <<( ostream &output, const PhoneNumber &number )
{ // Start operator<<()
output << "(" << number.areaCode << ") " << number.exchange < "-" << number.line;
return output; // enables cout << a << b << c;
} // End operator<<()
// overloaded stream extraction operator; can't be
// a member function if we would like to invoke it with
// cin >> somePhoneNumber
istream & operator >>( istream &input, PhoneNumber &number )
{ // Start operator>>()
// skip (
input.ignore();
// input area code
input >> setw( 3 ) >> number.areaCode;
// skip ) and space
input.ignore( 2 );
// input exchange
input >> setw( 3 ) >> number.exchange;
// skip dash(-)
input.ignore();
// input line
input >> setw( 4 ) >> number.line;
return input; // enables cin >> a >> b >> c;
} // end operator>>()
测试功能:
// Demonstrating class PhoneNumber's overloaded stream insertion
// and stream extraction operators
#include <iostream>
#include "phonenumber.h"
using namespace std;
int main(int argc, char *argv[])
{
PhoneNumber phone; // create object phone
cout << "Enter phone number in the form (123) 456-7890:" << endl;
// cin >> phone invokes operator>> by implicitly issuing
// the non-member function call operator>>(cin, phone )
cin >> phone;
cout << "The phone number entered was: ";
// cout << phone invokes operator<< by implicitly issuing
// the non-member function call operator<<( cout, phone)
cout << phone << endl;
return 0;
}
当我尝试构建它时,我得到一个错误消息:
phonenumber.cpp:13: error: no match for ‘operator<<’ (operand types are ‘const char [2]’ and ‘const string {aka const std::__cxx11::basic_string<char>}’)
output << "(" << number.areaCode << ") " << number.exchange < "-" << number.line;
你能告诉我错误的原因是什么吗? 你有一个简单的打字错误:
output << "(" << number.areaCode << ") " << number.exchange < "-" << number.line;
专:number.exchange < "-"
应:number.exchange << "-"
编译器认为您试图对两个字符串使用<
操作符。只需修改行,它就可以工作了。
现场演示!
系统给我的错误信息是:
main.cpp:30:71: error: no match for 'operator<<' (operand types are 'const char [2]' and 'const string {aka const std::__cxx11::basic_string<char>}')
output << "(" << number.areaCode << ") " << number.exchange < "-" << number.line;
~~~~^~~~~~~~~~~~~~
相关文章:
- Qt SQLite没有查询或参数计数不匹配
- 模板参数推导失败,函数参数/参数不匹配
- 在使用累加时,C++中的运算符+不匹配
- C++ 与操作员不匹配<<
- 在 Arduino 上使用 sscanf 会导致与 const char * 不匹配,并且返回值始终相同,尽管输入值不同
- 与'operator='不匹配(操作数类型'String'且"void")
- C++模板/别名 - 模板参数列表中参数 1 处的类型/值不匹配
- C4018:类内有符号、无符号不匹配
- 我在 .h 中有一个枚举类,并且在.cpp错误中有一个运算符重载:与"运算符<<不匹配
- 为什么我收到错误:"运算符<<不匹配?
- RE2 不匹配非 ASCII 字符
- 函数签名与调用的函数不匹配,常量字符[]和字符*之间的区别?
- 模板推导:为什么函数指针模板定义在常量和/或引用时不匹配?
- 错误:"模板<类_Tp,类_Dp>类 std::unique_ptr"的模板参数列表中参数 1 的类型/值不匹配
- OpenSSL fips in C++ wrapper Library 如何?错误:指纹不匹配
- 为什么我的数组值与此处的全局变量不匹配?
- 引号之间匹配/不匹配,带有不可避免的引号和多行
- 映射迭代器与运算符不匹配
- C++调用和定义不匹配
- 模板专用化与派生类不匹配