首页 > 解决方案 > Converting a string constant to char

问题描述

I have a class named Player but when building a constructor like the one below, I got a warning that read: ISO C++ forbids converting a string constant to 'char'. Can anyone tell me what this means and how I can fix it?

class Player
{
public:
    Player(char * firstN = "", char * lastN = "");
};

标签: c++

解决方案


ISO C++ 禁止将字符串常量转换为 'char'

我怀疑编译器是这么说的。它可能会说“ISO C++ 禁止从字符串常量转换为 char*”。它们是有区别的。

谁能告诉我这是什么意思

这意味着您正在尝试使用字符串常量初始化指向 char 的指针。正如错误消息所解释的,这是无法完成的,因为字符串常量不能隐式转换为这种类型。

我该如何解决?

您可以将参数类型更改为const char*. 字符串常量隐式转换为该类型。


推荐阅读