简体   繁体   中英

I am stuck with creating an output member function

I am stuck on the output member function of the class. I have no idea how to create it and just simply couting it does not seem to work. also any other advice would be great. thanks in advance

here's the 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 a 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, you should begin by solving some errors in your code:

  • You use a pointer to StringSet and after that you are trying to access the member-functions with the . operator instead of the -> . Anyway, do you really need to allocated your object dynamically ?

     StringSet strset(vstr); // No need to allocated dynamically your object 
  • After that, you are calling an empty() method which does not exist...

  • Also if you stay on dynamic allocation, don't forget to deallocated your memory :

     StringSet* strset = new StringSet(vstr); // ... delete strset; // <- Important 
  • Finally, I think that your function output should write in the stream the content of your vector, you can do it that way :

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

HERE is a live example of your code fixed.

I would suggest you to understan how class works : http://www.cplusplus.com/doc/tutorial/classes/

If your output function is going to print the state of the StringSet object, you may implement is like this:

#include<iterator>  //std::ostream_iterator
#include<algorithm>  //std::copy

void StringSet::output(ostream& outs)
{
    std::copy(starr.begin(), starr.end(), std::ostream_iterator<string>(outs, "\n"));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM