1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Rep Power

    Passing function instance(object) as parameter issue

    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    void myReplace(string *pnO, string *pnOR);
    void openFile(string *pfileName);
    void closeFile(void (*popenFile)(string*));
    int main(){
    string nOrigin="France";
    string nOriginReplace="Germany";
    string fileName="Campaign_LND.mis";
    myReplace(&nOrigin, &nOriginReplace);
    return 0;
    void myReplace(string *pnO, string *pnOR){
    void openFile(string *pfileName) {
    ofstream myfile;
    void closeFile(void (*popenFile)(string*)){

    YOu see what I'm doing with myfile.close() in closeFile function. I want a separate function to close myfile and am using the instance of myfile from openFile function.

    Therefore I'm passing the function openFile into closeFile and am attempting to use an object member from that class.

    What am I doing wrong?
    The error is:

    C:\Users\P Ditty\Desktop\New folder\Frontlines.cpp|42|error: request for member 'myfile' in '* popenFile', which is of non-class type 'void(std::string*) {aka void(std::basic_string<char>*)}'|
    Last edited by pditty8811; October 29th, 2013 at 09:24 PM.
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Rep Power
    You need to be more concerned about
    ofstream myfile;
    going out of scope.

    When you've solved that, closing the file will become self apparent.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper

IMN logo majestic logo threadwatch logo seochat tools logo