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

Type conversion issue

I've got a problem involving casting operators using GCC 3.2 -
hopefully someone here can help me out. I've written the following
small program:

class A {
public:
operator int() { return 2; }
};

int main() {
A a, b;
if (a == b);
}

This program compiles fine. But consider this slight permutation:

int main() {
std::vector<A> a, b;
if (a == b);
}

This version fails. If I can compare 2 A's in the 1st case, why can I
not in a vector? GCC reports that there is no match for the comparison
operator but then lists the internal operator(int, int) as a candidate.
If it found that as the candidate, then why not use it?

/ranger/local/linux/gcc-3.2.3/lib/gcc-lib/i686-pc-linux-gnu/3.2.3/include/c++/bits/stl_algobase.h:
In
function `bool std::equal(_InputIter1, _InputIter1, _InputIter2)
[with
_InputIter1 = __gnu_cxx::__normal_iterator<const A*, std::vector<A,
std::allocator<A> > >, _InputIter2 =
__gnu_cxx::__normal_iterator<const A*,
std::vector<A, std::allocator<A> > >]':
/ranger/local/linux/gcc-3.2.3/lib/gcc-lib/i686-pc-linux-gnu/3.2.3/include/c++/bits/stl_vector.h:765:
instantiated from `bool std::operator==(const std::vector<_Tp,
_Alloc>&, const std::vector<_Tp, _Alloc>&) [with _Tp = A, _Alloc =
std::allocator<A>]'
a.cpp:10: instantiated from here
/ranger/local/linux/gcc-3.2.3/lib/gcc-lib/i686-pc-linux-gnu/3.2.3/include/c++/bits/stl_algobase.h:681:
no
match for `const A& == const A&' operator
/ranger/local/linux/gcc-3.2.3/lib/gcc-lib/i686-pc-linux-gnu/3.2.3/include/c++/bits/stl_algobase.h:681:
candidates
are: operator==(int, int) <built-in>

Thanks for the help.

Jul 23 '05 #1
1 1291
balor wrote:
I've got a problem involving casting operators using GCC 3.2 -
hopefully someone here can help me out. I've written the following
small program:

class A {
public:
operator int() { return 2; }
Declare this member 'const' and you'll have it.
};
[...]


V
Jul 23 '05 #2

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

Similar topics

17
by: Sean Ross | last post by:
Hi. Recently I made a small script to do some file transferring (among other things). I wanted to monitor the progress of the file transfer, so I needed to know the size of the files I was...
21
by: Nitin Bhardwaj | last post by:
Hi all, It is said that C++ is a strongly typed language and thus a type-safe language (unlike C). So how does one explain the following behaviour : int main(void) { char *p = NULL; p = "A...
3
by: Ashish | last post by:
its on .net 2.0 I am calling a stored procedure which returns value SCOPE_INDENTITY() , when i try to convert the result from object to int, it throws a type conversion error... after some...
4
by: ranjeet.gupta | last post by:
Dear All Please check the below code: UINT8 MsgLength = 0; MsgLength = strlen((char *)msg); if ( MsgLength == 0 || MsgLength 64) {
6
by: NM | last post by:
I am given an int and have to tell whether the bit representation of that int is a palindrome or not. Here is what I have come up with bool isIntPalindrome(int num) { unsigned int temp = num;...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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: 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...

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.