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

    Join Date
    Mar 2013
    Posts
    54
    Rep Power
    2

    Passing function instance(object) as parameter issue


    Code:
    #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";
    openFile(&fileName);
    myReplace(&nOrigin, &nOriginReplace);
    
    closeFile(openFile);
    return 0;
    }
    
    
    void myReplace(string *pnO, string *pnOR){
    
    }
    
    
    void openFile(string *pfileName) {
    
    ofstream myfile;
    myfile.open("*pfileName");
    
    
    }
    
    void closeFile(void (*popenFile)(string*)){
    
    (*popenFile).myfile.close();
    
    }

    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:

    Code:
    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 08:24 PM.
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,379
    Rep Power
    1871
    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