473,405 Members | 2,349 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,405 software developers and data experts.

Basic problem with Inheritance

Hi there,

i am implementing a Point and Vector class and found that they have some
things in common. Therefore i introduced a common parent class called
ndimobj, that hosts an array of n values, offers some constructors and
the following operator

template<size_t n, typename T>
class ndimobj
{
<SNIP>
public:
ndimobj<n, T>& operator= (const T s);
};

ndimobj is templated by the type of the elements that live in a given "n
dimensional space" (doubles, polynomials, etc...) and the dimension n.

Point is now derived from ndimobj like this

template<size_t n=3, typename T=float>
class Point : public ndimobj<n, T>
{
public:
double distance(Point<n,Tp)
{ <snip}
};

If i do (in a test) program

Point <4, doubleq;
q=3.0;

my compiler (g++) complains:

test.cc: 49: error no match for 'operator=' in 'q=3.0e+0'
Point.hpp: 22: note: candidates are: Point<4u, double>& Point<4u,
double>::operator=(const Point<4u, double>&)

Why is operator= not known. and why does g++ find this unsuitable
candidate?!
Please let me know if didn't give sufficiently information and i'll post
the complete interface of ndimobj and Point if it helps.

Thanks in advance

matthias
Aug 16 '07 #1
8 1377
On 2007-08-16 11:55, Matthias Pfeifer wrote:
Hi there,

i am implementing a Point and Vector class and found that they have some
things in common. Therefore i introduced a common parent class called
ndimobj, that hosts an array of n values, offers some constructors and
the following operator

template<size_t n, typename T>
class ndimobj
{
<SNIP>
public:
ndimobj<n, T>& operator= (const T s);
};

ndimobj is templated by the type of the elements that live in a given "n
dimensional space" (doubles, polynomials, etc...) and the dimension n.

Point is now derived from ndimobj like this

template<size_t n=3, typename T=float>
class Point : public ndimobj<n, T>
{
public:
double distance(Point<n,Tp)
{ <snip}
};

If i do (in a test) program

Point <4, doubleq;
q=3.0;

my compiler (g++) complains:

test.cc: 49: error no match for 'operator=' in 'q=3.0e+0'
Point.hpp: 22: note: candidates are: Point<4u, double>& Point<4u,
double>::operator=(const Point<4u, double>&)

Why is operator= not known. and why does g++ find this unsuitable
candidate?!
Please let me know if didn't give sufficiently information and i'll post
the complete interface of ndimobj and Point if it helps.
Because you have not defined the = operator, what exactly do you expect
q=3.0; to do? If this was a point in the mathematical sense what would
that statement mean? Nothing as far as I know, you can't assign a scalar
to a vector (an a point is a vector as far as math is concerned).

The candidate found by gcc is used like this:

Point <4, doublep;
Point <4, doubleq;
p = q;

this = operator is automatically generated by the compiler for your
convenience.

--
Erik Wikström
Aug 16 '07 #2
Because you have not defined the = operator,
my ndimobj class has "operator= (const T s)" shouldn't class Point have
that inhereted?

template<size_t n, typename T>
class ndimobj
{
<SNIP>
public:
ndimobj<n, T>& operator= (const T s);
};

matt
Aug 16 '07 #3
LR
Matthias Pfeifer wrote:
>Because you have not defined the = operator,

my ndimobj class has "operator= (const T s)" shouldn't class Point have
that inhereted?

template<size_t n, typename T>
class ndimobj
{
<SNIP>
public:
ndimobj<n, T>& operator= (const T s);
};

The compiler will generate a Point &operator=(const Point &).
Consider this code:

class A {
public:
A &x(const int x) {
return *this;
}
};

class B : public A {
public:
B &x(const B &x) {
return *this;
}
};

int main() {
B q;
B p;
q.x(1); // error
q.x(p);
}

LR
Aug 16 '07 #4
On 2007-08-16 13:31, Matthias Pfeifer wrote:
>Because you have not defined the = operator,

my ndimobj class has "operator= (const T s)" shouldn't class Point have
that inhereted?

template<size_t n, typename T>
class ndimobj
{
<SNIP>
public:
ndimobj<n, T>& operator= (const T s);
};
Sorry, missed that. No it will not inherit that, you should get the same
error when trying to compile this:

struct A {
A& operator=(int i_);
};

struct B : public A {
};

int main() {
B b;
b = 1;
}

--
Erik Wikström
Aug 16 '07 #5
Hi!

Matthias Pfeifer schrieb:
Why is operator= not known.
There is something called "hiding" base class members. This is what is
happening here. Because the Point class has the automatically generated
"operator = (Point const&)" for self assignment, the base class
"operator =" is hidden. You can make it visible again by a "using"
declaration:

//in class Point
using ndimobj<n, T>::operator =;

HTH,
Frank
Aug 16 '07 #6
Frank Birbacher schrieb:
Hi!

