c++ - I am stuck with creating an output member function -


i stuck on output member function of class. have no idea how create , couting not seem work. other advice great. in advance

here's code:

#include <iostream> #include <string> #include <vector>   using namespace std;  class stringset {     public:     stringset(vector<string> str);     void add(string s);     void remove(int i);     void clear();     int length();     void output(ostream& outs);     private:     vector<string> strarr; }; stringset::stringset(vector<string> str) {     for(int k =0;k<str.size();k++)     {         strarr.push_back(str[k]);     } } void stringset::add(string s) {     strarr.push_back(s); } void stringset::remove(int i) {     strarr.erase (strarr.begin()+(i-1)); } void stringset::clear() {     strarr.erase(strarr.begin(),strarr.end()); } int stringset::length() {     return strarr.size(); } void stringset::output() {  }  int main() {     vector<string> vstr;     string s;     for(int i=0;i<10;i++)     {         cout<<"enter string: ";         cin>>s;         vstr.push_back(s);      }     stringset* strset=new stringset(vstr);     strset.length();     strset.add("hello");     strset.remove(3);     strset.empty();     return 0; } 

ok, should begin solving errors in code:

  • you use pointer stringset , after trying access member-functions . operator instead of ->. anyway, need allocated object dynamically ?

    stringset strset(vstr); // no need allocated dynamically object 
  • after that, calling empty() method not exist...

  • also if stay on dynamic allocation, don't forget deallocated memory :

    stringset* strset = new stringset(vstr); // ... delete strset;  // <- important 
  • finally, think function output should write in stream content of vector, can way :

    #include <algorithm> // std::copy #include <iterator>  // std::ostream_iterator  void stringset::output( ostream& outs ) //                      ^^^^^^^^^^^^^ don't forget arguments during definition {     std::copy(strarr.begin(), strarr.end(), std::ostream_iterator<string>(outs, "\n")); } 

here live example of code fixed.

i suggest understan how class works : http://www.cplusplus.com/doc/tutorial/classes/


Comments

Popular posts from this blog

java - activate/deactivate sonar maven plugin by profile? -

python - TypeError: can only concatenate tuple (not "float") to tuple -

java - What is the difference between String. and String.this. ? -