C++ 如何在C++;?
请看一下下面的头文件C++ 如何在C++;?,c++,arrays,visual-studio-2010,byte,C++,Arrays,Visual Studio 2010,Byte,请看一下下面的头文件 #pragma once class MissileLauncher { public: MissileLauncher(void); private: byte abc[3]; }; 这就产生了错误 Error 1 error C2143: syntax error : missing ';' before '*' 我试着这样做 byte *abc; 但它也失败了,同样的错误。然而,我注意到我可以用这种方式调用其他内置的tyes数组,
#pragma once
class MissileLauncher
{
public:
MissileLauncher(void);
private:
byte abc[3];
};
这就产生了错误
Error 1 error C2143: syntax error : missing ';' before '*'
我试着这样做
byte *abc;
但它也失败了,同样的错误。然而,我注意到我可以用这种方式调用其他内置的tyes数组,例如int数组。为什么字节数组会出现这种情况?如何解决这个问题?我想分配cpp文件中的值。有什么想法吗?< /P> < P>字节不是C或C++中的标准类型。试试char,它通常至少有8位长。试试看
class MissileLauncher
{
public:
MissileLauncher(void);
private:
unsigned char abc[3];
};
或
**注意:在较旧的编译器(非C++11)中,使用
行将替换为typedef无符号字符字节代码>如果您只需要一个字节,cstdint中定义的uint8将是最具表现力的
也许您可以利用C++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;
}
我得到:
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
<>你可以使用Qt,如果你不知道,它是C++,还有一组额外的库和类等等。Qt有一个非常方便的QByteArray类,我确信它会满足您的需要
字节不是C/C++中的标准数据类型,但它仍然可以按照我认为您需要的方式使用。方法如下:回想一下,字节是一个8位内存大小,它可以表示-128和127(包括-128和127)之间的任何整数。(该范围内有256个整数;8位可以表示256——2升为8的幂——不同的值)。还记得C/C++中的字符是一个字节(八位)。因此,在C/C++中创建字节数据类型所需做的就是将以下代码放在源文件的顶部:
#定义字节字符
所以你现在可以申报了
字节abc[3] 字节不是C/C++中的标准类型,因此它由char
表示
这样做的一个优点是,您可以将basic_字符串
视为字节数组,以实现安全存储和函数传递。这将帮助您避免在使用各种形式的char[]
和char*
时可能遇到的内存泄漏和分段错误
例如,这将创建一个字符串作为空值的字节数组:
typedef basic_string<unsigned char> u_string;
u_string bytes = u_string(16,'\0');
您提供的代码中没有*
,因此它不可能生成该错误消息。请准确。另外,byte
来自哪里?这不是标准类型。谢谢你的回复。那么,使用char数组?它能像字节一样将值发送到USB端口吗?\当您使用字符时,它正好是一个字节;因此,将“char”发送到C/C++中的USB端口,实际上是发送字节。MichaelPrice也只有C++ 11。“不,C++也可以在以前的C++中使用。”使用而不是。我今天在一些代码中使用它,正如我们所说::-)@KellyBeard-仅在C++11发布后可用(作为标准的可移植方法)。而且不完全是一回事。要不要解释一下否决票?我们每天都在多个生产级系统中使用这种模式,没有内存泄漏。我不是下选程序,但使用字符串存储二进制数据是一种非常糟糕的做法。您依赖于字符串实现来使用8字节字符。在实践中,特别是在现代系统上,许多字符串实现在内部将其字符串编码为UTF-8或UTF-16(可变长度编码)。这意味着您的.length
调用将引用字形数,而不是字节数。如果你在生产系统上使用这个,你应该担心。字符串和字节数组有非常不同的用途。@克里斯,你是说应该使用char
数组来保证8字节字符吗?该解决方案的悲哀之处在于引入了手动内存管理char*
,根据我的经验,这是导致C/C++代码中大约一半内存泄漏的原因char
在技术上不能保证为8位(但通常是这样)。如果您想要更有力的保证,请使用stdint.h
中的uint8\t
。关于手动内存管理,是的,从技术上讲,你所说的是正确的。但是C++的新版本有STD::数组,它将为你抽象内存管理。如果不能使用较新的标准,我认为有一个boost等价物(有一个boost数组
,还有一个boost缓冲区
,两者都处理字节序列)。但是如果我们谈论的是C,是的,手动内存管理是你唯一的选择。@Chris好的,那么这样声明它怎么样:basic\u string bytes
。或者是否有比uint8\t
更好的字节基类型?char
不需要是一个字节char
保证是一个字节sizeof(char)==1
,始终如此。但是“字节”不能保证是8位,问题是字节数组,而不是位数组
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
typedef basic_string<unsigned char> u_string;
u_string bytes = u_string(16,'\0');
u_string otherBytes = "some more chars, which are just bytes";
for(int i = 0; i < otherBytes.length(); i++)
bytes[i%16] ^= (int)otherBytes[i];