[英]I have an error in my code. Something with pointers I can't seem to fix it
I was practicing pointers and classes and I tried to print the members of the class after I put a value.我正在练习指针和类,并尝试在输入值后打印 class 的成员。 But an error appears that's something to do with pointers and the char array.但是出现与指针和 char 数组有关的错误。
Error (active) E0167 argument of type "const char *" is incompatible with parameter of type "char *"错误(主动)E0167 “const char *”类型的参数与“char *”类型的参数不兼容
Error (active) E0167 argument of type "const char *" is incompatible with parameter of type "char *"错误(主动)E0167 “const char *”类型的参数与“char *”类型的参数不兼容
Error C2664 'void car::setName(char [])': cannot convert argument 1 from 'const char [4]' to 'char []'错误 C2664“void car::setName(char [])”:无法将参数 1 从“const char [4]”转换为“char []”
Error C2664 'void car::setColor(char [])': cannot convert argument 1 from 'const char [4]' to 'char []'错误 C2664“void car::setColor(char [])”:无法将参数 1 从“const char [4]”转换为“char []”
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
class car {
private:
char name[15];
char color[10];
int maxspeed;
int model;
public:
void setName(char n[])
{
strcpy_s(name, n);
}
void setColor(char n[])
{
strcpy_s(color, n);
}
void setMaxspeed(int m)
{
maxspeed = m;
}
void setModel(int m)
{
model = m;
}
char* getName()
{
return name;
}
char* getcolor()
{
return color;
}
int getMaxspeed()
{
return maxspeed;
}
int getModel()
{
return model;
}
void print()
{
cout << "name = " << name << "\n"
<< "color = " << color << "\n"
<< "Maxspeed = " << maxspeed << "\n"
<< "Model = " << model << "\n";
}
};
int main()
{
car x;
x.setName("kia");
x.setColor("red");
x.setMaxspeed(300);
x.setModel(2017);
x.print();
return 0;
}
Those are the errors I got ( https://i.stack.imgur.com/xTx7E.png )这些是我得到的错误( https://i.stack.imgur.com/xTx7E.png )
It's telling you the problem fairly directly.它相当直接地告诉你问题所在。 You're trying to pass string literals to setName
and setColor
.您正在尝试将字符串文字传递给setName
和setColor
。 To be compatible with a string literal, you need an argument of type char const *
(or equivalently, const char *
).要与字符串文字兼容,您需要一个类型为char const *
的参数(或等效的const char *
)。 But what you've used is char []
, which (in the case of a function argument) is equivalent to char *
.但是您使用的是char []
,它(在 function 参数的情况下)等效于char *
。
So, change the arguments to char const *
:因此,将 arguments 更改为char const *
:
void setName(char const *n)
{
strcpy_s(name, n);
}
Once you've done that, look up std:string
, and (unless required for homework or similar) quit using raw pointers to char
.完成后,查找std:string
,并且(除非家庭作业或类似作业需要)停止使用指向char
的原始指针。
In C++, char*
(or char[]
, as you wrote it) is a different type than const char*
and string literals are of the latter.在 C++ 中, char*
(或char[]
,如您所写)是与const char*
不同的类型,字符串文字属于后者。 You can't pass a const char*
to a function expecting char*
.您不能将const char*
传递给期望char*
的 function。
If you rewrite your functions to take const char*
or const char[]
, it'll work:如果您重写函数以采用const char*
或const char[]
,它将起作用:
void setColor(const char n[])
// ...
void setColor(const char n[])
// ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.