如何在C++中创建一个字节数组?

Sol*_*ier 9 c++ arrays byte visual-studio-2010

请查看followng头文件

#pragma once

class MissileLauncher
{
public:
    MissileLauncher(void);

private:
    byte abc[3];
};
Run Code Online (Sandbox Code Playgroud)

这产生了错误

Error   1   error C2143: syntax error : missing ';' before '*'  
Run Code Online (Sandbox Code Playgroud)

我试着这样做

byte *abc;
Run Code Online (Sandbox Code Playgroud)

但它也失败了,同样的错误.但是,我注意到我可以用这种方式调用其他内置的tyes数组,例如一个int数组.为什么这会发生在字节数组中?怎么解决这个?我想在cpp文件中分配值.有任何想法吗?

Mic*_*ice 20

尝试

class MissileLauncher
{
public:
    MissileLauncher(void);

private:
    unsigned char abc[3];
};
Run Code Online (Sandbox Code Playgroud)

要么

using byte = unsigned char;

class MissileLauncher
{
public:
    MissileLauncher(void);

private:
    byte abc[3];
};
Run Code Online (Sandbox Code Playgroud)

**注意:在较旧的编译器(非C++ 11)中替换该usingtypedef unsigned char byte;

  • `char` 保证是一个字节;`sizeof(char) == 1`,总是。但是“字节”不能保证是 8 位。 (3认同)

Tra*_*ork 13

如果你只需要一个字节,cstdint中定义的uint8_t将是最具表现力的.

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


Joe*_*oel 6

字节不是C或C++中的标准类型.尝试char,通常至少8位长.


Esc*_*alo 6

也许你可以利用std::bitsetC++ 11中提供的类型.它可以用于表示固定的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;
}
Run Code Online (Sandbox Code Playgroud)

使用gcc编译4.7.2

g++ example.cpp -std=c++11
Run Code Online (Sandbox Code Playgroud)

我明白了:

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
Run Code Online (Sandbox Code Playgroud)