我正在研发一个只有2KB SRAM的微控制器,并且迫切需要节省一些内存.试图找出如何使用位域将8 0
/ 1
值放入单个字节但不能完全解决的问题.
struct Bits
{
int8_t b0:1, b1:1, b2:1, b3:1, b4:1, b5:1, b6:1, b7:1;
};
int main(){
Bits b;
b.b0 = 0;
b.b1 = 1;
cout << (int)b.b0; // outputs 0, correct
cout << (int)b.b1; // outputs -1, should be outputting 1
}
Run Code Online (Sandbox Code Playgroud)
是什么赋予了?
我试图operator<<
作为成员函数重载.如果只是这样做它的工作原理:
friend ostream& operator<<(ostream& os, const MyClass& myClass);
在我的头文件和我的MyClass.cc文件中:
ostream& operator<<(ostream& os, const MyClass& myClass)
{
return myClass.print(os);
}
Run Code Online (Sandbox Code Playgroud)
但是,如果我尝试friend
取消并使其成为成员函数,那么它抱怨operator<<
只能采用一个参数.为什么?
ostream& MyClass::operator<<(ostream& os, const MyClass& myClass)
{
return myClass.print(os);
}
Run Code Online (Sandbox Code Playgroud)
我在这个问题上读到它不能成为一个成员函数,但不确定为什么?
我一直在这里阅读一两个小时的问题关于我得到的这个错误,他们中的大多数都忘了#include string(我已经做过),或者重载<<运算符.
这是有问题的代码:
void Student::getCoursesEnrolled(const vector<Course>& c)
{
for (int i = 0; i < c.size(); i++)
{
cout << c[i] << endl;
}
}
Run Code Online (Sandbox Code Playgroud)
而我得到的错误:
Error: No operator matches these operands
operand types are: std::ostream << const Course
Run Code Online (Sandbox Code Playgroud)
我要做的就是返回向量.我读过关于重载<<运算符但是我们没有在课堂上学到任何这些,所以我假设有另一种方法可以做到这一点?
我感谢你的时间!