From: Vladimir Grigoriev on
Having the following code

struct Point

{

Point( int i = 0, int j = 0 ): x( i ), y( j ) {}

int x, y;

};

#define MAX_SIZE 10

int _tmain(int argc, _TCHAR* argv[])

{

std::vector<Point> v;

v.reserve( MAX_SIZE );

for ( int i = 0; i < MAX_SIZE; ++i )

{

v.push_back( Point( i, i ) );

}


return 0;

}

I get the error

error C2446: ':' : no conversion from 'const
std::_Vector_iterator<_Ty,_Alloc>' to 'int'
with
[
_Ty=Point,
_Alloc=std::allocator<Point>
]
No user-defined-conversion operator available that can perform this
conversion, or the operator cannot be called

: while compiling class template member function
'std::_Vector_iterator<_Ty,_Alloc>
std::vector<_Ty>::insert(std::_Vector_iterator<_Ty,_Alloc>,const _Ty &)'
with
[
_Ty=Point,
_Alloc=std::allocator<Point>
]



The error occurs inside <vector> in this place



iterator insert(iterator _Where, const _Ty& _Val)

{ // insert _Val at _Where

size_type _Off = size() == 0 ? 0 : _Where - begin();

_Insert_n(_Where, (size_type)1, _Val);

return (begin() + _Off);

}



What is the matter?



Vladimir Grigoriev


From: Vladimir Grigoriev on
I am sorry. The initial code is nor complete. I have found that the error
occurs when I add the following template operator

template <typename T>

inline const T operator -( const T &lhs, const T &rhs )

{

return ( T( lhs ) -= rhs );

}

So the code should look the following way

#include "stdafx.h"

#include <iostream>

#include <vector>

struct Point

{

explicit Point( int i = 0, int j = 0 ): x( i ), y( j ) {}

Point( const Point &rhs ): x( rhs.x ), y( rhs.y ) {}

~Point() {}

Point & operator =( const Point &rhs )

{

x = rhs.x; y = rhs.y;

return ( *this );

}

Point & operator -=( const Point &rhs )

{

x -= rhs.x; y -= rhs.y;

return ( *this );

}

Point & operator --()

{

*this -= Point( 1, 1 );

return ( *this );

}

const Point operator --( int )

{

Point tmp = *this;

--*this;

return ( tmp );

}

const Point operator -() const

{

return ( Point( -x, -y ) );

}

int x, y;

};

inline std::ostream & operator <<( std::ostream &os, const Point &rhs )

{

os << "{" << rhs.x << ", " << rhs.y << "}";

return ( os );

}

inline bool operator ==( const Point &lhs, const Point &rhs )

{

return ( ( lhs.x == rhs.x ) && ( lhs.y == rhs.y ) );

}

inline bool operator !=( const Point &lhs, const Point &rhs )

{

return ( !( lhs == rhs ) );

}

template <typename T>

inline const T operator -( const T &lhs, const T &rhs )

{

return ( T( lhs ) -= rhs );

}

#define MAX_SIZE 10

int _tmain(int argc, _TCHAR* argv[])

{

std::vector<Point> v;

v.reserve( MAX_SIZE );

for ( int i = 0; i < MAX_SIZE; ++i )

{

v.push_back( Point( i, i ) );

}


return 0;

}



What is the matter?



Vladimir Grigoriev


