本文介绍了来自'char'tp'char *'的转换无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我是C ++的新手,并试图开始制作一个可以随机播放 数组的脚本。有人可以告诉我有什么问题吗? #include< iostream.h> #include< string.h> int main(){ srand(time(0)); int array_length; int count; int randm; char temp [30]; cout<< 阵列中有多少项? " ;; cin>> array_length; char items [30] [array_length + 1]; for(count = 0; count< = array_length; count ++){ if(count!= 0){ cout<< \\\What shall item <<计数<< "是?\ n\t" ;; } cin.getline(items [count],30); } count = 0; count< array_length; count ++){ randm = rand()%array_length; strcpy(temp [30] ,项目[30] [count]); //错误 strcpy(项目[30] [计数],项目[30] [randm]); // ERROR strcpy(items [30] [randm],temp [30]); //错误 } 返回0; } 有人可以告诉我有什么问题吗?我得到的错误是: 21:错误:无效转换为''char''到''char *'' 21:错误:转换无效'' char''to''const char *'' 22:错误:无效转换为''char''到''char *'' 22:错误:无效转换从''char''到''const char *'' 23:错误:从''char''无效转换为''char *'' 23:错误:无效转换从''char''到''const char *''I''m new to C++, and tried to start making a script that will shuffle anarray. Can someone please tell me what''s wrong?#include <iostream.h>#include <string.h>int main () {srand(time(0));int array_length;int count;int randm;char temp[30];cout << "How many items in array? ";cin >> array_length;char items [30][array_length + 1];for (count = 0; count <= array_length; count++) {if (count != 0) {cout << "\nWhat shall item " << count << " be?\n\t";}cin.getline (items[count], 30);}for (count = 0; count < array_length; count++) {randm = rand() % array_length;strcpy (temp[30], items[30][count]);//ERRORstrcpy (items[30][count], items[30][randm]); //ERRORstrcpy (items[30][randm], temp[30]); //ERROR}return 0;}Can someone please tell me what is wrong? The errors I get are:21: error: invalid conversion from ''char'' to ''char*''21: error: invalid conversion from ''char'' to ''const char*''22: error: invalid conversion from ''char'' to ''char*''22: error: invalid conversion from ''char'' to ''const char*''23: error: invalid conversion from ''char'' to ''char*''23: error: invalid conversion from ''char'' to ''const char*''推荐答案 < snip> 我不确定出了什么问题(我无法理解)但是这里有点 随机程序: int main() { //用于保存项目的字符串向量 std :: vector< std :: string>物品; //获取物品 std :: cout<< 输入项目,或空白以完成: << std :: endl; for(;;) { std :: string item; std :: getline(std :: cin,item); if(item =="") break; items。 push_back(item); } std :: cout<< std :: endl<< 洗牌物品: << std :: endl; //使用< algorithm>中的random_shuffle算法shuffe序列 std :: random_shuffle(items.begin(),items.end()); //将混洗后的向量复制到std :: cout std :: copy(items.begin(),items.end(), std :: ostream_iterator< std :: string>(std :: cout," \ n")); } 此外,我建议您阅读免费&合法的用C ++思考布鲁斯的书 Eckel,可在作者的网站上找到, www。 mindview.net 。 - Pete<snip>I''m not sure what was wrong (I couldn''t understand it) but here''s a littleshuffle program:int main(){// a vector of strings to hold the itemsstd::vector<std::string> items;// get itemsstd::cout << "Enter items, or blank to finish:" << std::endl;for(;;){std::string item;std::getline(std::cin, item);if(item == "")break;items.push_back(item);}std::cout << std::endl << "Shuffled items:" << std::endl;// use the random_shuffle algorith in <algorithm> to shuffe the sequencestd::random_shuffle(items.begin(), items.end());// copy the shuffled vector to std::coutstd::copy(items.begin(), items.end(),std::ostream_iterator<std::string>(std::cout, "\n"));}Also, I suggest you read the free & legal "Thinking in C++" book by BruceEckel, availible at the author''s website, www.mindview.net.- Pete < snip> 我不确定出了什么问题(我无法理解)但是这里有点 随机程序: int main() { //用于保存项目的字符串向量 std :: vector< std :: string>物品; //获取物品 std :: cout<< 输入项目,或空白以完成: << std :: endl; for(;;) { std :: string item; std :: getline(std :: cin,item); if(item =="") break; items。 push_back(item); } std :: cout<< std :: endl<< 洗牌物品: << std :: endl; //使用< algorithm>中的random_shuffle算法shuffe序列 std :: random_shuffle(items.begin(),items.end()); //将混洗后的向量复制到std :: cout std :: copy(items.begin(),items.end(), std :: ostream_iterator< std :: string>(std :: cout," \ n")); } 此外,我建议您阅读免费&合法的用C ++思考布鲁斯的书 Eckel,可在作者的网站上找到, www。 mindview.net 。 - Pete<snip>I''m not sure what was wrong (I couldn''t understand it) but here''s a littleshuffle program:int main(){// a vector of strings to hold the itemsstd::vector<std::string> items;// get itemsstd::cout << "Enter items, or blank to finish:" << std::endl;for(;;){std::string item;std::getline(std::cin, item);if(item == "")break;items.push_back(item);}std::cout << std::endl << "Shuffled items:" << std::endl;// use the random_shuffle algorith in <algorithm> to shuffe the sequencestd::random_shuffle(items.begin(), items.end());// copy the shuffled vector to std::coutstd::copy(items.begin(), items.end(),std::ostream_iterator<std::string>(std::cout, "\n"));}Also, I suggest you read the free & legal "Thinking in C++" book by BruceEckel, availible at the author''s website, www.mindview.net.- Pete 不是我是专家但你似乎试图传递一个单独的段 的数组而不是全部数组,摆脱''[30]''。另外,我不认为你可以通过一个二维数组来strcpy。Not that I''m a expert but you appear to be trying to pass a single segmentof the array instead of all of the array, get rid of the ''[30]''. Also Idon''t think you can pass a two dimentional array to strcpy. 这篇关于来自'char'tp'char *'的转换无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-29 22:46