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
{
public:
    FileHandler(string);
    ~FileHandler();
    
    int getRecords(struct status *);
    int putRecords(struct status *);
  
    

private:
    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.precision(2);
    cout.setf(ios::fixed);
    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*’

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 );

http://www.cplusplus.com/reference/iostream/fstream/fstream/
http://www.cplusplus.com/reference/string/string/c_str/

FileHandler :: FileHandler(string fname)
{
    fPointer.open(fname.c_str(), ios::out | ios::binary);
}

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.