oop - Char passing in C++ with pointers -
im having unclear image of pointers , char passing functions. please can tell me im doing wrong , brief idea pointers?ex : should use them, etc...
#include <iostream> using namespace std; class book{ private: char *cname; int cfee; int nopeople; int income; public: void setdata(char &x,int y,int z){ cname = &x; cfee = y; nopeople = z; } void calincome(){ income = cfee * nopeople; } void viewincome(){ cout<<income; cout<<cname; } }; int main(){ book b1; b1.setdata('dise',20000,30); b1.calincome(); b1.viewincome(); }
im getting error in code
//b1.setdata('dise',20000,30); "non-const lvalue reference type 'char' cannot bind temparory of type 'int'"
you should change setdata()
declaration void setdata(const char *x,int y,int z)
as you're doing expecting reference single char
parameter, cannot used assign char*
pointer meant point @ character array.
also aren't specifying character array literal in call:
b1.setdata('dise',20000,30);
needs changed
b1.setdata("dise",20000,30); // ^ ^
Comments
Post a Comment