Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) _algobase.h: Cannot convert 'const int' to 'MyClass *'

 

Compile error.

 

 

 

 

 

Full error message

 

[C++ Error] _algobase.h(341): E2034 Cannot convert 'const int' to 'MyClass *'

 

 

 

 

 

Cause

 

The following code caused this compile error:

 

#include <vector>

struct MyClass {};

int main()
{
  const int size = 10; //Or other positive int
  std::vector<MyClass*> v(size,0);
}

 

IDE: C++ Builder 6.0

Compiler: Borland BCC32.EXE version 6.0.10.157

Project type: Console

 

The zero denotes that the MyClass pointer is uninitialized. The compiler, however, believes this zero denotes an integer value.

 

The code where the compiler takes you, in _algobase.h:

 

template <class _OutputIter, class _Size, class _Tp>
_STLP_INLINE_LOOP
_OutputIter fill_n(_OutputIter __first, _Size __n, const _Tp& __value) {
  _STLP_FIX_LITERAL_BUG(__first)
  for ( ; __n > 0; --__n, ++__first)
    *__first = __value; //THIS LINE
  return __first;
}

 

 

 

 

 

Solution

 

There are two options:

  1. Change the first argument's data type to unsigned int
  2. Cast the null in the second argument explicitly to a MyClass*

 

 

 

 

 

Change the first argument's data type to unsigned int

 

#include <vector>
 
struct MyClass {};
 
int main()
{
  const unsigned int size = 10; //Or other value
  std::vector<MyClass*> v(size,0);
}

 

I would bet that the first argument's data type might also be std::size_t.

 

 

 

 

 

Cast the null in the second argument explicitly to a MyClass*

 

#include <vector>

struct MyClass {};

int main()
{
  const int size = 10; //Or other positive int
  std::vector<MyClass*> v(size, static_cast<MyClass*>(0));
}

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict