2013-05-10 420 views
7

請看看在followng頭文件如何在C++中創建一個字節數組?

#pragma once 

class MissileLauncher 
{ 
public: 
    MissileLauncher(void); 

private: 
    byte abc[3]; 
}; 

此產生的錯誤

Error 1 error C2143: syntax error : missing ';' before '*' 

我試圖做這樣

byte *abc; 

,但它也失敗了,同樣的錯誤。但是,我注意到我可以以這種方式調用其他內置的tyes數組,例如一個int數組。爲什麼這發生在字節數組中?如何解決這個問題?我想分配cpp文件中的值。有任何想法嗎?

+2

有沒有''你在場,所以它不可能產生錯誤信息的代碼。請準確。 – delnan 2013-05-10 19:22:22

+2

另外,'byte'來自哪裏?這不是一個標準類型。 – 2013-05-10 19:22:37

回答

17

嘗試

class MissileLauncher 
{ 
public: 
    MissileLauncher(void); 

private: 
    unsigned char abc[3]; 
}; 

using byte = unsigned char; 

class MissileLauncher 
{ 
public: 
    MissileLauncher(void); 

private: 
    byte abc[3]; 
}; 

**注:在舊的編譯器(非C++ 11)如果你想要的是一個與typedef unsigned char byte;

+0

'char'不一定是一個字節 – Yola 2017-07-17 14:52:47

7

字節不是C或C++中的標準類型。嘗試char,通常至少8位長。

+0

感謝您的回覆。那麼,使用char數組?它可以像字節一樣將值發送到USB端口嗎?\ – Soldier 2013-05-10 19:25:57

+1

當您使用char時,它恰好是一個字節;所以發送'char'到C/C++中的USB端口實際上是發送一個字節。 – 2013-05-10 19:26:57

+0

非常感謝!來自我的+1! – Soldier 2013-05-10 19:31:19

10

更換using線字節,在cstdint中定義的uint8_t將是最具表現力的。

http://www.cplusplus.com/reference/cstdint/

+0

另外,C++ 11似乎只有它。 – 2013-05-10 19:35:17

+0

@MichaelPrice,不,它在以前的C++中也是可用的。使用而不是。我們今天用一些代碼來使用它。 :-) – 2015-02-25 14:41:17

+0

@KellyBeard - 僅在C++ 11出貨後纔可用(作爲標準的便攜式方法)。 並不完全一樣。 – 2015-05-02 03:19:44

6

也許你可以利用在C++ 11的可用std::bitset類型。它可以用來表示一個固定的N位序列,可以用常規邏輯來操縱。

#include<iostream> 
#include<bitset> 

class MissileLauncher { 
public: 
    MissileLauncher() {} 
    void show_bits() const { 
    std::cout<<m_abc[2]<<", "<<m_abc[1]<<", "<<m_abc[0]<<std::endl; 
    } 

    bool toggle_a() { 
    // toggles (i.e., flips) the value of `a` bit and returns the 
    // resulting logical value 
    m_abc[0].flip(); 
    return m_abc[0]; 
    } 

    bool toggle_c() { 
    // toggles (i.e., flips) the value of `c` bit and returns the 
    // resulting logical value 
    m_abc[2].flip(); 
    return m_abc[2]; 
    } 

    bool matches(const std::bitset<3>& mask) { 
    // tests whether all the bits specified in `mask` are turned on in 
    // this instance's bitfield 
    return ((m_abc & mask) == mask); 
    } 

private: 
    std::bitset<3> m_abc; 
}; 

