C++ 运算符 >> 重载

标签 c++ operator-overloading

我有一个如下的客户类,

#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include "SimpleDate.h"

using namespace std;

class Customer {

private:
    string customerId;
    string name;
    string address;
    string city;
    string postCode;
    SimpleDate* dateLastPurchased;
    double dollarsOwed;

public:
    //Customer(string customerIdVal, string nameVal);
    Customer();
    string getCustomerId();
    string getName();
    void setAddress(string addressVal);
    string getAddress();
    void setPostCode(string postCodeVal);
    string getPostCode();
    void setCity(string cityVal);
    string getCity();
    void setDateLastPurchased(SimpleDate* date);
    SimpleDate* getDateLastPurchased();
    void addDollarsOwed(double amount);
    double getDollarsOwed();

    friend ostream& operator<< (ostream &out, Customer &cust);
    friend istream& operator>> (istream &in, Customer &cust);
};

cpp 文件中的重载部分如下所示

ostream& operator<< (ostream &out, Customer &cust)
{
    out << cust.customerId << "\t" << cust.name << "\t" << cust.address << "\t" << cust.city << "\t" << cust.postCode << "\t" << cust.dateLastPurchased->getFullDate() << "\t" << cust.dollarsOwed << "\t" << std::endl;
    return out;
}

istream& operator>> (istream &in, Customer &cust)
{
    in >> cust.customerId;
    in >> cust.name;
    in >> cust.address;
    in >> cust.city;
    in >> cust.postCode;

    string stringData;
    in >> stringData;

    std::istringstream iss(stringData);
    std::string datePart;
    int tmp;
    vector<int> dateData;

    while(std::getline(iss, datePart, '/')) {

        sscanf(datePart.c_str(), "%d", &tmp);       
        dateData.push_back(tmp);
    }

    SimpleDate* date = new SimpleDate(dateData[2], dateData[1], dateData[0]);
    cust.dateLastPurchased = date;

    string stringDollarsOwed;
    in >> stringDollarsOwed;

    sscanf(stringDollarsOwed.c_str(), "%lf", &cust.dollarsOwed);

    return in;
}

然后在我的主类中,我尝试创建一个客户对象,如下所示,

Customer* cust = new Customer();

    cust << customerInfo[0] << customerInfo[1] << customerInfo[2] << customerInfo[3] << customerInfo[4] << customerInfo[5] << customerInfo[6];

但是当我编译时出现以下错误,

你能帮忙吗?

谢谢。

AppManager.cpp: In member function 'Customer* AppManager::createCustomerObject(std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >)':
AppManager.cpp:445: error: no match for 'operator<<' in '& cust << customerInfo.std::vector<_Tp, _Alloc>::operator[] [with _Tp = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _Alloc = std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >](0u)'
Customer.h:46: note: candidates are: std::ostream& operator<<(std::ostream&, Customer&)
*** Error code 1
make: Fatal error: Command failed for target `Tour'

最佳答案

这个运营商:

ostream& operator<< (ostream &out, Customer &cust)

允许你做类似的事情

Customer c;
std::cout << c << std::endl;
std::ofstream output("customers.txt");
output << c << std::endl;

并且不是

Customer c;
c << x;

绝对不是

Customer* c = ...;
c << x;

关于C++ 运算符 >> 重载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19395620/

相关文章:

c++ - C++ 数据类型后的符号 '^' 是什么意思?

c++ - if-else 函数的代码优化

c++ - 重载 operator= 不起作用

c++ - C++ 中 unsigned char 的 ostream 运算符重载

c++ - 如何在 Linux 上强制关闭套接字?

C++ 二进制写入/读取 32 位到/从 64 位

c++ - for 循环中没有检查条件,但循环仍然终止 C++

c++ - 以允许响应更新的方式重载 C++ 索引下标运算符 []

c++ - 如何根据精度轻松模板化数学函数?

c++ - 函数调用运算符