简体   繁体   English

char分配不兼容的类型?

[英]incompatible types in assignment of char?

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. “将char分配给char [13]时出现不兼容的类型”,我不知道如何初始化这些数组并使之工作。 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. 基本上,该程序采用ISBN代码(4组整数,并在每组数字之间使用一个带'-'的字符串组成)并验证它们是否正确。 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. 该程序使用ISBN类和加载实际ISBN代码并尝试使用ISBN类对其进行测试的main函数。 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) {}

The program loads these ISBN numbers and then prints and tests them using the class ISBN in the following way... 程序将加载这些ISBN号,然后以以下方式使用ISBN类打印和测试它们:

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

I'm having trouble converting the ISBN codes into the ISBN class so that they can be operated on by each of these functions. 我在将ISBN代码转换为ISBN类时遇到麻烦,因此它们可以由这些功能中的每一个操作。 Any help much appreciated! 任何帮助,不胜感激! Thanks! 谢谢!

This: 这个:

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 );
}

I would also suggest investigating the std::string class for your general string-processing needs. 我还建议调查一下std :: string类,以解决常规的字符串处理需求。

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

You cannot initialize char[13] by char*. 您不能通过char *初始化char [13]。

You have to manually copy, character-by-character: 您必须手动复制每个字符:

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

The straight-forward way to store strings in C++ is really only C++存储字符串的直接方法实际上只是

const char* group = "Group";

If you need extra string functionality look into the string class . 如果需要额外的字符串功能,请查看string类

In C/C++, you can treat an array as just a pointer to the first element of the array. 在C / C ++中,您可以将数组视为指向数组第一个元素的指针。 So use pointers in your constructor parameters, not arrays. 因此,在构造函数参数而不是数组中使用指针。 Eg 例如

ISBN::ISBN(const char* newisbn)
{
    strcpy(isbn, newisbn);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将char **分配给char *中的类型不兼容 - Incompatible types in assignment char ** to char * 错误:将'char *'分配给'char [20]'时类型不兼容 - error: incompatible types in assignment of 'char*' to 'char [20]' 将'char'赋值给'char [100]'的不兼容类型 - incompatible types in assignment of ‘char’ to ‘char [100]’ 错误:将'char *'分配给'char [4000]'时类型不兼容 - error: incompatible types in assignment of 'char*' to 'char [4000]' 错误:GEANY中将'int'分配给'char [1]'的类型不兼容 - error: incompatible types in assignment of 'int' to 'char [1]' in GEANY 在malloc中将'void *'赋值给'char'的不兼容类型 - incompatible types in assignment of 'void*' to 'char"---— in malloc 错误:将'const char [5]'赋值给'char [10]'时出现不兼容的类型 - Error:incompatible types in assignment of 'const char[5]' to 'char[10]' C ++错误:将'char *'分配给'char [2]时类型不兼容 - C++ Error: Incompatible types in assignment of ‘char*’ to ‘char [2] “在'char'到'char'[100]的赋值中不兼容的类型”我在使用strcat吗? - “incompatible types in assignment of 'char' to 'char'[100]” am I using strcat right? 将“ int”分配给“ int [2]”的类型不兼容 - Incompatible types in assignment of 'int' to 'int [2]'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM