473,382 Members | 1,423 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,382 software developers and data experts.

'virtual' can only be used....

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
7 1452
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
>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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
by: heted7 | last post by:
Hi, Most of the books on C++ say something like this: "A virtual destructor should be defined if the class contains at least one virtual member function." My question is: why is it only for...
14
by: JPRoot | last post by:
Hi I use the following syntax to have events inherited from base to child classes which works nicely (virtual and override keyword on events). But I am wondering if it is a "supported" way of using...
7
by: eric | last post by:
hello i'm confused by an example in the book "Effective C++ Third Edition" and would be grateful for some help. here's the code: class Person { public: Person(); virtual ~Person(); // see...
14
by: v4vijayakumar | last post by:
Why we need "virtual private member functions"? Why it is not an (compile time) error?
17
by: Jess | last post by:
Hello, If I have a class that has virtual but non-pure declarations, like class A{ virtual void f(); }; Then is A still an abstract class? Do I have to have "virtual void f() = 0;"...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.