<分区>
我正在尝试编写一个函数,将字符串数组作为参数,将其转换为 int 数组,然后返回新数组。我认为使用“Atoi”会非常简单,但显然您不能按照我尝试的方式使用它。
到目前为止,这是我的代码。
int GameHandler::convertToInt(string array[])
{
int tmp=0;
string arr[20]=array;
int values[20];
for(int i=0;i<20;i++)
values[i]=0;
for(int i=0;i<20;i++)
{
tmp=atoi(arr[i]);
values[i]=tmp;
}
return values;
}
这是我从编译器得到的错误信息:
GameHandler.cpp: In member function ‘int GameHandler::convertToInt(std::string*)’: GameHandler.cpp:60:20: error: conversion from ‘std::string* {aka std::basic_string*}’ to non-scalar type ‘std::string {aka std::basic_string}’ requested GameHandler.cpp:67:24: error: cannot convert ‘std::string {aka std::basic_string}’ to ‘const char*’ for argument ‘1’ to ‘int atoi(const char*)’ GameHandler.cpp:71:12: error: invalid conversion from ‘int*’ to ‘int’ [-fpermissive] GameHandler.cpp:61:9: warning: address of local variable ‘values’ returned [enabled by default]