By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,002 Members | 1,269 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,002 IT Pros & Developers. It's quick & easy.

namespaces conflict?

P: n/a
Take a look at the following code:

std::istream& operator>>(std::istream& in, char* str) {
int i=0;
for (char c; str[i] && in.get(c) && c==str[i]; ++i)
; //get consumes the characters from the stream
if (str[i]) {
in.setstate(std::ios_base::failbit);
std::cerr<<"problem with consuming"<<str; //debugging code
}
return in;
};

namespace SimpleSketch {

istream& operator >>(std::istream& is, SimpleGraph& graph) {
is>>"ImgWd = "; //line A
/* .... */
}
}

This code crashes on line A. But if I put the definition of the
operator >> inside the namespace SimpleSketch, it works.
Unfortunately the code is in another file, so that makes things highly
inconvenient. How can I make the caller code work without modifying or
copying the source of the operator.

I am programming on MSVC++.NET

Cheers,
Aleks D.

P.S. this relates to a post from a couple of months ago.
Jul 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a

"Aleks D" <in******@gmail.com> wrote in message
news:82**************************@posting.google.c om...
Take a look at the following code:

std::istream& operator>>(std::istream& in, char* str) {
int i=0;
for (char c; str[i] && in.get(c) && c==str[i]; ++i)
; //get consumes the characters from the stream
if (str[i]) {
in.setstate(std::ios_base::failbit);
std::cerr<<"problem with consuming"<<str; //debugging code
}
return in;
};

namespace SimpleSketch {

istream& operator >>(std::istream& is, SimpleGraph& graph) {
is>>"ImgWd = "; //line A
This is an attempt to modify a string literal.
/* .... */
}
}

This code crashes on line A.
I'm not surprised.

But if I put the definition of the
operator >> inside the namespace SimpleSketch, it works.
It's not required to.
Unfortunately the code is in another file, so that makes things highly
inconvenient. How can I make the caller code work without modifying or
copying the source of the operator.


Do not do things that produce undefined behavior.

-Mike
Jul 22 '05 #2

P: n/a
Aleks D wrote in news:82**************************@posting.google.c om in
comp.lang.c++:
Take a look at the following code:

std::istream& operator>>(std::istream& in, char* str) {
You don't modify the str argument so make it char const *.
int i=0;
for (char c; str[i] && in.get(c) && c==str[i]; ++i)
; //get consumes the characters from the stream
if (str[i]) {
in.setstate(std::ios_base::failbit);
std::cerr<<"problem with consuming"<<str; //debugging code
}
return in;
};

namespace SimpleSketch {

istream& operator >>(std::istream& is, SimpleGraph& graph) {
is>>"ImgWd = "; //line A
/* .... */
}
}

This code crashes on line A.
What's the crash

....

Ok got it, you're using the compiler in a non conforming mode
and its treating "ImgWd = " as a char *, it should be a const.

Then the compiler is calling std::operator >>( ... , char * ) which
overwrites the constant string and you get a crash, this is conforming
behaviour BTW.
But if I put the definition of the
operator >> inside the namespace SimpleSketch, it works.
Unfortunately the code is in another file, so that makes things highly
inconvenient. How can I make the caller code work without modifying or
copying the source of the operator.

I am programming on MSVC++.NET


#include <iostream>
#include <sstream>

std::istream& operator>>(std::istream& in, char const * str)
{
int i=0;
for (char c; str[i] && in.get(c) && c==str[i]; ++i)
; //get consumes the characters from the stream
if (str[i]) {
in.setstate(std::ios_base::failbit);
std::cerr<<"problem with consuming"<<str; //debugging code
}
return in;
};
int main()
{
std::istringstream iss( "Key = value" );
iss >> "Key = ";

char buf[ 10 ];
if ( !(iss >> buf) )
{
std::cout << "Arghhhhhhhh........\n";
}
else
{
std::cout << "[" << buf << "]\n";
}
}

The above works but I don't recomend it, be more explicit instead:

struct scanable
{
char const *str;
scanable( char const *s ) : str( s ) {}
};

std::istream operator >> ( std::istream &is, scanable const &scan )
{
// same as above but with scan.str substituted for str.
}

HTH.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.