#include class Sentence { public: Sentence(std::string const& input) : data {input} { if( words() < 1) { throw std::logic_error{"För kort mening!"}; } if( words() > 9 ) { throw std::logic_error{"För lång mening!"}; } } void add_word(std::string const& new_word) { if (words() >= 9) { throw std::logic_error{"Redan maximal längd för meningen!"}; } data += " "; data += new_word; } private: std::string data; int words() const { int spaces {}; for(char l : data) { if (l == ' ') { ++spaces; } } return spaces + 1; } }; Sentence operator+(std::string const& new_word, Sentence const& sentence) { Sentence new_sentence {sentence}; new_sentence.add_word(new_word); return new_sentence; }