typedef std::bitset<3> Mask; 
int main() { 
    MissileLauncher ml; 

    // notice that the bitset can be "built" from a string - this masks 
    // can be made available as constants to test whether certain bits 
    // or bit combinations are "on" or "off" 
    Mask has_a("001");  // the zeroth bit 
    Mask has_b("010");  // the first bit 
    Mask has_c("100");  // the second bit 
    Mask has_a_and_c("101"); // zeroth and second bits 
    Mask has_all_on("111"); // all on! 
    Mask has_all_off("000"); // all off! 

    // I can even create masks using standard logic (in this case I use 
    // the or "|" operator) 
    Mask has_a_and_b = has_a | has_b; 
    std::cout<<"This should be 011: "<<has_a_and_b<<std::endl; 

    // print "true" and "false" instead of "1" and "0" 
    std::cout<<std::boolalpha; 

    std::cout<<"Bits, as created"<<std::endl; 
    ml.show_bits(); 
    std::cout<<"is a turned on? "<<ml.matches(has_a)<<std::endl; 
    std::cout<<"I will toggle a"<<std::endl; 
    ml.toggle_a(); 
    std::cout<<"Resulting bits:"<<std::endl; 
    ml.show_bits(); 
    std::cout<<"is a turned on now? "<<ml.matches(has_a)<<std::endl; 
    std::cout<<"are both a and c on? "<<ml.matches(has_a_and_c)<<std::endl; 
    std::cout<<"Toggle c"<<std::endl; 
    ml.toggle_c(); 
    std::cout<<"Resulting bits:"<<std::endl; 
    ml.show_bits();  
    std::cout<<"are both a and c on now? "<<ml.matches(has_a_and_c)<<std::endl; 
    std::cout<<"but, are all bits on? "<<ml.matches(has_all_on)<<std::endl; 
    return 0; 
} 

編譯使用gcc 4.7.2

g++ example.cpp -std=c++11 

我得到:

This should be 011: 011 
Bits, as created 
false, false, false 
is a turned on? false 
I will toggle a 
Resulting bits: 
false, false, true 
is a turned on now? true 
are both a and c on? false 
Toggle c 
Resulting bits: 
true, false, true 
are both a and c on now? true 
but, are all bits on? false 
+0

哇..非常感謝您的回覆!來自我的+1! – Soldier 2013-05-10 20:01:00

1

你可以使用Qt它,如果你不知道,是C++和一幫額外的圖書館和類和什麼。 Qt有一個非常方便的QByteArray類,我相當肯定會滿足您的需求。

http://qt-project.org/

2

字節是不是在C/C++標準數據類型,但它仍然可以使用你想要的,我知道的方式。以下是方法:回想一下,一個字節是一個八位內存大小,它可以表示-128到127之間的任何整數,包括這兩個數字。 (在該範圍內有256個整數; 8位可以代表256個 - 兩個提升到8個功率 - 不同的值)。還要回顧一下,C/C++中的字符是一個字節(8位)。因此,在C/C++中使用字節數據類型所需的一切就是將此代碼放在源文件的頂部: #define byte char 因此,您現在可以聲明 byte abc [3];

-1

字節在C/C++中不是標準類型,因此它由char表示。

這樣做的一個優點是您可以將basic_string視爲允許安全存儲和函數傳遞的字節數組。這將幫助您避免使用各種形式的char[]char*時可能遇到的內存泄漏和分段錯誤。

例如,這產生一個字符串作爲空值的字節數組:

typedef basic_string<unsigned char> u_string; 

u_string bytes = u_string(16,'\0'); 

這允許與其他char值,包括那些存儲在其他string變量標準位運算。例如,異或的另一個u_stringchar值跨越bytes

u_string otherBytes = "some more chars, which are just bytes"; 
for(int i = 0; i < otherBytes.length(); i++) 
    bytes[i%16] ^= (int)otherBytes[i]; 
+0

謹慎解釋倒票?我們每天都在多個生產級系統中使用這種模式,並且沒有內存泄漏。 – 2016-03-11 17:46:35

+0

我不是downvoter,但使用字符串來存儲二進制數據是一個非常糟糕的做法。您依賴字符串實現來使用8字節字符。在實踐中,特別是在現代系統中,許多字符串實現在內部將它們的字符串編碼爲UTF-8或UTF-16(可變長度編碼)。這意味着你的'.length'調用將引用字形的數量,而不是字節的數量。如果你在生產系統上使用它,你應該擔心。字符串和字節數組具有非常不同的用途。 – 2016-09-08 20:22:49

+0

@Chris那麼你是說應該使用'char'數組來保證8字節的字符?關於該解決方案的令人傷心的事情是引入了'char *'的手動內存管理,根據我的經驗,這是C/C++代碼中大約一半內存泄漏的原因。 – 2016-09-08 20:40:24