从字符到字符编译器的转换无效错误
Invalid conversion from char to char compiler error
void Emplyee::setname (char Name[50])
{ name [50] = Name [50] ; }
void Emplyee::setadress (char Adress [100])
{ adress [100] = Adress [100] ; }
Number.setname (Name [50]); \ Error in this Line
Number.setadress (Adress [100]); \ Error in this Line
您已将函数参数声明为类型 char[]
,但您正在使用 char 数组的元素调用该函数,该元素的类型为 char
.仅使用Name
和Adress
调用函数
在数组的使用方面,您的代码还有其他问题,但这将修复有问题的编译器错误。它不会解决您将要看到的其他问题......
我会指出其中的一些...
void Emplyee::setname (char Name[50])
{
name [50] = Name [50] ; // this line won't do what you think it does. (look at strcpy...)
// ^ ^
// |-----------|------ also, subscript out of bounds...
}
此外,地址也一样。
char name[50]; // declares an array of chars, called name, with a size of 50 elements
// ...
char c = name[50]; // access the 50th element (out of bounds, btw) of the name array, and assign to c. has nothing to do with size.
name [50] = Name [50] ;
这不会做你认为它做的事情,它会用 Name 的元素 51 覆盖名称的元素 51。它不会复制整个数组,只复制一个元素。这也可能是缓冲区溢出错误。
这整个代码表明你不理解数组,我认为你现在使用 std::string 的麻烦会更少。这是一个基于您编写的内容的简单示例:
#include <string>
class Emplyee{
std::string address, name;
public:
void setname (std::string Name)
{ name = Name ; }
void setadress (std::string Adress)
{ adress = Adress ; }
};
Emplyee Number;
Number.setname ("AAAA");
std::string Address = "Your address here";
Number.setadress (Address);
最后,我想说的是,注释是//而不是 \\,为了保持一致性,最好尽可能尝试并尊重语言的命名约定。
相关文章:
- 如何解决一元"*"(有"字符")错误的无效类型参数?
- 成本字符*类型的非常量引用的无效初始化,并且来自常量字符*类型的临时引用
- 错误:AES 加密密钥:从"char*"到"无符号字符"的转换无效
- 构建 NFIQ2 时从"字符"到"字符*"的转换无效
- 跨平台套接字发送,Linux 上的缓冲区常量无效* Windows上的常量字符*,最佳处理方式
- 二进制表达式的操作数无效("常量字符*"和"常量字符*")
- 答案审查 - 从"常量字符*"到"字符"的转换无效
- 从字符到康斯特* 字符的转换无效,代码有什么问题?
- 如何替换/忽略 C stdio.h getline() 中的无效 Unicode/UTF8 字符
- 如何使用ISTream对象输入多个字符到对象的数组中?(and 0作为哨兵值.下面的我的代码将无效)
- CIN是否将输入流中的无效字符取用
- 从字符 * 到 int 的转换无效
- 如何将常量无效*转换为无符号字符*
- 简短的:从字符到常量字符*的转换无效
- 用户输入逻辑运算符符号或数字,如果输入字符或字符串则无效
- 从"常量字符*"到"字符*"[-允许]的转换无效;VTK-7.1.1 编译错误
- 指针与无效字符合法的比较
- 字符和常量字符[2] 类型的操作数无效到二进制运算符
- 读取文本文件会导致缓冲区末尾的字符无效
- 转换常量无符号字符无效