C ++错误:在'char *'到'char [2]的赋值中不兼容的类型 [英] C++ Error: Incompatible types in assignment of ‘char*’ to ‘char [2]
问题描述
我有一个问题,我的构造函数。
在我的头文件中我声明:
I have a bit of a problem with my constructor. In my header file I declare:
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>
My error:
Territory.cpp:在构造函数'Territory :: Territory(std :: string,
char *,Player *,char) ':Territory.cpp:15:33:error:incompatible types
在'char *'到'char [2]'的赋值
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]’
我已经知道 char [2]< => char *
但我不知道如何处理这个关于我的构造函数和get / setters。
I already figured out that char[2] <=> char*
but I'm not sure how to handle this about my constructor and get/setters.
推荐答案
p> C ++中的原始数组有点恼人,充满了危险。这是为什么除非你有一个很好的理由,你应该使用 std :: vector
或 std :: array
。
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
.
首先,如其他人所说, char [2]
不同于 char *
,或至少不是通常。 char [2]
是 char
和 char *
是指向 char
的指针。他们经常会感到困惑,因为数组会衰减到指向第一个元素的指针,只要他们需要。所以这个工作:
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;
但反之则不然:
const char* bar = "hello";
const char foo[6] = bar; // ERROR
添加到混淆,当声明函数参数时, char [ ]
等效于 char *
。所以在你的构造函数中,参数 char short_name [2]
真的是 char * short_name
。
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;
$ b c>并将它们复制到 bar
中,或者使用一些函数,例如 std :: copy
:
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));
所以在你的构造函数中,你必须手动复制 short_name
into
short_name _
:
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));
}
正如你可以看到,这是非常讨厌,所以除非你有一个非常好的原因你只是应该使用 std :: vector
而不是原始数组(或在这种情况下可能 std :: string
)。
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屋!