This is the mail archive of the gcc-bugs@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]

c++/2117: gcc3.0 cannot return references from conversion operators



>Number:         2117
>Category:       c++
>Synopsis:       gcc3.0 cannot return references from conversion operators
>Confidential:   no
>Severity:       critical
>Priority:       high
>Responsible:    unassigned
>State:          open
>Class:          rejects-legal
>Submitter-Id:   net
>Arrival-Date:   Tue Feb 27 04:16:02 PST 2001
>Closed-Date:
>Last-Modified:
>Originator:     
>Release:        3.1 20010226 (experimental)
>Organization:
Imperial College
>Environment:
System: Linux noddy 2.4.1 #3 Wed Jan 31 09:40:49 GMT 2001 i686 unknown
Architecture: i686
host: i686-pc-linux-gnu
build: i686-pc-linux-gnu
target: i686-pc-linux-gnu
configured with: /mirror/gcc-3.0/dev/gcc/configure 
>Description:
	This version of gcc is unable to return a reference from a conversion
    operator in C++ of the form:

class error
{
.
.
.
    operator Ostream&()
    {
		return os_;
	}

.
.
};

The error the compiler gives is:

gcc3Test.C: In function `int main()':
gcc3Test.C:89: cannot allocate an object of type `Ostream'
gcc3Test.C:89:   because the following virtual functions are abstract:
gcc3Test.C:26:  virtual bool Ostream::write(const char*) 

It appears that gcc is attempting to call a copy contructor to return the Ostream& which is not neccessary
and is not possible if the Ostream class is an abstract base class.  However, gcc2.95.2 and all the other
workstation compilers are happy with this code.
>How-To-Repeat:
#include <stream.h>

class Ostream
{

public:

    // Constructors

        //- Set stream status
        Ostream()
        {}


    // Destructor

        virtual ~Ostream()
        {}


    // Member functions

        // Write functions
            
            //- Write character string
            virtual bool write(const char*) = 0;
};


class OSstream
:
	public Ostream
{

public:

    // Constructors
 
        //- Set stream status
        OSstream()
        {}
 
 
    // Member functions
 
        // Write functions
 
            //- Write character string
            bool write(const char* str)    
			{
				cout << str << endl;
			}
};


Ostream& operator<<(Ostream& os, const char* str)
{
	os.write(str);
	return os;
}


class error
{
    OSstream os_;

public:

    // Constructors

        error()
        {}


    // Member functions

        //- Convert to Ostream
        operator Ostream&()
		{
			return os_;
		}
};


int main()
{
	error info;

	info<< "hello";

    return 0;
}


Compile:

g++ gcc3Test.C

which produces

gcc3Test.C: In function `int main()':
gcc3Test.C:89: cannot allocate an object of type `Ostream'
gcc3Test.C:89:   because the following virtual functions are abstract:
gcc3Test.C:26:  virtual bool Ostream::write(const char*)

>Fix:
	Use gcc2.95.2
>Release-Note:
>Audit-Trail:
>Unformatted:


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