473,224 Members | 1,291 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,224 software developers and data experts.

Why this code can't compile?

Hi, all
I got the following code from
http://builder.com.com/5100-6370_14-5079969.html,
but it can't compile, neither can I figure out how to fix it. Anyone
pls give me an
explanation?

1 #include <string>
2 #include <sstream>
3 #include <iostream>
4
5 class str_stream
6 {
7 public:
8 std::stringstream & underlying_stream() const
9 { return m_streamOut; }
10
11 operator std::string() const
12 {
13 return m_streamOut.str();
14 }
15
16 private:
17 mutable std::stringstream m_streamOut;
18 };
19
20 template<class T>
21 const str_stream & operator<< (const str_stream & out, const T
& value)
22 {
23 out.underlying_stream() << value;
24 return out;
25 }
26
27 int main()
28 {
29 int num = 48;
30 std::string str;
31
32 // str_stream s;
33 // str = s << "We have " << num << " words";
34 str = str_stream() << "We have " << num << " words";
35
36 std::cout << str << std::endl;
37
38 return 0;
39 }
40

I changed the code by omitting line 34 (which is the original version
at that site) and adding
line 32 and 33. It can work correctly. Since that I haven't been
programming with C++ for
rather a long time, I need your help thirstily.

Thanks in advance.

Feb 10 '06 #1
8 1585
David Lee wrote:
I got the following code from
http://builder.com.com/5100-6370_14-5079969.html,
but it can't compile, neither can I figure out how to fix it. Anyone
pls give me an
explanation?

1 #include <string>
Please do not include line numbers like this. They prevent us from
simply copying the code from the message into a text editor and
compiling it. If you needed to refer to any line by its number, just
add a comment to that line indicating what its number is.
2 #include <sstream>
3 #include <iostream>
4
5 class str_stream
6 {
7 public:
8 std::stringstream & underlying_stream() const
9 { return m_streamOut; }
10
11 operator std::string() const
12 {
13 return m_streamOut.str();
14 }
15
16 private:
17 mutable std::stringstream m_streamOut;
18 };
19
20 template<class T>
21 const str_stream & operator<< (const str_stream & out, const T
& value)
22 {
23 out.underlying_stream() << value;
24 return out;
25 }
26
27 int main()
28 {
29 int num = 48;
30 std::string str;
31
32 // str_stream s;
33 // str = s << "We have " << num << " words";
34 str = str_stream() << "We have " << num << " words";
35
36 std::cout << str << std::endl;
37
38 return 0;
39 }
40

I changed the code by omitting line 34 (which is the original version
at that site) and adding
line 32 and 33. It can work correctly. Since that I haven't been
programming with C++ for
rather a long time, I need your help thirstily.


I ran your program as is (after stripping all those line numbers)
and it compiled and ran and output "We have 48 words<newline>".
Is that what you expected? I guess I am not sure what your problem
is. The code is fine.

V
--
Please remove capital As from my address when replying by mail
Feb 10 '06 #2
Hi, Victor Bazarov
Sorry for the inconvenience with line numbers attached.

Yes I want that output. I'm working on Fedora Core 4 with g++ 4.0.0,
and the compiler compliant that:

string.cc: In copy constructor ‘std::basic_ios<char,
std::char_traits<char> >::basic_ios(const std::basic_ios<char,
std::char_traits<char> >&)’:
/usr/lib/gcc/i386-redhat-linux/4.0.0/../../../../include/c++/4.0.0/bits/ios_base.h:779:
error: ‘std::ios_base::ios_base(const std::ios_base&)’ is
private
string.cc:34: error: within this context
string.cc: In copy constructor ‘std::basic_stringbuf<char,
std::char_traits<char>, std::allocator<char> >::basic_stringbuf(const
std::basic_stringbuf<char, std::char_traits<char>, std::allocator<char>
&)’:

/usr/lib/gcc/i386-redhat-linux/4.0.0/../../../../include/c++/4.0.0/streambuf:772:
error: ‘std::basic_streambuf<_CharT,
_Traits>::basic_streambuf(const std::basic_streambuf<_CharT, _Traits>&)
[with _CharT = char, _Traits = std::char_traits<char>]’ is
private
string.cc:34: error: within this context

Feb 10 '06 #3
In article <11*********************@f14g2000cwb.googlegroups. com>,
"David Lee" <li*******@gmail.com> wrote:
Hi, all
I got the following code from
http://builder.com.com/5100-6370_14-5079969.html,
but it can't compile, neither can I figure out how to fix it. Anyone
pls give me an
explanation?


