Solved

Why is this error? C++ stringstream instance?

Posted on 2012-03-13
2
451 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
2 Comments
 
LVL 30

Accepted Solution

by:
Zoppo earned 150 total points
Comment Utility
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
Comment Utility
Great. Thank you very much.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

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…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now