c++ Error when passing class object into class function from main? -
i using switch statement based off user's input decide function call. want pass class object class function in code fills in details of class object. here segments of code:
//main.cpp void myswitch(int input) { switch (input) { case 1: { electricitybill ebill; electricitybill::ebilldata(ebill); break; } //---------------------------------------------- //bill.h class electricitybill: public bill { public: void ebilldata(electricitybill ebill); }; //---------------------------------------------- //bill.cpp void electricitybill::ebilldata(electricitybill ebill) { //get data } the error getting is: error: cannot call member function 'void electricitybill::ebilldata(electricitybill)' without object
help appreciated, in advance.
you invoking member function, object on function going invoked passed implicitly ebilldata() function if do:
electricitybill ebill; ebill.ebilldata(); which perhaps meant do. in case, ebilldata should take no explicit argument (a pointer ebill passed implicitly), , access necessary information object invoked on through implicit this pointer:
#include <string> class electricitybill: public bill { public: void ebilldata(); private: std::string something; }; now definition of ebilldata() should of course reflect declaration above. notice, in member function can access data members of object function being invoked on through implicit this pointer, or (more commonly) using name without this-> indirection. instance:
#include <iostream> void electricitybill::ebilldata() { // following 2 statements equivalent... std::cout << something; std::cout << this->something; }
Comments
Post a Comment