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

Template, Inheritance and Operator Overloading

Hi folks

I'm having a scope resolution issue. The gnu compiler is trying to
use the "operator function" from derived class rather than from correct
one, the base class.

Expand|Select|Wrap|Line Numbers
  1. // VecBasis.h file
  2. template<class T, size_t numDim> class VecBasis {
  3. protected:
  4. valarray<T> arr;
  5.  
  6. public:
  7. VecBasis<T, numDim>() : arr(numDim) {}
  8. VecBasis<T, numDim>(const VecBasis<T, numDim>& v) : arr(numDim)
  9. {/**/}
  10. // ...
  11. VecBasis<T, numDim> operator * (T v) const {/**/}
  12. };
  13.  
  14. // Vec3.h file
  15. template<class T> class Vec3 : public VecBasis<T, 3> {
  16. public:
  17. Vec3<T> () : VecBasis<T, 3>() {}
  18.  
  19. T x() const { return VecBasis<T, 3>::arr[0]; } // is the scope
  20. operator really necessary here?
  21. // ...
  22. T operator * (const Vec3<T>& v) {/**/} // here is the problem
  23. };
  24.  
  25. // main.cpp file
  26. int main()
  27. {
  28. Vec3<float> v0;
  29. v0.operator*(2.0f); // error
  30. }
  31.  
When I try to compile the code, it says,
.../../src/main.cpp:17: error: no matching function for call to
'Vec3<float>::operator*(float)'
.../../src/math/Vec3.h:21: note: candidates are: T
Vec3<T>::operator*(const Vec3<T>&) [with T = float]
If I comment out the Vec3::operator*(), the compiler can guess the
right operator function (from base class).

Perhaps I could use the explicit keyword (just guessing) to prevent
implicit casting and the compiler would simply ignore the operator from
derived class and use the correct one, but this would give me much more
trouble. Any idea?

One more thing: is that scope resolution operator obligatory? the
compiler complaints if I don't use it.

Thanks :)

Feb 20 '06 #1
3 2067
da**********@gmail.com wrote:
I'm having a scope resolution issue. The gnu compiler is trying to
use the "operator function" from derived class rather than from
correct one, the base class.
The derived one _hides_ the base one. Period. If you want them in
the scope of the derived class _together_, use 'using'.
[...]


V
--
Please remove capital As from my address when replying by mail
Feb 20 '06 #2

Victor Bazarov wrote:
da**********@gmail.com wrote:
I'm having a scope resolution issue. The gnu compiler is trying to
use the "operator function" from derived class rather than from
correct one, the base class.


The derived one _hides_ the base one. Period. If you want them in
the scope of the derived class _together_, use 'using'.
[...]


V
--
Please remove capital As from my address when replying by mail


"Overload resolution is not applied across different class scopes" -
Stroustrup. I understand now. Thanks.

Feb 20 '06 #3
da**********@gmail.com wrote:
// Vec3.h file
template<class T> class Vec3 : public VecBasis<T, 3> {
public:
Vec3<T> () : VecBasis<T, 3>() {}

T x() const { return VecBasis<T, 3>::arr[0]; } // is the scope
operator really necessary here?
// ...
T operator * (const Vec3<T>& v) {/**/} // here is the problem
}; uble. Any idea?
One more thing: is that scope resolution operator obligatory? the
compiler complaints if I don't use it.


Yes. You have a dependent base class (you derive from a class dependent
on the template parameter T), and therefore non-qualified names are not
looked up in the base class. There is good reason for this with respect
to template specialization. Suppose it would look up the member, decide
on it's type, and shortly after you specialize the base class for this
specific template T... the type of the base member now changes and the
original assumption is wrong. (I hope i said that right, if you need the
exact wording please browse the standard on dependent base classes...)

You can either qualify the name, as you've done, or you can use the
"this->arr[0]" notation. This (some pun huh?) achieves the same thing.

--
Regards,

Ferdi Smit (M.Sc.)
Email: Fe********@cwi.nl
Room: C0.07 Phone: 4229
INS3 Visualization and 3D Interfaces
CWI Amsterdam, The Netherlands
Feb 20 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Fuzzyman | last post by:
I've been programming in python for a few months now - and returning to programming after a gap of about ten years I've really enjoyed learning python. I've just made my first forays into...
2
by: franklini | last post by:
hello people i. can anybody help me, i dont know what is wrong with this class. it has something to do with the me trying to override the input output stream. if i dont override it, it works fine....
5
by: Tony Johansson | last post by:
Hello experts! I have two class template below with names Array and CheckedArray. The class template CheckedArray is derived from the class template Array which is the base class This program...
4
by: hall | last post by:
Hi all. I have run into a problem of overloading a templatized operator>> by a specialized version of it. In short (complete code below), I have written a stream class, STR, which defines a...
6
by: Jules | last post by:
I have the following code which isn't working as I expect it to: --- #include <iostream> using namespace std; class mystring { public: mystring (const char * src) { /* ... */ }
6
by: apm | last post by:
Recently I have had to use a value type for a complex structure because I don't know how to override the = operator. Can the operator ever be overloaded? Or can inheritance be used with value types?
6
by: Massimo Soricetti | last post by:
Hello, recently I wrote a little class which has to wrap two different type of data, showing the same external interface. I used operator overloading, but the same result I could eventually...
0
by: erik.erikson | last post by:
I am getting a compiler error that I can't well explain or even understand the origin of (though I boiled it down close...). Below is a bare-bones example. What I am doing is defining the...
272
by: Peter Olcott | last post by:
http://groups.google.com/group/comp.lang.c++/msg/a9092f0f6c9bf13a I think that the operator() member function does not work correctly, does anyone else know how to make a template for making two...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.