Matthias Pfeifer schrieb:
>Why is operator= not known.

There is something called "hiding" base class members. This is what is
happening here. Because the Point class has the automatically generated
"operator = (Point const&)" for self assignment, the base class
"operator =" is hidden. You can make it visible again by a "using"
declaration:

//in class Point
using ndimobj<n, T>::operator =;

HTH,
Frank
thank you all for your answers. Using
using ndimobj<n, T>::operator =;
my compiler is satisfied - i am however not. I know about copy
constructors, which should be the automagically generated "Point<n,
T>::operator= (Point const&)". I am confused, because "my" operator is
"ndimobj<n, T>::operator= (const T)". Where i like to point that he has
a completely different argument (not const Point&, but const T). Please.
Why is the operator hidden?

sincerely
Matthias
Aug 16 '07 #7

LR <lr***@superlink.netwrote in message...
>
class A {
public:
A &x(const int x) {
return *this;
}
};

class B : public A {
public:
B &x(const B &x) {
return *this;
}
};

int main() {
B q;
B p;
q.x(1); // error
q.x(p);
}

LR
Thanks for the spaces vs. tabs. Looks like it should now (indented). <G>

--
Bob R
POVrookie
Aug 16 '07 #8
Hi!

Matthias schrieb:
my compiler is satisfied - i am however not. I know about copy
constructors, which should be the automagically generated "Point<n,
T>::operator= (Point const&)".
In fact, this is not a "constructor". It is the (automatically
generated" "assignment operator". The copy constructor is
"Point<n,T>::Point(Point const&)".
I am confused, because "my" operator is
"ndimobj<n, T>::operator= (const T)". Where i like to point that he has
a completely different argument (not const Point&, but const T). Please.
Why is the operator hidden?
Hiding is not about argument types. This is not like overloading. Hiding
is solely done by function NAME. In your case the function name is
"operator =". And "Point" has its own "operator =" (automatically
generated) which hides the "ndimobj::operator ="s (both, yours and the
automatic one).

Example:

struct Base
{
void foo();
};

struct Dev : Base
{
void foo(int);
};

int main()
{
Dev d;
d.foo(); //error
}

"void Base::foo()" is hidden by "void Dev::foo(int)" although they have
different arguments and may otherwise be overloads.
struct Base2
{
void foo();
};

struct Dev2 : Base2
{
using Base2::foo;
void foo(int);
};

int main()
{
Dev d;
d.foo(); //works
}

Here the "using Base2::foo" makes the base class functions visible
again. Now "foo" is an overloaded function.

Your case (simplified):

struct ndimobj
{
//ndimobj& operator = (ndimobj const&); //automatic
ndimobj& operator = (int); //your operator =
};

struct Point : ndimobj
{
//this "operator =" hides "operator =" of ndimobj:
//Point& operator = (Point const&); //automatic
};

The automatic operator = in Point hides the base class operator =.

HTH,
Frank
Aug 16 '07 #9

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

Similar topics

9
by: Frantisek Fuka | last post by:
This thing keeps bugging me. It's probably some basic misunderstanding on my part but I am stumped. Let's say I have two Python files: file.py and file2.py. Their contents is as follows: ...
4
by: Matthew Bell | last post by:
I've got a conceptual problem to do with inheritance. I'd be grateful if someone could help to clear up my confusion. An example. Say I need a class that's basically a list, with all the...
30
by: Vla | last post by:
why did the designers of c++ think it would be more useful than it turned out to be?
3
by: Ot | last post by:
First, a bit of background... I am an experienced programmer who has been programming since 1962. Languages I know include (a partial list) FORTRAN, COBOL, Basic (a number of versions), well, I...
4
by: MikeB | last post by:
I've been all over the net with this question, I hope I've finally found a group where I can ask about Visual Basic 2005. I'm at uni and we're working with Visual Basic 2005. I have some books, ...
7
by: jason | last post by:
In the microsoft starter kit Time Tracker application, the data access layer code consist of three cs files. DataAccessHelper.cs DataAcess.cs SQLDataAccessLayer.cs DataAcccessHelper appears...
8
by: Chris Asaipillai | last post by:
Hi there I have some questions for those experienced Visual Basic 6 programmers out there who have made the transition from VB6 to Vb.net. How long did it take you to learn at least the basic...
28
by: Randy Reimers | last post by:
(Hope I'm posting this correctly, otherwise - sorry!, don't know what else to do) I wrote a set of programs "many" years ago, running in a type of basic, called "Thoroughbred Basic", a type of...
5
by: Michael Thompson | last post by:
I am new to .Net and OOP techniques; I am not even certain that I using the correct terminology here. I believe that I want to know how to do inheritance. I want to create a custom "string"...
14
by: MartinRinehart | last post by:
Working on parser for my language, I see that all classes (Token, Production, Statement, ...) have one thing in common. They all maintain start and stop positions in the source text. So it seems...
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: 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?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.