rowboat
rowboat

Reputation: 3

Unable to output vector with an ostream output C++

Hi there i was wondering if i could get some help with a problem i am having printing out the contents of a vector in C++

I am attempting to output all the variables of a class within one or two function calls in a particular order. However i am receiving an odd error when iterating through a vector

The error i am receiving is Error

 error C2679: binary '=' : no operator found which takes a right-hand operand of type 
'std::_Vector_const_iterator<std::_Vector_val<std::_Simple_types<std::basic_string<char,std::char_traits<char>,std::allocator<char>>>>>' (or there is no acceptable conversion) 

My relevant code is below

ifndef idea
#define idea

using namespace std;

class Idea{
private:
    int id;
    string proposer, content;
    vector<string> keywords;
public:
    Idea(int i);
    Idea(int i, string pro, string con);
    void inputIdea();
    int getID(){ return id; };
    string getProposer(){ return proposer; };
    string getContent(){ return content; };
    vector<string> getKeyword();
    bool wordSearch(string word);
    friend ostream& operator<< (ostream& stream, const Idea& i);
    void printIdea(ostream& os)const;
};

bool Idea::wordSearch(string word){
     vector<string>::iterator it;
     for(it = keywords.begin(); it < keywords.end(); it++){
         if (word == *it){
             return true;
         }
     }

     if (content.find(word) != string::npos){
         return true;
     }

     return false;
 }

void Idea::printIdea(ostream& os)const{
     vector<string>::iterator it;

     os << "ID: " << id << endl;
     os << "Proposer:  " << proposer << endl;
     os << "keywords: ";
     for (it = keywords.begin(); it < keywords.end(); it++){ // error C2679
         os << *it << " ,";
     }
     os << endl << "content: " << content << endl;

 }

 ostream& operator<<(ostream& os, const Idea& i)
 {
     i.printIdea(os);
     return os;

 }

i find it odd because the iterator function works in a different section of the code.

 bool Idea::wordSearch(string word){
     vector<string>::iterator it;
     for(it = keywords.begin(); it < keywords.end(); it++){
         if (word == *it){
             return true;
         }
     }

     if (content.find(word) != string::npos){
         return true;
     }

     return false;
 }

I wish to print out the id, then the proposer, then the keywords, then the content.

Upvotes: 0

Views: 140

Answers (3)

Hatted Rooster
Hatted Rooster

Reputation: 36483

This is because printIdea is a const method. const methods aren't allowed to modify their members and thus keywords.begin() returns a vector<string>::const_iterator, not a normal one (vector<string>::iterator) . You should change the type of it :

vector<string>::iterator it;

to:

vector<string>::const_iterator it;

To have compatible types.

Or, if you have a compiler that has C++11 support you could just let the compiler figure it out for you:

auto it = keywords.begin()

Upvotes: 2

Rene
Rene

Reputation: 2466

You need to use a const_iterator since your method printIdea() is const.

And you should use != to compare the iterator against end().

Upvotes: 0

NathanOliver
NathanOliver

Reputation: 180630

printIdea is defined as

void Idea::printIdea(ostream& os)const

which means all non static members in that function are const qualified. Because of that keywords.begin() returns std::vector::const_iterator and not a std::vector::iterator. A std::vector::const_iterator is not assignable to a ``std::vector::iteratorwhich is howit` is declared so you get the error. You need to change

vector<string>::iterator it;

to

vector<string>::const_iterator it;

In order to get it to work.

Alternatively you can use a range based for loop and not even have to remember all of this like

for (auto& e : keywords)
{
     os << e << " ,";
}

Upvotes: 0

Related Questions