Solved

Why is this error? C++ stringstream instance?

Posted on 2012-03-13
2
457 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 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

Suggested Solutions

Title # Comments Views Activity
c++ how to tell if the progra is ctl or mfc atl ect 6 84
C++ Language error 28 245
I could not build boost code, 10 94
I could not set window to top 4 20
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

840 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