Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/138.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
不能将流用作c+中的字段+; 我正在创建一个C++类的文件,我有这个代码, #include "stdafx.h" #include <iostream> #include <string> #include <stdlib.h> #include <fstream> using namespace std; class FileWriter { private: bool isLittleEndian; ofstream file; public: FileWriter(string fileName) : file("data.bin", ios::out | ios::binary) { int i = 1; char *p = (char *)&i; if(p[0] == 1) isLittleEndian = true; } void writeByte() { } void writeShort() { } void writeInt() { } void writeLong() { } void writeUnsignedByte() { } void writeUnsignedShort() { } void writeUnsignedInt() { } void writeUnsignedLong() { } void writeFloat() { } void writeDouble() { } void writeString() { } void closeFile() { file.close(); } }; int main() { FileWriter writer = FileWriter("C:\\Users\\Owner\\Desktop\\Test.bin"); writer.closeFile(); return 0; } #包括“stdafx.h” #包括 #包括 #包括 #包括 使用名称空间std; 类文件编写器 { 私人: 布尔伊斯利特林迪安; 流文件; 公众: FileWriter(字符串文件名):文件(“data.bin”,ios::out | ios::binary) { int i=1; char*p=(char*)&i; 如果(p[0]==1) isLittleEndian=真; } void writeByte() { } void writeShort() { } 无效写入() { } 无效写长() { } void writeUnsignedByte() { } 无效writeUnsignedShort() { } 无效writeUnsignedInt() { } void writeUnsignedLong() { } void writeFloat() { } 无效可写双工() { } 无效写列() { } void closeFile() { file.close(); } }; int main() { FileWriter=FileWriter(“C:\\Users\\Owner\\Desktop\\Test.bin”); writer.closeFile(); 返回0; }_C++ - Fatal编程技术网

不能将流用作c+中的字段+; 我正在创建一个C++类的文件,我有这个代码, #include "stdafx.h" #include <iostream> #include <string> #include <stdlib.h> #include <fstream> using namespace std; class FileWriter { private: bool isLittleEndian; ofstream file; public: FileWriter(string fileName) : file("data.bin", ios::out | ios::binary) { int i = 1; char *p = (char *)&i; if(p[0] == 1) isLittleEndian = true; } void writeByte() { } void writeShort() { } void writeInt() { } void writeLong() { } void writeUnsignedByte() { } void writeUnsignedShort() { } void writeUnsignedInt() { } void writeUnsignedLong() { } void writeFloat() { } void writeDouble() { } void writeString() { } void closeFile() { file.close(); } }; int main() { FileWriter writer = FileWriter("C:\\Users\\Owner\\Desktop\\Test.bin"); writer.closeFile(); return 0; } #包括“stdafx.h” #包括 #包括 #包括 #包括 使用名称空间std; 类文件编写器 { 私人: 布尔伊斯利特林迪安; 流文件; 公众: FileWriter(字符串文件名):文件(“data.bin”,ios::out | ios::binary) { int i=1; char*p=(char*)&i; 如果(p[0]==1) isLittleEndian=真; } void writeByte() { } void writeShort() { } 无效写入() { } 无效写长() { } void writeUnsignedByte() { } 无效writeUnsignedShort() { } 无效writeUnsignedInt() { } void writeUnsignedLong() { } void writeFloat() { } 无效可写双工() { } 无效写列() { } void closeFile() { file.close(); } }; int main() { FileWriter=FileWriter(“C:\\Users\\Owner\\Desktop\\Test.bin”); writer.closeFile(); 返回0; }

不能将流用作c+中的字段+; 我正在创建一个C++类的文件,我有这个代码, #include "stdafx.h" #include <iostream> #include <string> #include <stdlib.h> #include <fstream> using namespace std; class FileWriter { private: bool isLittleEndian; ofstream file; public: FileWriter(string fileName) : file("data.bin", ios::out | ios::binary) { int i = 1; char *p = (char *)&i; if(p[0] == 1) isLittleEndian = true; } void writeByte() { } void writeShort() { } void writeInt() { } void writeLong() { } void writeUnsignedByte() { } void writeUnsignedShort() { } void writeUnsignedInt() { } void writeUnsignedLong() { } void writeFloat() { } void writeDouble() { } void writeString() { } void closeFile() { file.close(); } }; int main() { FileWriter writer = FileWriter("C:\\Users\\Owner\\Desktop\\Test.bin"); writer.closeFile(); return 0; } #包括“stdafx.h” #包括 #包括 #包括 #包括 使用名称空间std; 类文件编写器 { 私人: 布尔伊斯利特林迪安; 流文件; 公众: FileWriter(字符串文件名):文件(“data.bin”,ios::out | ios::binary) { int i=1; char*p=(char*)&i; 如果(p[0]==1) isLittleEndian=真; } void writeByte() { } void writeShort() { } 无效写入() { } 无效写长() { } void writeUnsignedByte() { } 无效writeUnsignedShort() { } 无效writeUnsignedInt() { } void writeUnsignedLong() { } void writeFloat() { } 无效可写双工() { } 无效写列() { } void closeFile() { file.close(); } }; int main() { FileWriter=FileWriter(“C:\\Users\\Owner\\Desktop\\Test.bin”); writer.closeFile(); 返回0; },c++,C++,但由于某种原因,它不会让我有一个流动场的概念,当我尝试时,它会说,Error 1 Error C2248:'std::basic_of stream::operator=':无法访问在类'std::basic_of stream'c:\users\owner\documents\visual studio 2012\projects\bytetests\bytetests\bytetests.cpp 25 1中声明的私有成员,因为我的类中的函数需要操作此流。我不明白为什么这么难做到。您不能复制或

