简体   繁体   English

我的代码有错误。 有指针的东西我似乎无法修复它

[英]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 .您正在尝试将字符串文字传递给setNamesetColor 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.

相关问题 我的代码中出现以下错误。 有人能帮我修一下吗 - i got the following error in my code. can some help me fix it 我最近更改了编译器路径以运行 c++ 代码,但现在我无法运行任何 python 代码。 我该如何解决? - I recently changed my compiler path to run c++ code, but now I can't run any python code. How do I fix this? 我的指针似乎消失了 - I seem to have dissapearing pointers 您好,我的代码中有语法问题。 你能帮助我吗? - Greetings, I have a syntax problem in my code. Can you help me? 我有一个向量,它包含指向通过接口访问的 2 个子类型的元素的指针。 似乎无法重新排列向量 - I have a vector that has pointers to elements from 2 subtypes accessed via an interface. Can't seem to re-arrange the vector 我的代码中有错误,可能是语法错误或我找不到的 if 语句。 编程新手 - I have an error within my code, probably syntax or with the if statements that I can't find. New to programming VC ++ LNK2019错误我似乎无法修复 - VC++ LNK2019 Error I can't seem to fix 我不断收到错误LNK2019,似乎无法弄清楚如何解决它 - I keep getting error LNK2019, can't seem to figure out how to fix it 有没有办法解决分段错误? 我似乎无法弄清楚 - Is there a way to fix the segmentation fault ? I can't seem to figure it out 如何解决代码中的“无效的内存引用”错误? - How can I fix the “Invalid memory reference” error in my code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM