samedi 30 mars 2019

Why is there no output for friend function?

I can compile and run with no errors, however, there is no output when I run the program.

The friend ostream& operator<<(ostream&, Element&) function should output the Element to the provided ostream.

//In .h

...

struct Element{
public:
  string key_;
  vector<string> values_;
  Element()=default;
  Element(string, initializer_list<string>);
  bool operator==(const Element&)const;
  friend ostream& operator<<(ostream&, Element&);
};

...


//In .cpp:

ostream& operator<<(ostream& os, Element& e1){
    os<<e1.key_<<':';
    copy( e1.values_.begin(), e1.values_.end(), ostream_iterator<string>(cout, ", ") );
return os;

...

int main(){
    Element e1("abc",{"a","b","c"});
    ostringstream oss;
    oss<<e1;
    string result = oss.str();
}

Output format should be something like this:

Element e ("abc",{"a", "b", "c"});
ostringstream oss;
oss << e;
string result = oss.str();
string ans = "abc:a,b,c";

Aucun commentaire:

Enregistrer un commentaire