This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: GCC compile problem ...


Hi!

On Friday 27 September 2002 09:09, gcc@wiesinger.com wrote:
> Hello!
>
> I have the following compile problem with gcc 3.0.4.
>
> Borland C++ 5.5 and Microsoft Visual C++ 6.0 works well.
>
> #include <string>
> #include <iostream>
>
> using namespace std;
>
> class Test
> {
> public:
>         string getString() { return "hallo"; }
> };
>
> int main(int argc, char* argv)
> {
>         Test test;
>
>         string& s = test.getString(); // Error here

This is not ok, because you try to initialize a non-const reference with a 
temporary string object.

>         // works well: string s = test.getString();

This is ok, because of the assignment operator looks like:
operator=( const std::string&)

>         cout << s << "\n";

This is also ok, because the overloaded shift operator takes a constant 
reference.

>         // gcc --version: 3.0.4
> }
>
> main.cpp: In function `int main (int, char *)':
> main.cpp:17: initialization of non-const reference type `class string
> &'
> main.cpp:17: from rvalue of type `basic_string<char,
> string_char_traits<char>, __default_alloc_template<true, 0> >'
> make[1]: *** [main.o] Error 1
>
> Why aren't there references allowed here with gcc? Is this not Ansi C++
> conform?
>
> Ciao,
> Gerhard


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]