'virtual' can only be used....

C

cppaddict

Hi,

My compiler is erroring on the following line of a RectangleUser
class:

virtual Rectangle& const getNameDisplayRect(int) const=0;

The error is:

'virtual' can only be used with non-template member functions

The Rectangle class contains no templates and neither does
RectangleUser. I have already double-checked that Rectangle.h is
included in the file RectangleUser.h.

Does anyone know what might be causing this?

Thanks,
cpp
 
V

Victor Bazarov

cppaddict said:
My compiler is erroring on the following line of a RectangleUser
class:

virtual Rectangle& const getNameDisplayRect(int) const=0;

Write it as

virtual Rectangle const& getNameDisplayRect(int) const = 0;

(your first 'const' is in the wrong place)
The error is:

'virtual' can only be used with non-template member functions

The Rectangle class contains no templates and neither does
RectangleUser. I have already double-checked that Rectangle.h is
included in the file RectangleUser.h.

Does anyone know what might be causing this?

I can only guess that the first 'const' being out of place freaks
the compiler out. Another guess is that 'Rectangle' is somehow
a macro that has some template thing in it, and you don't know of
it because the actual "Rectangle.h" file being included is not
the one you provided, but some system file from some other package
(happened to me once with a header named "data.h" -- very original
name, isn't it?)

Victor
 
C

cppaddict

Write it as
virtual Rectangle const& getNameDisplayRect(int) const = 0;

(your first 'const' is in the wrong place)

Victor,

Thanks for your response. Unfortunately, this change didn't help.

As a side note, are you sure the way I have it is wrong? I thought
that "Rectangle& const" means "A reference which cannot be changed
that refers to a rectangle" whereas "Rectangle const&" means "A
reference (which can be changed) that refers to a Rectangle which
cannot be changed". Thus both are legal. Am I wrong here?

Another guess is that 'Rectangle' is somehow
a macro that has some template thing in it, and you don't know of
it because the actual "Rectangle.h" file being included is not
the one you provided, but some system file from some other package
(happened to me once with a header named "data.h" -- very original
name, isn't it?)

This isn't the case, either. Rectangle.h is a file I wrote, it's an
extremely simple class with no macros.

Any other ideas?

thanks,
cpp
 
T

Thomas Matthews

cppaddict said:
Hi,

My compiler is erroring on the following line of a RectangleUser
class:

virtual Rectangle& const getNameDisplayRect(int) const=0;

The error is:

'virtual' can only be used with non-template member functions

The Rectangle class contains no templates and neither does
RectangleUser. I have already double-checked that Rectangle.h is
included in the file RectangleUser.h.

Does anyone know what might be causing this?

Thanks,
cpp

Please post the definitions of Rectangle and RectangleUser.


--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 
V

Victor Bazarov

cppaddict said:
Victor,

Thanks for your response. Unfortunately, this change didn't help.

As a side note, are you sure the way I have it is wrong? I thought
that "Rectangle& const" means "A reference which cannot be changed
that refers to a rectangle" whereas "Rectangle const&" means "A
reference (which can be changed) that refers to a Rectangle which
cannot be changed". Thus both are legal. Am I wrong here?

Yes.

References cannot be changed (according to their nature) and therefore
cannot be cv-qualified. The standard explicitly prohibits qualifiers
for references.
This isn't the case, either. Rectangle.h is a file I wrote, it's an
extremely simple class with no macros.

Any other ideas?

Begin eliminating stuff from your program until it compiles. Thus you
will isolate the offending part. Then create a small test program with
it and see if you got it right. If after that you still don't see the
reason why the compiler is complaining, post the small test program and
we can discuss it.

Victor
 
J

JKop

cppaddict posted:
Hi,

My compiler is erroring on the following line of a RectangleUser
class:

virtual Rectangle& const getNameDisplayRect(int) const=0;


Try:

const Rectangle& const getNameDisplayRect(int) const = 0;



-JKop
 
J

JKop

JKop posted:
cppaddict posted:



Try:

const Rectangle& const getNameDisplayRect(int) const = 0;



-JKop

TYPO TYPO TYPO


virtual const Rectangle& getNameDisplayRect(int) const = 0;



-JKop
 
C

cppaddict

References cannot be changed (according to their nature) and therefore
cannot be cv-qualified. The standard explicitly prohibits qualifiers
for references.

Yes, that makes sense. Thanks.
Begin eliminating stuff from your program until it compiles. Thus you
will isolate the offending part. Then create a small test program with
it and see if you got it right. If after that you still don't see the
reason why the compiler is complaining, post the small test program and
we can discuss it.

I did this and found that an include being included by one of the
includes I checked brought in the offending part. It was a windows
include and there is a Rectangle function which I think was creating
the problem. In any case, I renamed my class and it works now. Sorry
for the mistake, I thought I'd already ruled out such a conflict.

Thanks again for the guidance,
cpp
 
D

Daniel T.

cppaddict said:
Hi,

My compiler is erroring on the following line of a RectangleUser
class:

virtual Rectangle& const getNameDisplayRect(int) const=0;

The error is:

'virtual' can only be used with non-template member functions

The Rectangle class contains no templates and neither does
RectangleUser. I have already double-checked that Rectangle.h is
included in the file RectangleUser.h.

Does anyone know what might be causing this?

Try this:

class Foo { };

class Bar {
virtual const Foo& bar(int) const = 0;
};

My guess is that this *will* compile just fine.

My guess is, that your compiler is implicitly including some 'Rectangle'
class and then assuming that your code is referencing that class instead
of your Rectangle class.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top