C ++错误:赋值为'char *'到'char [2]的不兼容类型 [英] C++ Error: Incompatible types in assignment of ‘char*’ to ‘char [2]

查看:1386
本文介绍了C ++错误:赋值为'char *'到'char [2]的不兼容类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的构造函数有一点问题。
在我的头文件中我声明:

  char short_name_ [2]; 




  • 和其他变量



在我的构造函数中:

  Territory(std :: string name,char short_name [2],Player * owner,char units); 
void setShortName(char * short_name);
inline const char(& getShortName()const)[2] {return short_name_;

在我的cpp文件中:

  Territory :: Territory(std :: string name,char short_name [2],Player * owner,
char units):name_(name),short_name_(short_name),
owner_(owner),units_(units)
{}

我的错误: / p>


Territory.cpp:在构造函数Territory :: Territory(std :: string,
char *,Player *,char) ':Territory.cpp:15:33:错误:不兼容的类型
赋值为'char *'到'char [2]'


我已经弄清楚 char [2]< => char * 但我不知道如何处理这个关于我的构造函数和get / setter。

解决方案

p> C ++中的原始数组是一种恼人的,充满危险的。这就是为什么除非你有一个很好的理由,你应该使用 std :: vector std :: array



如其他人所说,首先, char [2] char * ,或者至少不是通常的。 char [2] char char * 是指向 char 的指针。他们经常感到困惑,因为数组会在需要时衰减到指向第一个元素的指针。所以这样做:

  char foo [2]; 
char * bar = foo;

但反之亦然:

  const char * bar =hello; 
const char foo [6] = bar; // ERROR

在声明函数参数时,添加到混淆, char [ ] 相当于 char * 。所以在你的构造函数中,参数 char short_name [2] 真的是 char * short_name



数组的另一个奇怪之处在于它们不能像其他类型一样复制(这是为什么函数参数中的数组被视为指针的一个解释)。所以例如我可以这样做:

  char foo [2] = {' a','b'}; 
char bar [2] = foo;

相反,我必须迭代 foo 并将它们复制到 bar 中,或者使用一些对我来说的功能,例如 std :: copy

  char foo [2] = {'a','b'}; 
char bar [2];
// std :: begin和std :: end仅在C ++ 11中可用
std :: copy(std :: begin(foo),std :: end(foo),std: :开始(巴));

所以在你的构造函数中你必须手动复制 short_name into short_name _

  Territory :: Territory std :: string name,char * short_name,Player * owner,
char units):name_(name),owner_(owner),units_(units)
{
//注意std :: begin和std :: end可以*不*用于指针。
std :: copy(short_name,short_name + 2,std :: begin(short_name));
}

你可以看到这一切都很烦人,所以除非你有一个非常很好的理由,你应该使用 std :: vector 而不是原始数组(或在这种情况下可能 std :: string )。


I have a bit of a problem with my constructor. In my header file I declare:

char short_name_[2]; 

  • and other variables

In my constructor:

Territory(std::string name, char short_name[2], Player* owner, char units);
void setShortName(char* short_name);
inline const char (&getShortName() const)[2] { return short_name_; }

In my cpp file:

Territory::Territory(std::string name, char short_name[2], Player* owner, 
                     char units) : name_(name), short_name_(short_name), 
                    owner_(owner), units_(units)
{ }

My error:

Territory.cpp: In constructor ‘Territory::Territory(std::string, char*, Player*, char)’: Territory.cpp:15:33: error: incompatible types in assignment of ‘char*’ to ‘char [2]’

I already figured out that char[2] <=> char* but I'm not sure how to handle this about my constructor and get/setters.

解决方案

Raw arrays in C++ are kind of annoying and fraught with peril. This is why unless you have a very good reason to you should use std::vector or std::array.

First off, as others have said, char[2] is not the same as char*, or at least not usually. char[2] is a size 2 array of char and char* is a pointer to a char. They often get confused because arrays will decay to a pointer to the first element whenever they need to. So this works:

char foo[2];
char* bar = foo;

But the reverse does not:

const char* bar = "hello";
const char foo[6] = bar; // ERROR

Adding to the confusion, when declaring function parameters, char[] is equivalent to char*. So in your constructor the parameter char short_name[2] is really char* short_name.

Another quirk of arrays is that they cannot be copied like other types (this is one explanation for why arrays in function parameters are treated as pointers). So for example I can not do something like this:

char foo[2] = {'a', 'b'};
char bar[2] = foo;

Instead I have to iterate over the elements of foo and copy them into bar, or use some function which does that for me such as std::copy:

char foo[2] = {'a', 'b'};
char bar[2];
// std::begin and std::end are only available in C++11
std::copy(std::begin(foo), std::end(foo), std::begin(bar));

So in your constructor you have to manually copy the elements of short_name into short_name_:

Territory::Territory(std::string name, char* short_name, Player* owner, 
                     char units) : name_(name), owner_(owner), units_(units)
{ 
    // Note that std::begin and std::end can *not* be used on pointers.
    std::copy(short_name, short_name + 2, std::begin(short_name));
}

As you can see this is all very annoying, so unless you have a very good reason you just should use std::vector instead of raw arrays (or in this case probably std::string).

这篇关于C ++错误:赋值为'char *'到'char [2]的不兼容类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