来自'char'tp'char *'的转换无效 [英] Invalid conversion from 'char' tp 'char*'

查看:57
本文介绍了来自'char'tp'char *'的转换无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是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 an
array. 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]);
//ERROR
strcpy (items[30][count], items[30][randm]); //ERROR
strcpy (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*''

推荐答案

Tim Johansson写道:
Tim Johansson wrote:
我是C ++的新手,并试图开始制作一个脚本
^^^^^^

仅供参考,C ++程序不是脚本,它们是' '重新编程。

将改组阵列。有人可以告诉我什么是错的吗?
I''m new to C++, and tried to start making a script ^^^^^^
FYI, C++ programs are not "scripts", they''re programs.
that will shuffle
an array. Can someone please tell me what''s wrong?



< 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 little
shuffle program:

int main()
{
// a vector of strings to hold the items
std::vector<std::string> items;

// get items
std::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 sequence
std::random_shuffle(items.begin(), items.end());

// copy the shuffled vector to std::cout
std::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 Bruce
Eckel, availible at the author''s website, www.mindview.net.

- Pete


Tim Johansson写道:
Tim Johansson wrote:
I 我是C ++的新手,并试图开始编写脚本
^^^^^^

仅供参考,C ++程序不是脚本,它们是程序。 br />
会洗牌一个阵列。有人可以告诉我什么是错的吗?
I''m new to C++, and tried to start making a script ^^^^^^
FYI, C++ programs are not "scripts", they''re programs.
that will shuffle
an array. Can someone please tell me what''s wrong?



< 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 little
shuffle program:

int main()
{
// a vector of strings to hold the items
std::vector<std::string> items;

// get items
std::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 sequence
std::random_shuffle(items.begin(), items.end());

// copy the shuffled vector to std::cout
std::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 Bruce
Eckel, availible at the author''s website, www.mindview.net.

- Pete




" Tim Johansson" < SP ** @ gurka.se>在留言中写道

news:c5 ************* @ ID-224346.news.uni-berlin.de ...

"Tim Johansson" <sp**@gurka.se> wrote in message
news:c5*************@ID-224346.news.uni-berlin.de...
我是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 <<计数<< "对于(count = 0; count< array_length)来说,cin.getline(items [count],30);
}
是什么?\ n \> ;;
}
; count ++){
randm = rand()%array_length;
strcpy(temp [30],items [30] [count]);
// ERROR
strcpy(items [30] [count],items [30] [randm]); // ERROR
strcpy(items [30] [randm],temp [30]);
//错误}
返回0;
}

有人可以告诉我有什么问题吗?我得到的错误是:
21:错误:从''char''无效转换为''char *''
21:错误:从''char''转换为''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 an
array. 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]);
//ERROR
strcpy (items[30][count], items[30][randm]); //ERROR
strcpy (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*''




不是我是专家但你似乎试图传递一个单独的段

的数组而不是全部数组,摆脱''[30]''。另外,我不认为你可以通过一个二维数组来strcpy。



Not that I''m a expert but you appear to be trying to pass a single segment
of the array instead of all of the array, get rid of the ''[30]''. Also I
don''t think you can pass a two dimentional array to strcpy.


这篇关于来自'char'tp'char *'的转换无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