2013-10-16 95 views
0

我有一個客戶類,如下圖所示C++運算符>>重載

#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' 
+1

您已經顯示了'operator >>',但錯誤消息是關於'operator <<'的。你已經定義了'ostream&operator <<(ostream&,Customer&)'。您通常需要'Customer const&'。 –

回答

0

您似乎正在將customerInfo [0] etc對象插入到您創建的cust對象中。你不能像這樣插入一個對象。插入/提取操作符在被重載時只能與原始數據類型一起使用,即在對象中存儲和顯示值。

+0

我想設置客戶的屬性,customerInfo [0]是一個字符串值 –

+0

如果你想從字符串中讀取值到對象中,那麼你需要編寫一個單獨的函數,它可以解析字符串(可能是你的字符串是',',' - ','_'等分隔符)並填充對象的字段。 – AquaAsh

+0

我有一個函數已經這樣做:) customerInfo是一個字符串值的向量,我試圖用>>來設置它們在對象中。 –

1

此操作:

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; 
+0

我試圖設置客戶的屬性,customerInfo [0]是一個字符串值 –