c++ - Function call is giving me the error expression must have class type -
i have made function call in simple terms, displays content of list have values for. here function definition:
void display_list(list<string>*type_list) { cout << "you made function call" << endl; (list<string>::iterator dis = type_list.begin(); dis != type_list.end(); ++dis) { cout << *dis; cout << "\n"; } }
all supposed make easier on me, because there numerous times throughout code have display contents of list, tried make easier on myself , make function call have make function call:
display_list(&list_name_here);
although works fine, can see added test 'cout' make sure function call works correctly, doesn't display contents, , error highlights the
type_list
and error pops says expression must have class type?
now did change code this:
void display_list(list<string>*type_list) { cout << "you made function call" << endl; list<string> gen; *type_list = gen; (list<string>::iterator dis = gen.begin(); dis != gen.end(); ++dis) { cout << *dis; cout << "\n"; } }
in form dereferenced type_list local variable, , proceeded normal.. method rid of class type error, when compile , run it, nothing displayed list.. list simple should display 10 values.
now in case asking, original algorithm when placed in main code , replace type_list appropriate list names, code works , designed. display contents of list. know error isn't in that.
can please shed light on this?
you need use ->
access members , member functions pointer.
void display_list(list<string>*type_list) { cout << "you made function call" << endl; (list<string>::iterator dis = type_list->begin(); dis != type_list->end(); ++dis) { cout << *dis; cout << "\n"; } }
as problem of empty list in second attempt,
list<string> gen; *type_list = gen;
sets *type_list
gen
doesn't change gen
. gen
empty list , proceed iterate on it.
you have used:
list<string> gen = *typ_list;
or (thanks, @mattmcnabb)
list<string>& gen = *typ_list;
Comments
Post a Comment