不兼容的类型中的char分配? [英] incompatible types in assignment of char?

查看:232
本文介绍了不兼容的类型中的char分配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到一个错误与此code。 不兼容类型的字符分配到char [13]我无法弄清楚如何初始化这些阵列,使这项工作。基本上,计划采取ISBN codeS(4组的整数,使用一个字符串 - 他们中的每一组数字之间),并验证它们是正确的。该方案采用了类ISBN和加载实际ISBN codeS和尝试使用类ISBN来测试他们的主要功能。下面是我。

I am getting an error with this code. 'Incompatible types in assignment of char to char[13]' I can't figure out how to initialize these arrays and make this work. Basically, the program takes ISBN codes (4 groups of integers and makes one string with '-' in them between each group of numbers) and verifies that they are correct. The program uses a class ISBN and a main function that loads the actual ISBN codes and tries to use the class ISBN to test them. Here is what I have.

class ISBN {
private:
char group[6];                          
char publisher[8];                     
char book[8];                      
char check;  
char isbn[13];
char compute_check();

public:
ISBN();
ISBN(char newisbn[]);             
ISBN(char group[ ], char publisher[ ], char book[ ], char check);                                       
bool valid();                           
char *getpublisher();                  
void print(ostream &o);                 
};

ISBN::ISBN(char newisbn[]) : isbn(newisbn) {}

程序加载这些ISBN号,然后打印并使用类ISBN以下列方式测试他们......

The program loads these ISBN numbers and then prints and tests them using the class ISBN in the following way...

strcpy(isbns[index++], "1-57676-074-X");
ISBN isbn(isbns[i]);
isbn.print(cout);
if (isbn.valid())

我无法转换ISBN codeS到ISBN类,这样他们可以通过每个功能进行操作。任何帮助非常AP preciated!谢谢!

I'm having trouble converting the ISBN codes into the ISBN class so that they can be operated on by each of these functions. Any help much appreciated! Thanks!

推荐答案

ISBN::ISBN(char newisbn[]) : isbn(newisbn) {}

不会做你想要的。尽管你可能已经被告知什么,数组的指针不相同 - 这里的构造函数采用指针(伪装成一个数组),并试图用它来初始化一个实际的数组。您需要:

doesn't do what you want. Despite what you may have been told, arrays are not identical with pointers - the constructor here is taking a pointer (disguised as an array) and trying to use it to initialise an actual array. You need:

ISBN::ISBN(char newisbn[]) {
   strcpy( isbn, newisbn );
}

我也建议调查为您一般的字符串处理需求std :: string类。

I would also suggest investigating the std::string class for your general string-processing needs.

这篇关于不兼容的类型中的char分配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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