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

doubts on operator

I have a class that defines a generic point where p={a1..an}

template<class T,int iclass Point
{
public:
T v[i];

Point<T,i>()
{
for (int j=0;j<i;j++) v[j]=0;
}
inline T & operator [] ( const int j ){
assert(j>=0 && j<i);
return v[j];
}
inline const T & operator [] ( const int j ) const {
assert(j>=0 && j<i);
return v[j];
}
inline Point<T,i>& operator=(const Point<T,i& num)
{
for (int j=0;j<i;j++) v[j]=num.v[j];
return *this;

}
inline Point<T,i>& operator=(const float* pnum)
{
for (int j=0;j<i;j++) v[j]=pnum[j];
return *this;

}
inline Point<T,ioperator + ( Point<T,iconst & num) const {
Point<T,ivect;
for (int j=0;j<i;j++) vect.v[j]=v[j]+num.v[j];
return vect;
}
inline Point<T,ioperator - ( Point<T,iconst & num) const {
Point<T,ivect;
for (int j=0;j<i;j++) vect.v[j]=v[j]-num.v[j];
return vect;
}
inline Point<T,ioperator * ( const T s ) const {
Point<T,ivect;
for (int j=0;j<i;j++) vect.v[j]=v[j]*s;
return vect;
}

inline Point<T,i>& operator *= ( const T s ){
for (int j=0;j<i;j++) v[j]=v[j]*s;
return *this;
}
}
I define a new class Point2

template<class Tclass Point2: public Point<T,2>
{
public:
Point2<T>()
{
v[0]=0;
v[1]=1;
}

Point2<T>(const T& a, const T& b)
{
v[0]=a;
v[1]=b;
}

T x() { return v[0] };
T y() { return v[1] };

};

and in main program I run:

Point2f a;
Point2f b;
Point2f c;
c=a+b;

I get this error:

error C2679: binary '=' : no operator found which takes a right-hand
operand of type 'Point<T,i>' (or there is no acceptable conversion)
with
[
T=float,
i=2
]
could be 'Point2<T&Point2<T>::operator =(const Point2<T>
&)'
with
[
T=float
]
while trying to match the argument list '(Point2f,
Point<T,i>)'
with
[
T=float,
i=2
]
Why Point2 don't call the operator= defined in Point<i,T>?

Point2 is a Point<i,Tbecause derive by Point<i,T>, I don't
understand...
Please explain me.

Aug 1 '08 #1
6 1435
alessio211734 wrote:
I have a class that defines a generic point where p={a1..an}

template<class T,int iclass Point
{
public:
T v[i];
Public? Really?
>
Point<T,i>()
First off, you don't need to repeat "<T,i>" *inside* the template class
definition.
{
for (int j=0;j<i;j++) v[j]=0;
}
inline T & operator [] ( const int j ){
assert(j>=0 && j<i);
return v[j];
}
inline const T & operator [] ( const int j ) const {
assert(j>=0 && j<i);
return v[j];
}
inline Point<T,i>& operator=(const Point<T,i& num)
{
for (int j=0;j<i;j++) v[j]=num.v[j];
return *this;

}
inline Point<T,i>& operator=(const float* pnum)
{
for (int j=0;j<i;j++) v[j]=pnum[j];
return *this;

}
inline Point<T,ioperator + ( Point<T,iconst & num) const {
Point<T,ivect;
for (int j=0;j<i;j++) vect.v[j]=v[j]+num.v[j];
return vect;
}
inline Point<T,ioperator - ( Point<T,iconst & num) const {
Point<T,ivect;
for (int j=0;j<i;j++) vect.v[j]=v[j]-num.v[j];
return vect;
}
inline Point<T,ioperator * ( const T s ) const {
Point<T,ivect;
for (int j=0;j<i;j++) vect.v[j]=v[j]*s;
return vect;
}

inline Point<T,i>& operator *= ( const T s ){
for (int j=0;j<i;j++) v[j]=v[j]*s;
return *this;
}
}
I define a new class Point2

template<class Tclass Point2: public Point<T,2>
{
public:
Point2<T>()
{
v[0]=0;
v[1]=1;
}

Point2<T>(const T& a, const T& b)
{
v[0]=a;
v[1]=b;
}

T x() { return v[0] };
T y() { return v[1] };

};

and in main program I run:

Point2f a;
So, my guess would be that 'Point2f' is a typedef, something like

typedef Point2<floatPoint2f;

Next time, please, be verbose about those things.
Point2f b;
Point2f c;
c=a+b;

I get this error:

error C2679: binary '=' : no operator found which takes a right-hand
operand of type 'Point<T,i>' (or there is no acceptable conversion)
[..]
Why Point2 don't call the operator= defined in Point<i,T>?
Because Point2 has its *own* operator=, which *hides* any that it can
inherit from its base class.
>
Point2 is a Point<i,Tbecause derive by Point<i,T>, I don't
understand...
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 1 '08 #2
Because Point2 has its *own* operator=, which *hides* any that it can
inherit from its base class.
What's the operator= that Point2 define?
Point2 define a default operator?
What's the advantages of hereditance if I should re-define an operator
for Point2 again?
Can't use operator= and other operators defined in the base class
without re-define in the derived class Point2?
Aug 1 '08 #3
alessio211734 wrote:
>Because Point2 has its *own* operator=, which *hides* any that it can
inherit from its base class.

