1

Accelerated C++, exercise 14.5 involves reimplementing a split function (which turns text input into a vector of strings). One must use store input in a std::string - like class (Str) & use the split function to return a Vec<Str>, Vec being a std::vector - like container. The Str class manages a custom pointer (Ptr) to the underlying Vec<char> data.

The Str class provides a constructor Str(i,j), in Str.h below, which constructs a Ptr to the underlying Vec The problem arises when I try to create substrings by calling str(i,j) I've detailed in the code where the the issues arise. Here is a whittled-down version of the Str class (can post more code if needed):

Str.h

#include "Ptr.h"
#include "Vec.h"


class Str {
    friend std::istream& operator>>(std::istream&, Str&);
public:

    // define iterators

    typedef char* iterator;        
    typedef char* const_iterator;  

    iterator begin() { return data->begin(); }
    const_iterator begin() const { return data->begin(); }

    iterator end() { return data->end(); }
    const_iterator end() const { return data->end(); }

    //** This is where we define a constructor for `Ptr`s to substrings **

    template<class In> Str(In i, In j): data(new Vec<char>) {   
      std::copy(i, j, std::back_inserter(*data));
    }

private:
    // store a Ptr to a Vec
    Ptr< Vec<char> > data;
};

Split.h

Vec<Str> split(const Str& str) {
    typedef Str::const_iterator iter;
    Vec<Str> ret;

    iter i = str.begin();
    while (i != str.end()) {

        // ignore leading blanks
        i = find_if(i, str.end(), not_space);

        // find end of next word
        iter j = find_if(i, str.end(), space);

        // copy the characters in `[i,' `j)'
        if (i != str.end()) 
            ret.push_back(**substring**);        // Need to create substrings here
                                                 // call to str(i,j) gives error, detailed below
        i = j;
    }

    return ret;
}

My first thought was to use this constructor to create (pointers to) the required substrings. Calling str(i,j) here gives the error message

type 'const Str' does not provide a call operator
  1. It appears as if one cannot simply call str(i,j) here. Why not?
  2. Could a solution be to write a Str member function which is similar to substr?
Trojan
  • 2,256
  • 28
  • 40
Octave1
  • 525
  • 4
  • 19
  • 2
    You probably want `Str(i,j)` (use the type, not the object). That would construct a brand new string out of two iterators. – n. m. could be an AI Aug 10 '13 at 14:39
  • `type 'const Str' does not provide a call operator` . It means you have not implemented `operator (Str::const_iterator, Str::const_iterator)` as the member function. – Mahesh Aug 10 '13 at 14:39
  • Thanks @n.m, that was the mistake, couldn't see the wood for the trees. If you write an answer I can accept it. – Octave1 Aug 10 '13 at 14:56
  • @Mahesh, the member function was implemented. The problem was that I was calling str(i,j) instead of Str(i,j), i.e. using the object & not the type, as n.m pointed out. – Octave1 Aug 10 '13 at 14:59
  • As alternative to n.m.'s, I'd suggest a `substr()` memberfunction, similar to that of `std::string`. – Ulrich Eckhardt Aug 10 '13 at 16:01
  • possible duplicate of [Splitting a string in C++](http://stackoverflow.com/questions/236129/splitting-a-string-in-c) – Manu343726 Aug 10 '13 at 16:55

0 Answers0