2015-12-15 88 views
3

我對C++很陌生,我試圖打印出一個機構的向量,這是我創建的一種對象類型。該對象的創建和我的程序的其餘部分運行得很好,但是當我嘗試打印出該向量時,「< <」給出了一個錯誤,指出「操作數類型是std :: ostream」。使用std :: ostream打印矢量

void PrintVector(const vector<Institution> &institutions) 
{ 
    for (int x = 0; x < institutions.size; x++) 
    { 
     cout << institutions.at(x) << endl; 
    } 
} 

我試圖做的是什麼標準:: ostream的研究,或者做什麼,但因爲我不知道很多關於C++(或一般程序),我無法理解任何的解釋它的網站。爲什麼通常的「cout < <」在這種情況下工作?任何人都可以向我解釋這是什麼意思,或者如果有不同的方式來打印出我不需要的矢量?

任何幫助表示讚賞,謝謝。

+0

你寫過的'操作符<<'你'Institution'類? – Chad

+0

由於您知道'x'的值是向量中的有效索引,因此使用'institutions.at(X)'檢查它們沒有意義。只需使用'院校[x]'。更好的是,閱讀迭代器。 –

回答

4

您可能需要重載ostream的操作員(< <)爲您的類院校: https://msdn.microsoft.com/en-us/library/1z2f6c2k.aspx

ostream& operator<<(ostream& os, const Institution& inst) 
{ 
    os << inst.foo; /* some member variable */; 
    return os; 
} 
+0

好吧,那麼它說inst.foo,我應該把其中一個成員在inst對象? –

+2

@ LiliaO.Yes,你會把你想打印的所有成員 – Tas

+1

@Tas如果我想打印多個成員,我會這樣做嗎? 'os << inst.x << inst.y << inst.z;' –

1

你必須爲你的類的operator <<

std::ostream& operator << (std::ostream& os, const Institution& institution) 
{ 
    os << institution.getValue(); 
    // ... 
    return os; 
} 
1

operator<<過載,使輸出的內置類型,如intdouble。但是,你需要告訴編譯器如何輸出類Institution再次超載:

std::ostream& operator<<(std::ostream& os, const Institution& inst) { 
    os << inst.name(); // or something 
    return os; 
} 
+1

男子比賽很艱難T.T –

0

至少有兩個問題,您顯示的代碼。

1)

for (int x = 0; x < institutions.size; x++) 

的std ::矢量::尺寸()是一類方法,函數。它不是班級成員。這應該閱讀:

for (int x = 0; x < institutions.size(); x++) 

2)

cout << institutions.at(x) << endl; 

不幸的是,std::ostream知道也不關心你的Institution類。您將需要實現一個類方法,例如display(),它將組裝類的內容的可打印表示,並將其寫入輸出流。因此,舉例來說,如果類包含兩個std::string S,叫name,並address

class Institution { 

// ... 

public: 

     void display(std::ostream &o) 
     { 
      o << name << std::endl << address << std::endl; 
     } 
// ... 
}; 

...或者,在任何格式樣式,你要顯示你的類的實例。然後:

for (int x = 0; x < institutions.size(); x++) 
    institutions.at(x).display(std::cout); 

3)

那麼,這裏的獎金問題與您的代碼。它實際上是用過時的C++方言編寫的,幾十年前就已經過時了。無論你用什麼教科書學習C++,你都需要拋棄它,並拿起本世紀編寫的教科書,並教你現代C++。在現代C++中,這變得更可讀:

for (const auto &institution:institutions) 
    institution.display(std::cout); 
0

這裏的答案都是正確的。我將提供與顯示問題略有不同的答案,因爲這是一個反覆出現的問題。你可以做的是定義一個抽象類,我們將其稱爲IDisplay,它聲明一個純虛函數std::ostream& display(std::ostream&) const並聲明operator<<作爲朋友。然後,每個想要顯示的類都必須從IDisplay繼承,並因此實現display成員函數。這種方法重用了代碼,非常優雅。下面的例子:

#include <iostream> 

class IDisplay 
{ 
private: 
    /** 
    * \brief Must be overridden by all derived classes 
    * 
    * The actual stream extraction processing is performed by the overriden 
    * member function in the derived class. This function is automatically 
    * invoked by friend inline std::ostream& operator<<(std::ostream& os, 
    * const IDisplay& rhs). 
    */ 
    virtual std::ostream& display(std::ostream& os) const = 0; 

public: 
    /** 
    * \brief Default virtual destructor 
    */ 
    virtual ~IDisplay() = default; 

    /** 
    * \brief Overloads the extraction operator 
    * 
    * Delegates the work to the virtual function IDisplay::display() 
    */ 
    friend inline 
    std::ostream& operator<<(std::ostream& os, const IDisplay& rhs) 
    { 
     return rhs.display(os); 
    } 
}; /* class IDisplay */ 

class Foo: public IDisplay 
{ 
public: 
    std::ostream& display(std::ostream& os) const override 
    { 
     return os << "Foo"; 
    } 
}; 

class Bar: public IDisplay 
{ 
public: 
    std::ostream& display(std::ostream& os) const override 
    { 
     return os << "Bar"; 
    } 
}; 

int main() 
{ 
    Foo foo; 
    Bar bar; 
    std::cout << foo << " " << bar;  
} 

Live on Coliru