i trying globally declare – , initialize – vector of structs. code following:
struct creation (in header file vsystem.h) struct var { string name; float value; }; variable (in source file vsystem.cpp) #include <string> #include <vector> vector <var> varlist;
both of # include <string>
, <vector>
. have tried
vector <var> varlist ();
but doesn't work either. error is
expected constructor, destructor, or type conversion before '<' token
on side note, setvar
function triggering error:
multiple markers @ line - 'string' not declared in scope - expected primary-expression before 'float' - initializer expression list treated compound expression - expected ',' or ';' before '{' token
code:
int setvar(string varname, float value){ // check see if varname exists varexists = false; (int i=0; i<varlist.size(); i++){ if (varlist[i].name == varname){ varexists = true; return err_var_exists; } } // good! variable doesn't exist yet. var tempvar (); var.name = varname; var.value = value; varlist.push_back(tempvar); return 0; }
help please!
i running eclipse helios service release 2 g++ compiler on mac 10.6.7.
vector
in std
namespace. need qualify std::vector
in declaration:
std::vector <var> varlist;
(alternatively use using declaration, using std::vector;
, if hate std::
.)
similarly string
; needs qualified std::string
. of names in c++ standard library in std
namespace except (a) macros , (b) in legacy c headers (the ones end in .h)
Comments
Post a Comment