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

What the assignment operator from other type(with template) doesn't work?

The following shows the source code and the error message(from
g++-3.3). I wrote two assignment operator. One is for the same type
case, the other one is for different type case. I'm wondering why it
doesn't work. The error line is marked with comments in the source
code.

Thanks,
Peng
/* main.cc */
template <typename U>
class A{
public:
A() { }
A &operator=(A &that) {
_internal = that._internal;
}
template <typename V>
A &operator=(V &that) {
_internal = that.get_internal();
}
private:
U _internal;
};

int main(int argc, char *argv[])
{
A<int> a;
A<double> b = a;//error
}

/* error message */
g++-3.3 -g -I/usr/local/include/clapack/ -c -o main.o main.cc
main.cc: In function `int main(int, char**)':
main.cc:19: error: conversion from `A<int>' to non-scalar type
`A<double>'
requested
make: *** [main.o] Error 1

Nov 9 '05 #1
4 1844

Pe*******@gmail.com wrote:
The following shows the source code and the error message(from
g++-3.3). I wrote two assignment operator. One is for the same type
case, the other one is for different type case. I'm wondering why it
doesn't work. The error line is marked with comments in the source
code.


Sorry, there were some typos in the previous post. Here is the
corrected code. Please look at the error line at the end of the
program.
I'm wondering why
A<double> b;
b = a;
works, but
A<double> c = a; //error
doesn't work.
/** source code */
template <typename U>
class A{
public:
A() { }
A &operator=(A &that) {
_internal = that._internal;
}
template <typename V>
A &operator=(V &that) {
_internal = that.get_internal();
}
U get_internal() const {return _internal; }
private:
U _internal;
};

int main(int argc, char *argv[])
{
A<int> a;
A<double> b;
b = a;
A<double> c = a; //error
}
/********* error message */
g++-3.3 -g -I/usr/local/include/clapack/ -c -o main.o main.cc
main.cc: In function `int main(int, char**)':
main.cc:22: error: conversion from `A<int>' to non-scalar type
`A<double>'
requested
make: *** [main.o] Error 1

Nov 9 '05 #2
Pe*******@gmail.com wrote:

Pe*******@gmail.com wrote:
The following shows the source code and the error message(from
g++-3.3). I wrote two assignment operator. One is for the same type
case, the other one is for different type case. I'm wondering why it
doesn't work. The error line is marked with comments in the source
code.
Sorry, there were some typos in the previous post. Here is the
corrected code. Please look at the error line at the end of the
program.
I'm wondering why
A<double> b;
b = a;
works, but
A<double> c = a; //error
doesn't work.
/** source code */
template <typename U>
class A{
public:
A() { }
A &operator=(A &that) {
_internal = that._internal;
}
template <typename V>
A &operator=(V &that) {
_internal = that.get_internal();
}
U get_internal() const {return _internal; }
private:
U _internal;
};

int main(int argc, char *argv[])
{
A<int> a;
A<double> b;
b = a;
A<double> c = a; //error


Contrary to what the notation suggests, this does not call the assignment
operator. Instead a copy constructor is needed: C++ considers this line as
the construction of the object c from the object a. The above is actually
equivalent to:

A<double> c ( a );
}

Best

Kai-Uwe Bux
Nov 9 '05 #3
Pe*******@gmail.com wrote:
Pe*******@gmail.com wrote:
The following shows the source code and the error message(from
g++-3.3). I wrote two assignment operator. One is for the same type
case, the other one is for different type case. I'm wondering why it
doesn't work. The error line is marked with comments in the source
code.
Sorry, there were some typos in the previous post. Here is the
corrected code. Please look at the error line at the end of the
program.
I'm wondering why
A<double> b;
b = a;
works, but
A<double> c = a; //error
doesn't work.


Isn't this in the FAQ? There is no assignment operator involved
when you write

<type-id> name = othername;

It's called "initialisation" and the actual form is

<type-id> name(<type-id>(othername));

(only by form, the syntax immediately above won't work). IOW, you
attempt to initialise a temporary object of type 'A<double>' from
the 'a' object, when 'A<double>' has no constructor from 'A<int>'.
[...]


V
Nov 9 '05 #4
Kai-Uwe Bux wrote:
A<int> a;
A<double> b;
b = a;
A<double> c = a; //error
Contrary to what the notation suggests, this does not call the
assignment operator. Instead a copy constructor is needed: C++
considers this line as the construction of the object c from the
object a. The above is actually equivalent to:

A<double> c ( a );


Not exactly, but close enough.

}


V
Nov 9 '05 #5

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

Similar topics

2
by: Dave Benjamin | last post by:
Hey folks, I was just reading about the attempt to insert a backdoor into the Linux kernel. You can read the details here: http://kerneltrap.org/node/view/1584 This is the code that the...
2
by: Default User | last post by:
I received some code from another programmer who happens to be out of town. In this, he had something like: struct st { std::string s<20>; }; Looks like it's setting the string size.
3
by: vbMark | last post by:
Hi there, I need to create an asp page using ASP.NET that does not have an interface but only accepts URL Query Strings. When starting a new project what template should I use? Thanks! --
6
by: libsfan01 | last post by:
hi all! is it possible to change an element type with javacript for example onclick to change a text box to a hidden element or to a textarea? kind regards marc
4
by: jason.teen | last post by:
Hi, when i am joining on a Column of Text Type with one of Memo type the resulting entry has funny chinese characters! Has anyone else encountered this before? Is there a cure?? Cheers.
1
by: Hema | last post by:
Onsite Assignment with SIEMENS / 6-8 years experience /J2EE/SOAP/EAI The position that we have is a long term onsite assignment to US with SIEMENS. J2EE, EAI, SOAP technologies 6-8 years of...
0
by: neeraj | last post by:
Hi, all Could anny one give me help how can I use like operator with these data types "integer , datetime or boolean" in DataView.RowFilter Actually when I try to get the data from dataview...
1
by: ankitamca85 | last post by:
difference between reference and reference type?with example
2
by: kenneth6 | last post by:
i am now having: string* book=new string; // string array in Windows form application (managed code), i am unable to user it ! how can change it to char type with the same effect and...
7
by: neelsmail | last post by:
Hi, I want to give default value as NULL/0 for non-type template parameter. I using SunStudio on Linux. I have tried following: #define non_closer ((int(*)(FILE*))0L) template<class T, int...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
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...

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.