Solved

Why is this error? C++ stringstream instance?

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
why "." vs "->" 23 119
object modeling - load and save functions 5 60
Problem with SqlConnection 4 160
Safe conversion? 4 58
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

895 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

18 Experts available now in Live!

Get 1:1 Help Now