尝试将指针作为参数传递给指向文件的 fstream 成员
/* 感谢任何看到这个问题并可能尝试回答它的人。我真的不想在这里浪费任何人的时间,但我已经为此绞尽脑汁大约三天了。我意识到对于理解它的人来说这可能非常简单。我已经尝试了我能想到的几乎所有可能的组合,但仍然出现编译器错误。
C:\random\RNDNUMTEST.cpp(41) : 错误 C2102: '&'需要左值
我试图将指针作为参数传递给成员函数 fstream.open() 的函数 makeRndmNumber() 。我想打开 RNDNUMTEST.cpp 中的文件,然后将其传递给 makeRndmNumber() ,以便可以以某种方式对其进行修改。我在网上寻求帮助,包括这个网站,但我觉得我忽略了一些重要或简单的事情,或者也许我只是完全错过了这个概念。
这不是作业,我不是大学生。虽然我确实上过学,但我已经有 10 多年没有做过任何编程了,而且我从一开始就没有真正理解这一点。任何建议将不胜感激。
// These are only excerpts from the actual files.
// RndmNum_Class.h file
typedef void(fstream::*fStream_MPT)(const char*); // fStream_MPT (Member Pointer Type)
class RandomNumber {
public:
RandomNumber();
~RandomNumber() {};
static void loadDigits(double, double, char array[]);
static int getLastNDigits(char array[], int);
static int makeRndmNumber(int, int, fStream_MPT);
};
//*************************************************************8
//RndmNum_Class.cpp file
int RandomNumber::makeRndmNumber(int seed, int _fileSize, fStream_MPT FILE) {
......
}
//**************************************************************/
// RNDNUMTEST.cpp file
#include "RndmNum_Class.h"
int main() {
const char* RNDM_FILE = "c:\\RandomFile.txt";
fstream FStream_Obj;
// FStream_Obj.open(RNDM_FILE);
fStream_MPT FileMembPtr = &FStream_Obj.open(RNDM_FILE);
//fStream_MPT FileMembPtr = &fstream::open;
int seed = 297814;
int size = 20000;
cout << RandomNumber::makeRndmNumber(seed, size, FileMembPtr);
return 0;
}
/* Thanks to anyone looking at this who might attempt to answer it. I'm really not trying to waste anyone's time here, but I have beat my head on this for about three days. I realize it is probably very simple for someone who understands it. I have tried most every possible combination I can think of and still get compiler errors.
C:\random\RNDNUMTEST.cpp(41) : error C2102: '&' requires l-value
I am trying to pass a pointer as a parameter to a function makeRndmNumber() for the member function fstream.open(). I want to open the file in RNDNUMTEST.cpp and then pass it to makeRndmNumber() so that it can be modified in some way. I have looked online for help, including this website, but I feel like I am overlooking something important or simple or maybe I am just missing the concept altogether.
This isn't for homework, I'm not a college student. Although I did go to school for it, it has been over 10 years since I've done any programming and I never really understood this that well to begin with. Any suggestions would be appreciated.
// These are only excerpts from the actual files.
// RndmNum_Class.h file
typedef void(fstream::*fStream_MPT)(const char*); // fStream_MPT (Member Pointer Type)
class RandomNumber {
public:
RandomNumber();
~RandomNumber() {};
static void loadDigits(double, double, char array[]);
static int getLastNDigits(char array[], int);
static int makeRndmNumber(int, int, fStream_MPT);
};
//*************************************************************8
//RndmNum_Class.cpp file
int RandomNumber::makeRndmNumber(int seed, int _fileSize, fStream_MPT FILE) {
......
}
//**************************************************************/
// RNDNUMTEST.cpp file
#include "RndmNum_Class.h"
int main() {
const char* RNDM_FILE = "c:\\RandomFile.txt";
fstream FStream_Obj;
// FStream_Obj.open(RNDM_FILE);
fStream_MPT FileMembPtr = &FStream_Obj.open(RNDM_FILE);
//fStream_MPT FileMembPtr = &fstream::open;
int seed = 297814;
int size = 20000;
cout << RandomNumber::makeRndmNumber(seed, size, FileMembPtr);
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
此:
&FStream_Obj.open(RNDM_FILE)
并未获取函数的地址,而是尝试获取对该函数的调用的返回值的地址。但该函数返回void
,因此出现错误消息。This:
&FStream_Obj.open(RNDM_FILE)
is not taking the address of the function, it's trying to take the address of the return value of a call to that function. But that function returnsvoid
, hence the error message.首先,将函数定义从
typedef void(fstream::*fStream_MPT)(const char*);
更改为typedef void(fstream::*fstream_MPT)(const char*,ios_base:: openmode)
,有一个您忘记的默认参数。根据您的评论,将
fStream_MPT FileMembPtr = &FStream_Obj.open(RNDM_FILE);
更改为fStream_MPT FileMembPtr = &fstream::open;
,并添加一个附加参数makeRndNumber,指向要操作的 fstream 的指针。编辑
这也可以使用 std::function 对象来完成得更干净一些。
首先重新定义你的类型。
然后当你分配的时候,一定要绑定你的对象。
然后在你的函数中你只需调用该函数
First, change the function definition from
typedef void(fstream::*fStream_MPT)(const char*);
totypedef void(fstream::*fstream_MPT)(const char*,ios_base::openmode)
, there is a default parameter you are forgetting.Change the
fStream_MPT FileMembPtr = &FStream_Obj.open(RNDM_FILE);
tofStream_MPT FileMembPtr = &fstream::open;
as per your comment, and add an additional parameter to makeRndNumber, a pointer to an fstream to operate on.EDIT
This could also be done a little cleaner with std::function objects.
First redefine your type.
Then when you assign, be sure to bind your objects.
Then in your function you simply call the function
它没有任何意义:使用成员函数指针,因此您可以在某处应用不同的成员函数,而无需知道调用哪个确切的函数。这就像传递函数的名称(除了该名称是在编译时解析的)。看来这不是你想做的事啊!
即使您正确获取函数的地址(而不是尝试获取调用
open()
的结果的地址),它也不会工作,因为std::fstream::open ()
有两个参数:第二个参数用于开放模式,默认为std::ios_base::in | std::ios_base::out
。我不太确定你真正想要什么,但似乎你想传递文件流。执行此操作的正常方法是将 std::iostream 的引用作为参数传递给函数。好吧,实际上您可能希望最初使用
std::ifstream
,因此将参数作为std::istream&
传递。It doesn't make any sense: member function pointers is used so you can apply different member functions somewhere without knowing which exact function is called. It is like passing the function's name around (except that the name is resolved at compile-time). It doesn't seem that this is what you want to do!
Even if you would correctly obtain the function's address (rather than trying to get the address of the result of calling
open()
), it wouldn't work becausestd::fstream::open()
takes two arguments: the second argument is for the open-mode and it is defaulted tostd::ios_base::in | std::ios_base::out
.I'm not quite sure what you really want to d but it seems you want to pass the file stream around. The normal way to do this is to pass a reference to a
std::iostream
as argument to the function. Well, actually you probably want to use astd::ifstream
initially and hence pass the argument asstd::istream&
.