17

次のヘッダファイルを見てください

#pragma once

class MissileLauncher
{
public:
    MissileLauncher(void);

private:
    byte abc[3];
};

これによりエラーが発生しました

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

こんな風にしてみました

byte *abc;

しかし、それも失敗しました、同じエラーです。ただし、int 配列の例では、この方法で他の組み込みの tyes 配列を呼び出すことができることに気付きました。なぜこれがバイト配列で起こっているのですか? これを解決するには?cpp ファイルに値を割り当てたいと思います。何か案は?

4

7 に答える 7

26

試す

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 以外) では、using行を次のように置き換えます。typedef unsigned char byte;

于 2013-05-10T19:24:50.720 に答える
14

正確に 1 バイトが必要な場合は、cstdint で定義された uint8_t が最も表現力があります。

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

于 2013-05-10T19:29:24.940 に答える
9

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;
}

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
于 2013-05-10T19:35:09.037 に答える
1

知らない場合は、C++ である Qt を使用して、多数の追加のライブラリとクラスなどを使用できます。Qt には非常に便利な QByteArray クラスがあり、これはあなたのニーズに合っていると確信しています。

http://qt-project.org/

于 2013-12-20T15:24:11.990 に答える