Why do you think it can't compile? Did you maybe get an error from your
compiler? If so, then can we see it?

Thanks.
--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Feb 10 '06 #4
David Lee wrote:
Hi, Victor Bazarov
Sorry for the inconvenience with line numbers attached.
No biggie. Just don't make a habit out of it :*)
Yes I want that output. I'm working on Fedora Core 4 with g++ 4.0.0,
and the compiler compliant that:

string.cc: In copy constructor ?~std::basic_ios<char,
std::char_traits<char> >::basic_ios(const std::basic_ios<char,
std::char_traits<char> >&)?T:
/usr/lib/gcc/i386-redhat-linux/4.0.0/../../../../include/c++/4.0.0/bits/ios_base.h:779:
error: ?~std::ios_base::ios_base(const std::ios_base&)?T is
private
string.cc:34: error: within this context
string.cc: In copy constructor ?~std::basic_stringbuf<char,
std::char_traits<char>, std::allocator<char> >::basic_stringbuf(const
std::basic_stringbuf<char, std::char_traits<char>,
std::allocator<char>
&)?T:

/usr/lib/gcc/i386-redhat-linux/4.0.0/../../../../include/c++/4.0.0/streambuf:772:
error: ?~std::basic_streambuf<_CharT,
_Traits>::basic_streambuf(const std::basic_streambuf<_CharT,
_Traits>&)
[with _CharT = char, _Traits = std::char_traits<char>]?T is
private
string.cc:34: error: within this context


Well, I don't know what to tell you. Both VC++ v8 and Comeau C++ (online)
compile the code without a complaint. Try 'gnu.g++.help' or GNU online
forums. Perhaps there is a bug in 4.0 or in the library that ships with it.

V
--
Please remove capital As from my address when replying by mail
Feb 10 '06 #5
David Lee wrote:
Hi, all
I got the following code from
http://builder.com.com/5100-6370_14-5079969.html,
but it can't compile, neither can I figure out how to fix it. Anyone
pls give me an
explanation?

1 #include <string>
2 #include <sstream>
3 #include <iostream>
4
5 class str_stream
6 {
7 public:
8 std::stringstream & underlying_stream() const
9 { return m_streamOut; }
10
11 operator std::string() const
12 {
13 return m_streamOut.str();
14 }
15
16 private:
17 mutable std::stringstream m_streamOut;
18 };
19
20 template<class T>
21 const str_stream & operator<< (const str_stream & out, const T
& value)
22 {
23 out.underlying_stream() << value;
24 return out;
25 }
26
27 int main()
28 {
29 int num = 48;
30 std::string str;
31
32 // str_stream s;
33 // str = s << "We have " << num << " words";
34 str = str_stream() << "We have " << num << " words";
In this line, you bind the temporary str_stream() to the non-const reference
parameter of operator<<( str_stream &, T const & ). This does not fly
because the standard forbids that.

35
36 std::cout << str << std::endl;
37
38 return 0;
39 }
40

Here is a fix:

#include <string>
#include <sstream>
#include <iostream>

class str_stream
{
public:

std::stringstream & underlying_stream() const
{ return m_streamOut; }

operator std::string() const
{
return m_streamOut.str();
}

str_stream & me ( void ) {
return ( *this );
}
private:
mutable std::stringstream m_streamOut;
};

template<class T>
const str_stream & operator<< (const str_stream & out, const T & value)
{
out.underlying_stream() << value;
return out;
}

int main() {
int num = 48;
std::string str;

str = str_stream().me() << "We have " << num << " words";

std::cout << str << std::endl;

return 0;
}
This compiles because it is legal to call a non-const member function of a
temporary object. The return value of me() is a reference, which can be
bound to the first parameter of operator<<(). That the reference just so
happens to refer to a temporary is immaterial.
Best

Kai-Uwe Bux
Feb 10 '06 #6
Kai-Uwe Bux wrote:
David Lee wrote:
Hi, all
I got the following code from
http://builder.com.com/5100-6370_14-5079969.html,
but it can't compile, neither can I figure out how to fix it. Anyone
pls give me an
explanation?

