C++ 错误:将‘char*’分配给‘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 文件中:
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)
{ }
我的错误:
Territory.cpp: 在构造函数中'Territory::Territory(std::string,char*, Player*, char)’: Territory.cpp:15:33: 错误: 不兼容的类型将‘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.
推荐答案
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]
是一个大小为 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;
相反,我必须遍历 foo
的元素并将它们复制到 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
的元素复制到 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::字符串
).
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
).
相关文章