Solved

Why is this error? C++ stringstream instance?

Posted on 2012-03-13
2
459 Views
Last Modified: 2012-03-13
I am creating this wrapper class:
When I compile this it keep on telling me that there is an error in the mDataStream object definition in the .h file. I do not understand why. I have seen several examples on th enet and I do not see a problem. Can someobe tell me why?

//BinaryStream.h

#ifndef BinaryStream_EXISTS
#define BinaryStream_EXISTS

#include <string>
#include <sstream>

class BinaryStream;

namespace Wrsp
{
    class BinaryStream
    {
        public:
          void read(char *, int);
          void write(const char*, int);
 
          void operator<<(const char* );
          void operator>>(char* );  
        private:
           std::stringstream mDataStream(std::stringstream::in | std::stringstream::out | std::stringstream::binary);
    };
}


//BinaryStream.cpp
#include "BinaryStream.h"

namespace Wrsp
{
    void BinaryStream::read(char* buffer, int buffersize)
    {
        mDataStream.read(buffer, buffersize);
    }


    void BinaryStream::write(const char* buffer, int buffersize)
    {
        mDataStream.write(buffer, buffersize);
    }


    void BinaryStream::operator<<(const char* inChar )
    {
        write(inChar, 1);
    }
     

    void BinaryStream::operator>>(char* inChar)
    {  
        read(inChar, 1);
    }  
}
0
Comment
Question by:prain
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 31

Accepted Solution

by:
Zoppo earned 150 total points
ID: 37714791
Hi prain,

the problem is simply that it's not possible to initialize (none-const and none-static) members within the class declaration. You have to move it to the constructor, i.e.:
namespace Wrsp
{
    class BinaryStream
    {
        public:
          BinaryStream();

          void read(char *, int);
          void write(const char*, int);
 
          void operator<<(const char* );
          void operator>>(char* );  
        private:
           std::stringstream mDataStream;
    };
}

namespace Wrsp
{
    BinaryStream::BinaryStream()
    : mDataStream( std::stringstream::in | std::stringstream::out | std::stringstream::binary )
    {
    }
...
}

Open in new window

Hope that helps,

ZOPPO
0
 

Author Closing Comment

by:prain
ID: 37714935
Great. Thank you very much.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question