Original Post
I get an error when I try to compile this.. I know its something to do about the const strLst object.. but I dont get why, cause the member functions called arent realy changing anything in the object?
#include "stringl.h"
void StringList::push_back(const string &str)
{
list.push_back(str);
}
string StringList::getString(unsigned i)
{
return (list);
}
void StringList::addStrTokenz(const string &str,const string &delim)
{
//skips any delimiters at start of string(sets startPos to start of the first single word string)
string::size_type startPos = str.find_first_not_of(delim, 0);
//finding first delimiter(sets endPos to end of the first single word string)
string::size_type endPos = str.find_first_of(delim, startPos);
//checks if any of the find_ algorithms failed
while(string::npos != endPos && string::npos != startPos)
{
//putting string at the end of the list vector
list.push_back(str.substr(startPos, endPos - startPos));
//skipping consecutive delimiters (sets startPos to the start of the next single word string)
startPos = str.find_first_not_of(delim, endPos);
//finding the next delimiter (sets endPos to the end of the next single word string )
endPos = str.find_first_of(delim, startPos);
}
}
StringList StringList::strOfSizeBelow(StringList &strLst, const unsigned size)
{
StringList returnList;
string tmpStr;
for(unsigned i = 0; i < strLst.size(); i++)
{
tmpStr = strLst.getString(i);
if(tmpStr.size() < size)
returnList.push_back(tmpStr);
}
return returnList;
}
StringList StringList::noDuplicate(StringList &strLst)
{
StringList returnList;
string tmpStr;
for(unsigned i = 0; i < strLst.size(); i++)
{
tmpStr = strLst.getString(i);
if(!returnList.find(tmpStr))
returnList.push_back(tmpStr);
}
return returnList;
}
bool StringList::find(string &str)
{
bool flag = false;
string tmpStr;
for(unsigned i = 0; i < list.size(); i++)
{
tmpStr = list;
if(str == tmpStr)
flag = true;
}
return flag;
}
float StringList::averageLength()
{
unsigned sum = 0;
string tmpStr;
for(unsigned i = 0; i < list.size(); i++)
{
tmpStr = list;
sum += tmpStr.size();
}
return (float)sum / size();
}
ostream & operator <<( ostream & os,const StringList & strLst )
{
unsigned count = 1;
for(unsigned i = 0; i < strLst.size(); i++)
{
if (count == 1)
{
count++;
os << strLst.getString(i);
}
else
{
count = 1;
os << strLst.getString(i) << '\n';
}
}
return os;
}
J:\ict209\assignment1\STRINGL.CPP||In function `std::ostream& operator<<(std::ostream&, const StringList&)':|
J:\ict209\assignment1\STRINGL.CPP|91|error: passing `const StringList' as `this' argument of `unsigned int StringList::size()' discards qualifiers|
J:\ict209\assignment1\STRINGL.CPP|96|error: passing `const StringList' as `this' argument of `std::string StringList::getString(unsigned int)' discards qualifiers|
J:\ict209\assignment1\STRINGL.CPP|101|error: passing `const StringList' as `this' argument of `std::string StringList::getString(unsigned int)' discards qualifiers|
||=== Build finished: 3 errors, 0 warnings ===|