但由于某种原因,它不会让我有一个流动场的概念,当我尝试时,它会说,
Error 1 Error C2248:'std::basic_of stream::operator=':无法访问在类'std::basic_of stream'c:\users\owner\documents\visual studio 2012\projects\bytetests\bytetests\bytetests.cpp 25 1中声明的私有成员,因为我的类中的函数需要操作此流。我不明白为什么这么难做到。

您不能复制或分配
std::ofstream
,这意味着您不能这样做:

file = openedFile;
您需要正确初始化它,或者移动副本分配

初始化(首选选项):

移动副本分配:

file = std::move(openedFile);
或者,您可以使用
std::ofstream::open
方法:

file.open("data.bin", ios::out | ios::binary);

您无法复制或分配
std::of Stream
,这意味着您无法执行此操作:

file = openedFile;
您需要正确初始化它,或者移动副本分配

初始化(首选选项):

移动副本分配:

file = std::move(openedFile);
或者,您可以使用
std::ofstream::open
方法:

file.open("data.bin", ios::out | ios::binary);

您可以只打开现有对象:

file.open("data.bin", ios::out | ios::binary);
您需要避免调用(或至少需要)类的副本。改变

FileWriter writer = FileWriter("C:\\Users\\Owner\\Desktop\\Test.bin");
致:


您可以只打开现有对象:

file.open("data.bin", ios::out | ios::binary);
您需要避免调用(或至少需要)类的副本。改变

FileWriter writer = FileWriter("C:\\Users\\Owner\\Desktop\\Test.bin");
致:

只有一个
操作符=
是为
流的基本单元定义的,即移动分配操作符。复制赋值运算符被隐式删除,因此此代码将不起作用

您需要使用成员初始值设定项列表初始化
文件
,路径如下:

FileWriter(std::string fileName)
    : file(fileName, ios::out | ios::binary)
{
    ...
}
只有一个
操作符=
是为
流的基本单元定义的,即移动分配操作符。复制赋值运算符被隐式删除,因此此代码将不起作用

您需要使用成员初始值设定项列表初始化
文件
,路径如下:

FileWriter(std::string fileName)
    : file(fileName, ios::out | ios::binary)
{
    ...
}


@Popgalop我已经添加了一些解释。我仍然得到错误1错误C2248:“std::basic_of stream::basic_of stream”:无法访问在类“std::basic_of stream”c:\users\owner\documents\visual studio 2012\projects\bytetests\bytetests\bytetests.cpp 87 1中声明的私有成员ByteTests@Popgalop我的答案有一个输入错误,请参见编辑。我用你建议的编辑更新了我问题中的代码,但我仍然得到了一个error@Popgalop我添加了一些解释。我仍然收到错误1错误C2248:“std::basic_of stream::basic_of stream”:无法访问在类“std::basic_of stream”c:\users\owner\documents\visualstudio中声明的私有成员2012\projects\bytetests\bytetests\bytetests.cpp 87 1ByteTests@Popgalop我的答案有一个输入错误,请参见编辑。还有,我用你建议的编辑更新了我问题中的代码,但我仍然得到了一个错误,我仍然得到了这个错误2 IntelliSense:“std::basic_of stream::basic_of stream(const std::basic_of stream::_Myt&_Right)[with _Elem=char,_Traits=std::char_Traits]”(在“C:\Program Files(x86)”的第1034行声明)\Microsoft Visual Studio 11.0\VC\include\fstream)无法访问c:\Users\Owner\Documents\Visual Studio 2012\Projects\ByteTests\ByTests\ByteTests.cpp 13 11ByteTests@Popgalop删除流openFile(…)的原始
file=openedFile
错误1错误C2248:“std::basic_of stream::basic_of stream”:无法访问在类“std::basic_of stream”c:\users\owner\documents\visual studio 2012\projects\bytetests\bytetests\bytetests\bytetests.cpp 84 1中声明的私有成员ByteTests@Popgalop我认为这与你刚才展示的代码无关我们…我仍然得到这个错误2智能感知:“std::basic_of stream::basic_of stream(const std::basic_of stream::_Myt&_Right)[with _Elem=char,_Traits=std::char_Traits]”(在“C:\Program Files(x86)\Microsoft Visual Studio 11.0\VC\include\fstream”的第1034行声明)无法访问c:\Users\Owner\Documents\Visual Studio 2012\Projects\ByteTests\ByteTests\ByteTests.cpp 13 11ByteTests@Popgalop删除流openFile(…)的原始
file=openedFile
错误1错误C2248:“std::basic_of stream::basic_of stream”:无法访问在类“std::basic_of stream”c:\users\owner\documents\visual studio 2012\projects\bytetests\bytetests\bytetests\bytetests.cpp 84 1中声明的私有成员ByteTests@Popgalop我认为这与你刚才展示的代码无关我们…你不应该使用你的代码,它会让所有答案变得无关紧要。但是现在似乎没有什么明显的错误,所以错误肯定在别处。那么为什么它不起作用呢,你能编译它吗?我能编译。@Popgalop我更新了我的答案,它应该能解决你剩下的问题。你不应该修改你的代码,它使所有的答案都无关紧要。但是现在似乎没有什么明显的错误,所以错误肯定在别处。那么为什么它不能工作呢,你能编译它吗