C++ 运算符重载未从文件中提取重写值
所以,我遇到了相当大的麻烦。我试图创建两个重载函数,一个使用插入操作符输入文件,另一个使用提取操作符从文件中提取值 代码示例1:显示类成员C++ 运算符重载未从文件中提取重写值,c++,operator-overloading,C++,Operator Overloading,所以,我遇到了相当大的麻烦。我试图创建两个重载函数,一个使用插入操作符输入文件,另一个使用提取操作符从文件中提取值 代码示例1:显示类成员 class OUSB { private: unsigned short PORTB; public: OUSB() { }; char command[256]; // Example of Interface methods (These can be changed....)
class OUSB
{
private:
unsigned short PORTB;
public:
OUSB() { };
char command[256];
// Example of Interface methods (These can be changed....)
unsigned short writePORTB(unsigned short newValue);
unsigned short readPORTB();
unsigned short runOUSBcommand(const char* command);
// you may choose to implement operator overloaded functions for read and write functions for portb
void operator<<(const unsigned short &val); // write portb
void operator>>(unsigned short &val); // read portb
};
usb类
{
私人:
无符号短端口B;
公众:
OUSB(){};
char命令[256];
//接口方法示例(这些方法可以更改…)
无符号短写端口B(无符号短新值);
无符号短读取器b();
无符号短runOUSBcommand(const char*命令);
//您可以选择为portb的读写函数实现运算符重载函数
void运算符(无符号的short&val);//读取端口B
};
代码示例2:显示每个成员或至少大多数成员的定义
unsigned short OUSB::runOUSBcommand(const char* command)
{
FILE* fpipe;
char line[256];
fpipe = (FILE*)_popen(command, "r"); // attempt to open pipe and execute a command
if (fpipe != NULL) // check that the pipe opened correctly
{
while (fgets(line, sizeof(line), fpipe))
{ // do nothing here, or print out debug data
//cout << line; // print out OUSB data for debug purposes
}
_pclose(fpipe); // close pipe
}
else cout << "Error, problems with pipe!\n";
int ousbOP = (int)atoi(line);
return ousbOP;
}
void OUSB::operator<<(const unsigned short& val)
{
OUSB hello;
hello.writePORTB(val);
}
void OUSB::operator>>(unsigned short& val)
{
OUSB hello;
hello.readPORTB();
}
unsigned short OUSB::writePORTB(unsigned short newValue)
{
sprintf_s(command, "ousb -r io portb %d", newValue);
PORTB = runOUSBcommand(command);
return PORTB;
}
unsigned short OUSB::readPORTB()
{
PORTB = runOUSBcommand("ousb -r io portb");
return PORTB;
}
无符号短OUSB::runOUSBcommand(const char*命令)
{
文件*fpipe;
字符行[256];
fpipe=(文件*)\u popen(命令,“r”);//尝试打开管道并执行命令
if(fpipe!=NULL)//检查管道是否正确打开
{
而(fgets(管线、尺寸(管线)、fpipe))
{//此处不执行任何操作,或打印调试数据
//库特
您不使用参数val
,而是放弃hello.readPORTB()的返回值
。如果在编译时启用所有警告,并将警告视为错误,则此错误将更容易识别。您将读取的值存储在一个新对象中,然后立即丢弃,并且不修改参数
您应该让*此
执行读写操作,并且在读取时需要将值存储在参数中:
void OUSB::operator<<(const unsigned short& val)
{
writePORTB(val);
}
void OUSB::operator>>(unsigned short& val)
{
val = readPORTB();
}
void OUSB::运算符(无符号短值&val)
{
val=readPORTB();
}
我需要您的帮助,请告诉我如何与您联系
void OUSB::operator>>(unsigned short& val)
{
OUSB hello;
hello.readPORTB();
}
void OUSB::operator<<(const unsigned short& val)
{
writePORTB(val);
}
void OUSB::operator>>(unsigned short& val)
{
val = readPORTB();
}