不兼容的类型在赋值char? [英] incompatible types in assignment of char?

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

问题描述

我收到此代码的错误。 '在char的赋值中不兼容的类型char [13]'我无法弄清楚如何初始化这些数组并使它工作。基本上,程序采用ISBN代码(4组整数,并在每组数字之间用一个字符串 - ),并验证它们是否正确。该程序使用一个ISBN类和一个加载实际ISBN代码的主函数,并尝试使用类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) {}


$ b b

程序加载这些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代码进入ISBN类,以便它们可以由这些功能中的每一个操作。任何帮助非常感谢!感谢!

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类的一般string-处理需求。

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

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

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