1,105,386 Community Members

File handling problem

Member Avatar
Light Poster
30 posts since Jul 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]

HI folks,
This is my code. How can i pass a outside structure parameter to a class method?

using namespace std;
struct status 
    char name[80];
    double balance;
    unsigned long account_num;

class FileHandler
    int getRecords(struct status *);
    int putRecords(struct status *);

    string fileName;
    fstream fPointer; // ref

FileHandler :: FileHandler(string fname)
    fPointer.open(fname, ios::out | ios::binary);
int FileHandler :: getRecords(struct status *acc)
    strcpy(acc.name, "Heartly");
    acc.balance = 1000;
    acc.account_num = 3;
    return 0;
int FileHandler :: putRecords(struct status *acc)
    cout << acc.name << endl;
    cout << "Account # " << acc.account_num;
    cout << endl << "Balance: $" << acc.balance<<endl;
    return 0;

In FileHandler constructor i got the below error

clshandle.cpp: In constructor ‘FileHandler::FileHandler(std::string)’:
clshandle.cpp:31:45: error: no matching function for call to ‘std::basic_fstream<char>:: open(std::string&, std::_Ios_Openmode)’
/usr/include/c++/4.5/fstream:866:7: note: candidate is: void std::basic_fstream<_CharT, _Traits>:: open(const char*, std::ios_base:: openmode) [with _CharT = char, _Traits = std::char_traits<char>, std::ios_base:: openmode = std::_Ios_Openmode]

in getReords() methods, i got the below error.

clshandle.cpp: In member function ‘int FileHandler::getRecords(status*)’:
clshandle.cpp:53:13: error: request for member ‘name’ in ‘acc’, which is of non-class type ‘status*’
clshandle.cpp:54:6: error: request for member ‘balance’ in ‘acc’, which is of non-class type ‘status*’
clshandle.cpp:55:6: error: request for member ‘account_num’ in ‘acc’, which is of non-class type ‘status*’
clshandle.cpp: In member function ‘int FileHandler:: putRecords(status*)’:
clshandle.cpp:60:14: error: request for member ‘name’ in ‘acc’, which is of non-class type ‘status*’
clshandle.cpp:61:30: error: request for member ‘account_num’ in ‘acc’, which is of non-class type ‘status*’
clshandle.cpp:64:38: error: request for member ‘balance’ in ‘acc’, which is of non-class type ‘status*’
Member Avatar
Posting Expert
5,632 posts since Jan 2008
Reputation Points: 2,218 [?]
Q&As Helped to Solve: 768 [?]
Skill Endorsements: 26 [?]

Lines 27 -30. The fstream open function takes const char*, not a string. You need to convert the string to a const char* using the c_str function from the string library.

void open ( const char * filename,
ios_base::openmode mode = ios_base::in | ios_base::out );


FileHandler :: FileHandler(string fname)
    fPointer.open(fname.c_str(), ios::out | ios::binary);
Member Avatar
Practically a Master Poster
604 posts since Aug 2010
Reputation Points: 48 [?]
Q&As Helped to Solve: 83 [?]
Skill Endorsements: 1 [?]

And in getRecords(), you're passing a pointer to a struct, so you need to use the -> operator to access its methods, e.g:

acc->balance = 1000;

Or you can pass the struct "by reference", in which case you need to change your parameter declaration (in the prototype and the implementation of the method):

int FileHandler :: getRecords(struct status &acc)

(notice the '&' instead of the '*'). And then you can continue to use the .-operator as you are now.

This article has been dead for over three months: Start a new discussion instead
Start New Discussion
View similar articles that have also been tagged: