1

I have a class that extends ostringstream class.

Class A: public ostringstream
{

}

I want to read data of specified size and a specific offset from that object. So trying:

A a_;
a_ << data1 << data2;
string datax(a_[offset], size);

But getting a compile error at string datax statement.

error: no match for operator[] in ... 

How can I copy data from object a_ for a specified offset and size? I dont want to remove data from the object a_.

NOTE: The class was designed by someone else and cannot be modified.

Vite Falcon
  • 6,575
  • 3
  • 30
  • 48
Romonov
  • 8,145
  • 14
  • 43
  • 55

3 Answers3

1

First of all, nothing in the standard guarantees you that deriving from std::ostringstream will work. You'd be better not doing it at all.

Second, there is no operator [] for std::ostringstream so why should there be one for A?

Finally, you can call str() on a_ and get an std::string with the contents of the buffer. I'm not sure if that would help you, what you want to do is not entirely clear.

K-ballo
  • 80,396
  • 20
  • 159
  • 169
1

I believe with the code you have you could do:

a_ << data1 << data2;
string datax(&a_.str()[offset], size);

which looks a bit ugly to me. Why not just use plain std::stringstream instead? (unless you have a strong reason to inherit from std::ostringstream.

Vite Falcon
  • 6,575
  • 3
  • 30
  • 48
  • Thank you. I am using Class A that has been cleardy designed by someone else, which I cant change. So I am having to do this. – Romonov May 22 '12 at 18:34
0

You need to call to str() method:

ostringstream oss (ostringstream::out);
oss << "abcdef";
string s = oss.str();
cout << s.substr(1, 3) << endl;

This example would output "bcd".

betabandido
  • 18,946
  • 11
  • 62
  • 76