From: Victor Bazarov on
Vladimir Grigoriev wrote:
> Having the following code
>
> struct Point
>
> {
>
> Point( int i = 0, int j = 0 ): x( i ), y( j ) {}
>
> int x, y;
>
> };
>
> #define MAX_SIZE 10
>
> int _tmain(int argc, _TCHAR* argv[])
>
> {
>
> std::vector<Point> v;

I don't see '#include <vector>' anywhere in this program. Did you post
the actual code?

>
> v.reserve( MAX_SIZE );
>
> for ( int i = 0; i < MAX_SIZE; ++i )
>
> {
>
> v.push_back( Point( i, i ) );
>
> }
>
>
> return 0;
>
> }
>
> I get the error
>
> error C2446: ':' : no conversion from 'const
> std::_Vector_iterator<_Ty,_Alloc>' to 'int'
> with
> [
> _Ty=Point,
> _Alloc=std::allocator<Point>
> ]
> No user-defined-conversion operator available that can perform this
> conversion, or the operator cannot be called
>
> : while compiling class template member function
> 'std::_Vector_iterator<_Ty,_Alloc>
> std::vector<_Ty>::insert(std::_Vector_iterator<_Ty,_Alloc>,const _Ty &)'
> with
> [
> _Ty=Point,
> _Alloc=std::allocator<Point>
> ]
>
>
>
> The error occurs inside <vector> in this place
>
>
>
> iterator insert(iterator _Where, const _Ty& _Val)
>
> { // insert _Val at _Where
>
> size_type _Off = size() == 0 ? 0 : _Where - begin();
>
> _Insert_n(_Where, (size_type)1, _Val);
>
> return (begin() + _Off);
>
> }
>
>
>
> What is the matter?

Any chance you're using VC++ v6? If so, you're on your own.

I don't see you use 'insert' in your code anywhere. Are you sure you've
posted the code that you've compiled?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
From: Vladimir Grigoriev on

I use Visual C++ 2005 EE.

Vladimir Grigoriev


From: Vladimir Grigoriev on
Victor, it is a good remark and I have presented already more detailed
information.

Vladimir Grigoriev

"Victor Bazarov" <v.Abazarov(a)comAcast.net> wrote in message
news:hg893g$ef5$1(a)news.datemas.de...
> Vladimir Grigoriev wrote:
>> Having the following code
>>
>> struct Point
>>
>> {
>>
>> Point( int i = 0, int j = 0 ): x( i ), y( j ) {}
>>
>> int x, y;
>>
>> };
>>
>> #define MAX_SIZE 10
>>
>> int _tmain(int argc, _TCHAR* argv[])
>>
>> {
>>
>> std::vector<Point> v;
>
> I don't see '#include <vector>' anywhere in this program. Did you post
> the actual code?
>
>>
>> v.reserve( MAX_SIZE );
>>
>> for ( int i = 0; i < MAX_SIZE; ++i )
>>
>> {
>>
>> v.push_back( Point( i, i ) );
>>
>> }
>>
>>
>> return 0;
>>
>> }
>>
>> I get the error
>>
>> error C2446: ':' : no conversion from 'const
>> std::_Vector_iterator<_Ty,_Alloc>' to 'int'
>> with
>> [
>> _Ty=Point,
>> _Alloc=std::allocator<Point>
>> ]
>> No user-defined-conversion operator available that can perform
>> this conversion, or the operator cannot be called
>>
>> : while compiling class template member function
>> 'std::_Vector_iterator<_Ty,_Alloc>
>> std::vector<_Ty>::insert(std::_Vector_iterator<_Ty,_Alloc>,const _Ty &)'
>> with
>> [
>> _Ty=Point,
>> _Alloc=std::allocator<Point>
>> ]
>>
>>
>>
>> The error occurs inside <vector> in this place
>>
>>
>>
>> iterator insert(iterator _Where, const _Ty& _Val)
>>
>> { // insert _Val at _Where
>>
>> size_type _Off = size() == 0 ? 0 : _Where - begin();
>>
>> _Insert_n(_Where, (size_type)1, _Val);
>>
>> return (begin() + _Off);
>>
>> }
>>
>>
>>
>> What is the matter?
>
> Any chance you're using VC++ v6? If so, you're on your own.
>
> I don't see you use 'insert' in your code anywhere. Are you sure you've
> posted the code that you've compiled?
>
> V
> --
> Please remove capital 'A's when replying by e-mail
> I do not respond to top-posted replies, please don't ask