2015-12-15 89 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