2013-04-09 376 views
7

我的構造函數有點問題。 在我的頭文件我宣佈: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) 
{ } 

我的錯誤:

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*,但我不知道如何處理這對我的構造函數和獲取/ setter方法。

+1

'我已經想出了char [2] <=> char *'不是真的。 – Rapptz 2013-04-09 23:23:45

+0

但我認爲C++編譯器是char [2]相當於char *?!我真的不知道如何初始化這個構造函數和獲取者... – vicR 2013-04-09 23:30:32

+1

數組和指針是*非常*不同的東西。閱讀[comp.lang.c常見問題](http://www.c-faq.com/)的第6部分; C和C++在這方面的規則基本相同。 – 2013-04-09 23:37:21

回答

12

在C++中的原始數組是一種討厭和充滿危險。這就是爲什麼除非你有很好的理由,你應該使用std::vectorstd::array

首先,像其他人所說的那樣,char[2]char*不一樣,或者至少不是通常的。 char[2]char的大小2陣列,而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 and std::end are only available in C++11 
std::copy(std::begin(foo), std::end(foo), std::begin(bar)); 

所以在構造函數你有short_name元素手動複製到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)。

2

當一個函數想要一個數組作爲參數時,它會得到一個指向數組第一個元素的指針。該指針不能用於初始化數組,因爲它是一個指針,而不是數組。

您可以編寫接受引用到數組作爲參數的函數:

void i_dont_accept_pointers(const char (array&)[2]) {} 

是這裏的問題,這個數組引用不能用於初始化另一個數組。

class Foo { 
    char vars[2]; 
    Foo(const char (args&)[2]) 
    : vars(args) // This will not work 
    {} 
}; 

引入std::array到eliminiate這個和數組的其他問題C++ 11。在較舊的版本中,您將不得不遍歷數組元素並單獨複製它們或使用std::copy

相關問題