What's the operator= that Point2 define?
It's called "the default copy-assignment operator", provided by the
compiler. It has the following signature:

Point2& Point2::operator=(Point2 const&);
Point2 define a default operator?
Yes.
What's the advantages of hereditance if I should re-define an operator
for Point2 again?
"Hereditance"? You don't have to redefine the assignment operator, you
just need to make it visible. See your book about the " 'using'
declaration".
Can't use operator= and other operators defined in the base class
without re-define in the derived class Point2?
Can.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 1 '08 #4
Hi

alessio211734 wrote:
>Because Point2 has its *own* operator=, which *hides* any that it can
inherit from its base class.

What's the operator= that Point2 define?
Point2 define a default operator?
What's the advantages of hereditance if I should re-define an operator
for Point2 again?
Can't use operator= and other operators defined in the base class
without re-define in the derived class Point2?
An assignment operator for a base class is rarely useful. Consider:
A a;
B b;
a = b;

Without additional knowledge (i.e. without a user-defined assignment
operator), this only makes sense if b contains all the parts of an A,
which is the case if B is equal to or derived from A. If A is derived from
B, on the other hand, then how would you fill in the missing data?

Markus

Aug 1 '08 #5
On 1 Ago, 16:36, Markus Moll <markus.m...@esat.kuleuven.ac.bewrote:
Hi

alessio211734 wrote:
Because Point2 has its *own* operator=, which *hides* any that it can
inherit from its base class.
What's the operator= that Point2 define?
Point2 define a default operator?
What's the advantages of hereditance if I should re-define an operator
for Point2 again?
Can't use operator= and other operators defined in the base class
without re-define in the derived class Point2?

An assignment operator for a base class is rarely useful. Consider:
A a;
B b;
a = b;

Without additional knowledge (i.e. without a user-defined assignment
operator), this only makes sense if b contains all the parts of an A,
which is the case if B is equal to or derived from A. If A is derived from
B, on the other hand, then how would you fill in the missing data?

Markus

I would like define constructors for n-dimensional point.
If I include in the base class these constructors my application work,
but I think that this is not safe because
I can call the constructor with three parameters when I use
Point<float,2for example.
Point<T,i>(const T& a, const T& b)
{
assert(i==2);
v[0]=a;
v[1]=b;
}

Point<T,i>(const T& a, const T& b,const T& c)
{
assert(i==3);
v[0]=a;
v[1]=b;
v[2]=c;
}

I think to delete constructors in base classes and extends the base
class with
for example Point2 derived by Point and in this class define the
constructor with 2 parameters.

What's syntax should use in Point2 for utilize Point operators?


Aug 1 '08 #6
alessio211734 wrote:
I have a class that defines a generic point where p={a1..an}

template<class T,int iclass Point
{
public:
T v[i];

Point<T,i>()
{
for (int j=0;j<i;j++) v[j]=0;
}
inline T & operator [] ( const int j ){
In addition to Victor's comments, the 'inline' is superfluous here. All
member functions defined in the class declaration are inline.

--
Ian Collins.
Aug 1 '08 #7

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

Similar topics

6
by: greg | last post by:
Hello all, I havent used STL containers much, as I am used to MFC containers/classes always ..The differences that I could see is iterators and algorithms. The algorithms providing some basic...
0
by: abbas reji | last post by:
--0-599929911-1059996886=:4358 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit Content-Id: Content-Disposition: inline ...
1
by: Piotre Ugrumov | last post by:
I have some problems and some doubts. I have implemented a class hierachy. The base class Velivolo, from Velivolo derive Militare and Civile, from militare derive Aereo and Elicottero, from Civile...
6
by: ritesh | last post by:
Hi, I have been reading some text on C and C++ (i.e advanced books). One of the books mentioned that C++ requires a runtime support whereas C does not - what the author was trying to say was...
17
by: ranjeet.gupta | last post by:
Dear All Below are the few doubts which I got while studying about C 1. Is there any method in C by which we can process the entire string in one unit, 2. Does there exist any way to...
1
by: NagaKiran | last post by:
Hi I want to post VBA related doubts. Where can I post my doubts in VBA? thanks bye
24
by: temper3243 | last post by:
Hi, Many people have used this code in my project. It works because b is using the extra memory for other 4 variables.I access obj max. well here are a few questions 1) Where does it fail. I...
2
by: tcsvikr | last post by:
consider the code given below: class base { //line1 private: //line2 const int a; ...
7
by: sindhu sweet | last post by:
Hi friends, can anyone tell me.. 1)what happens in the statement int a=16,a>>2=4 what does the >>(right shift operator)do? 2)what will be the result of the expression 13&25? what...
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
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
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
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.