C++ Error C2664

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

Suji

C++ Error C2664

Post by Suji »

I'm not very far into programming my own engine (I'm using C++), but I'm working on the FEN input function, and I got this error:

Code: Select all

error C2664: 'atoi' : cannot convert parameter 1 from 'char' to 'const char *'
1>Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
I honestly don't have a clue how to solve this problem. My engine is going to hopefully be using the 0x88 method.

Here is the relevant code to my issue.

Code: Select all

class Board
{
     private:
          int *board;  //Pointer to the board in 0x88 method...Or, a 
                                 dynamically created array

     public:
         Board()
		  {
			  this->board = new int[128];  //dynamically creates the
                                                                       array
                  }
         InputFEN(string fen)
               do something;

                       default:
                       squareindex += atoi(fen[i]);  //Where compiler says the
                                                                    code fails
};
squareindex keeps track of where on the board I am.
fen is a string, and fen is how I keep track of where I am in the string.

This is the point where I am taking into account the numbers in the position part of the string.

How do I solve this problem?
Thanks in advance.

P.S. I have Microsoft Visual Studio 2008 Professional as a compiler.
Dann Corbit
Posts: 12538
Joined: Wed Mar 08, 2006 8:57 pm
Location: Redmond, WA USA

Re: C++ Error C2664

Post by Dann Corbit »

Suji wrote:I'm not very far into programming my own engine (I'm using C++), but I'm working on the FEN input function, and I got this error:

Code: Select all

error C2664: 'atoi' : cannot convert parameter 1 from 'char' to 'const char *'
1>Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
I honestly don't have a clue how to solve this problem. My engine is going to hopefully be using the 0x88 method.

Here is the relevant code to my issue.

Code: Select all

class Board
{
     private:
          int *board;  //Pointer to the board in 0x88 method...Or, a 
                                 dynamically created array

     public:
         Board()
		  {
			  this->board = new int[128];  //dynamically creates the
                                                                       array
                  }
         InputFEN(string fen)
               do something;

                       default:
                       squareindex += atoi(fen[i]);  //Where compiler says the
                                                                    code fails
};
squareindex keeps track of where on the board I am.
fen is a string, and fen is how I keep track of where I am in the string.

This is the point where I am taking into account the numbers in the position part of the string.

How do I solve this problem?
Thanks in advance.

P.S. I have Microsoft Visual Studio 2008 Professional as a compiler.


The type of fen is a string. The variable fen is therefore assuredly not a char pointer.

const char *fen_guts = fen.c_str() ;
will give you a pointer to the character string inside of fen.
Then you can do this:
squareindex += atoi(&fenguts);
mcostalba
Posts: 2684
Joined: Sat Jun 14, 2008 9:17 pm

Re: C++ Error C2664

Post by mcostalba »

These are declaration of involved functions:

Code: Select all

int atoi ( const char * str );

const char& string::operator[] ( size_t pos ) const;
The error message says:

Code: Select all

error C2664: 'atoi' : cannot convert parameter 1 from 'char' to 'const char *'
So it means that atoi() expects a pointer to a char as argument while you are passing a char (a const char reference to be precise).

Just pass a pointer to fix the compile:

Code: Select all

       squareindex += atoi(&fen[i]);
Suji

Re: C++ Error C2664

Post by Suji »

Thanks Dann and Marco. Your solutions work.

YAY!! :D :D :D :D

You guys are awesome!
mathmoi
Posts: 286
Joined: Mon Mar 13, 2006 5:23 pm
Location: Québec

Re: C++ Error C2664

Post by mathmoi »

If you are sure the character is a digit there is a more elegent/efficient way to convert it to an integer :

Code: Select all

unsigned int a_int;
char a_char = '8';
a_int = a_char - '0'; // That's the zero character
Teemu Pudas
Posts: 88
Joined: Wed Mar 25, 2009 12:49 pm

Re: C++ Error C2664

Post by Teemu Pudas »

mcostalba wrote:Just pass a pointer to fix the compile:

Code: Select all

       squareindex += atoi(&fen[i]);
Don't do that - C++ strings aren't required to be null-terminated. Just use c_str().
mcostalba
Posts: 2684
Joined: Sat Jun 14, 2008 9:17 pm

Re: C++ Error C2664

Post by mcostalba »

Teemu Pudas wrote:
mcostalba wrote:Just pass a pointer to fix the compile:

Code: Select all

       squareindex += atoi(&fen[i]);
Don't do that - C++ strings aren't required to be null-terminated. Just use c_str().
Yes. You are right. In our case, for fen strings should not occur, but is a good general rule.

As it is a good general rule try to avoid mixing c and c++ library functions if it is possible and use only C or only C++ approach for writing a given function.
Sven
Posts: 4052
Joined: Thu May 15, 2008 9:57 pm
Location: Berlin, Germany
Full name: Sven Schüle

