473,785 Members | 2,756 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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>::operat or=(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 1393
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>::operat or=(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=(cons t 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***@superlin k.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 "constructo r". It is the (automatically
generated" "assignment operator". The copy constructor is
"Point<n,T>::Po int(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::opera tor ="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
1772
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: file.py: --------------------- import file2 def hello(): print "Hello" file2.hello2()
4
1407
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 normal list methods, but I want a custom __init__ so that the list that is created is rather than (yes, it's a bogus example, but it does to make the point). Without bothering with inheritance, I could do:
30
2728
by: Vla | last post by:
why did the designers of c++ think it would be more useful than it turned out to be?
3
1167
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 counted them up, a total of 18 high-level languages and 22 assembler languages. In addition, I have seen the growth of OOP from its evolution from fundamental concepts. There was old-style spaghetti code, structured code, functional isolation,...
4
1732
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, - Programming Visual Basic by Balena (MS Press) and - Visual Basic 2005 by Willis (WROX), but they don't go into the forms design aspects and describing the various controls at all. What bookscan I get that will cover that?
7
4474
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 to be checking that the correct data type is used DataAcess sets an abstract class and methods
8
2864
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 elements of VB.net....so that you were confident to write a application from scratch. This wouldnt necessarily
28
3597
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 business basic. I need to re-write it, bring it kicking and screaming to run on Windows XP. This is for a video rental place, tracks movie and game rentals, customers, employee transactions, reservations, does reports,..... and on. I know some of...
5
1327
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" class, "TSTString". TSTString should behave exactly like a string except that I want to add a couple methods like "public string ProperCase()", but still have the TSTString object behave like string, and be able to pass them through as string-type...
14
1852
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 logical to have them all inherit from a base class that defines those, but this doesn't work: import tok class code: def __init__( self, start, stop ):
0
9643
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10147
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10087
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9947
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8971
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6737
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5380
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.