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

'virtual' can only be used....

P: n/a
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

Jul 22 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
cppaddict wrote:
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
Jul 22 '05 #2

P: n/a
>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
Jul 22 '05 #3

P: n/a
cppaddict wrote:
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

Jul 22 '05 #4

P: n/a
cppaddict wrote:
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?


Yes.

References cannot be changed (according to their nature) and therefore
cannot be cv-qualified. The standard explicitly prohibits qualifiers
for references.
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?


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
Jul 22 '05 #5

P: n/a
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
Jul 22 '05 #6

P: n/a
JKop posted:
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


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

-JKop
Jul 22 '05 #7

P: n/a
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

Jul 22 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.