Re: C++ Error C2664

Post by Sven »

Suji wrote:Thanks Dann and Marco. Your solutions work.
That surprises me. While you may now have satisfied the compiler you probably don't get the semantics right. What atoi() does is that it converts a character string into its numerical value, or zero if the character string contains non-digits. So atoi("123") is 123, atoi("5") is 5, and atoi("5XY") is 0.

From your code example you give I get the strong impression that this is not what you want, or need. It looks like you're parsing an FEN string, and there you most probably have to scan each single character.

So atoi() is simply the wrong function for that purpose. What you need is some conversion of a single character into a value that can be used directly for further processing. In case that value is numeric the solution has already been mentioned in this thread: subtract the character '0' from the input character, and check that the result, considered as an int, is in the range [0..9], otherwise the character was non-numeric.

Coming back to your comment "Your solutions work", I would really like to know whether this means "it compiles" or "it does the right thing at runtime". The latter would indicate that you have a very special case where atoi() returns the right value for you "by accident" (e.g. you are already at the last position of the input string).

Sven
User avatar
Zach Wegner
Posts: 1922
Joined: Thu Mar 09, 2006 12:51 am
Location: Earth

Re: C++ Error C2664

Post by Zach Wegner »

Sven Schüle wrote:
Suji wrote:Thanks Dann and Marco. Your solutions work.
That surprises me. While you may now have satisfied the compiler you probably don't get the semantics right. What atoi() does is that it converts a character string into its numerical value, or zero if the character string contains non-digits. So atoi("123") is 123, atoi("5") is 5, and atoi("5XY") is 0.

From your code example you give I get the strong impression that this is not what you want, or need. It looks like you're parsing an FEN string, and there you most probably have to scan each single character.

So atoi() is simply the wrong function for that purpose. What you need is some conversion of a single character into a value that can be used directly for further processing. In case that value is numeric the solution has already been mentioned in this thread: subtract the character '0' from the input character, and check that the result, considered as an int, is in the range [0..9], otherwise the character was non-numeric.

Coming back to your comment "Your solutions work", I would really like to know whether this means "it compiles" or "it does the right thing at runtime". The latter would indicate that you have a very special case where atoi() returns the right value for you "by accident" (e.g. you are already at the last position of the input string).

Sven
No, atoi("5XY") is 5. I still think this solution is bad, because it relies on the fact that FENs can only have one consecutive digit in a row, which most likely will not be obvious from the code. *c - '0' is much simpler and better IMO.
Dann Corbit
Posts: 12538
Joined: Wed Mar 08, 2006 8:57 pm
Location: Redmond, WA USA

Re: C++ Error C2664

Post by Dann Corbit »

Sven Schüle wrote:
Suji wrote:Thanks Dann and Marco. Your solutions work.
That surprises me. While you may now have satisfied the compiler you probably don't get the semantics right. What atoi() does is that it converts a character string into its numerical value, or zero if the character string contains non-digits. So atoi("123") is 123, atoi("5") is 5, and atoi("5XY") is 0.

From your code example you give I get the strong impression that this is not what you want, or need. It looks like you're parsing an FEN string, and there you most probably have to scan each single character.

So atoi() is simply the wrong function for that purpose. What you need is some conversion of a single character into a value that can be used directly for further processing. In case that value is numeric the solution has already been mentioned in this thread: subtract the character '0' from the input character, and check that the result, considered as an int, is in the range [0..9], otherwise the character was non-numeric.

Coming back to your comment "Your solutions work", I would really like to know whether this means "it compiles" or "it does the right thing at runtime". The latter would indicate that you have a very special case where atoi() returns the right value for you "by accident" (e.g. you are already at the last position of the input string).

Sven
#include <stdio.h>
#include <stdlib.h>
/*
7.20.1.2 The atoi, atol, and atoll functions
Synopsis
1 #include <stdlib.h>
int atoi(const char *nptr);
long int atol(const char *nptr);
long long int atoll(const char *nptr);
Description
2 The atoi, atol, and atoll functions convert the initial portion of the string pointed to by nptr to int, long int, and long long int representation, respectively.
Except for the behavior on error, they are equivalent to
atoi: (int)strtol(nptr, (char **)NULL, 10)
atol: strtol(nptr, (char **)NULL, 10)
atoll: strtoll(nptr, (char **)NULL, 10)
Returns
3 The atoi, atol, and atoll functions return the converted value.
Forward references: the strtol, strtoll, strtoul, and strtoull functions
(7.20.1.4).
*/
int main(void)
{
int i = atoi("5XY");
printf("i=%d\n",i);
return 0;
}
/*
On my system this prints:
i=5
*/