1 #include <string>
2 #include <sstream>
3 #include <iostream>
4
5 class str_stream
6 {
7 public:
8 std::stringstream & underlying_stream() const
9 { return m_streamOut; }
10
11 operator std::string() const
12 {
13 return m_streamOut.str();
14 }
15
16 private:
17 mutable std::stringstream m_streamOut;
18 };
19
20 template<class T>
21 const str_stream & operator<< (const str_stream & out, const T
& value)
22 {
23 out.underlying_stream() << value;
24 return out;
25 }
26
27 int main()
28 {
29 int num = 48;
30 std::string str;
31
32 // str_stream s;
33 // str = s << "We have " << num << " words";
34 str = str_stream() << "We have " << num << " words";


In this line, you bind the temporary str_stream() to the non-const
reference parameter of operator<<( str_stream &, T const & ). This does
not fly because the standard forbids that.


Oops, sorry: I missed the const in operator<<(). BTW: that const is ill
advises logic and only serves to make the compiler shut up. The operation
is non-const!

[snip]
Best

Kai-Uwe Bux
Feb 10 '06 #7
Thanks a lot for kai-Uwe's explanation, and Bazarov, of course.
Thank you all. :)

Feb 10 '06 #8
David Lee wrote:
Hi, all
I got the following code from
http://builder.com.com/5100-6370_14-5079969.html,
but it can't compile, neither can I figure out how to fix it. Anyone
pls give me an
explanation?

1 #include <string>
2 #include <sstream>
3 #include <iostream>
4
5 class str_stream
6 {
7 public:
8 std::stringstream & underlying_stream() const
9 { return m_streamOut; }
10
11 operator std::string() const
12 {
13 return m_streamOut.str();
14 }
15
16 private:
17 mutable std::stringstream m_streamOut;
18 };
19
20 template<class T>
21 const str_stream & operator<< (const str_stream & out, const T
& value)
22 {
23 out.underlying_stream() << value;
24 return out;
25 }
26
27 int main()
28 {
29 int num = 48;
30 std::string str;
31
32 // str_stream s;
33 // str = s << "We have " << num << " words";
34 str = str_stream() << "We have " << num << " words";
35
36 std::cout << str << std::endl;
37
38 return 0;
39 }
40


I think I understand now what is going on. I stripped all of the example
down to:

#include <sstream>

struct str_stream {
mutable std::stringstream m_streamOut;
};

void dump ( str_stream const & out, int value ) {
out.m_streamOut << value;
}

int main() {
dump( str_stream(), 2 );
}
What happens is that according to clause [8.5.3/5] and implementation is
free to choose the mechanism by which to initialize const references from
things that are not lvalues. It may choose to

(a) directly bind to an rvalue, or
(b) copy construct a new temporary from the passed object and bind that.

Your implementation seems to opt for (b) which implies that you need a copy
constructor. However, stringstreams do not have those and you get a load of
error messages.

Now, what I do not understand is why this compiles on Comeau. The standard
mandates that the copy constructor be callable in any case. So, maybe my
analysis is mistaken.
Best

Kai-Uwe Bux
Feb 10 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: Hung Jung Lu | last post by:
Hi, I know people have talked about it before, but I am still really confused from reading the old messages. When I talk about code blocks, I am not talking about Lisp/Ruby/Perl, so I am not...
242
by: James Cameron | last post by:
Hi I'm developing a program and the client is worried about future reuse of the code. Say 5, 10, 15 years down the road. This will be a major factor in selecting the development language. Any...
8
by: Kai-Uwe Bux | last post by:
Hi folks, in another thread there is some discussion about cloning smart pointers. So I started thinking about it and stumbled upon a rather simple generic implementation. However, it uses...
13
by: AFKAFB | last post by:
Hi Sometimes when i edit previously saved VBA code e.g. To update field names etc the revised code does not work. Even if i copy and paste in a previous version it does not work. The only...
16
by: Fronsac | last post by:
Hi, I've been asked in a job interview how to make C code look like C++ code, and honestly I didn't know what to answer because I have never really done a lot of C. Now, I've been searching around...
13
by: R Reyes | last post by:
is there a way to avoid the validateRequest error page in my code behind file? i can't seem to find a way to handle the error w/o an error code or exception being thrown... i am NOT looking for...
3
by: farseer | last post by:
i am getting "error C2057: expected constant expression" with the following code: ifstream f( argv ); f.seekg( 0, ios::end ); const long fSize = f.tellg(); f.close(); char content;
6
by: YASER WAHEED | last post by:
I have found few days ago that .NET dll can be reversed to view all source code. I have used Dotfuscator but it only renames variables etc. I want to hide code. Please reply with some...
10
by: Toms hilidhe | last post by:
I'd post this on a gcc newsgroup but I'd be more productive talking to the wall. Anyway, let's say someone throws some source code at you for a particular program and says, "Just compile it, it...
14
by: anchitgood | last post by:
Hey, I have developed the following code and it is executing well on windows. But I don't have LINUX installed on my system. I would be grateful to you guys if you check whether this code is...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.