Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Why is this error? C++ stringstream instance?

Posted on 2012-03-13
2
Medium Priority
?
467 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 600 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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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 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.
Suggested Courses

618 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