c++ - Why was the function not declared in this scope? -
i'm trying invoke constructor out of class. unfortunately error:
fun2 not declared in scope
this code:
class fun1 { public: fun1 () { fun2("message"); } }; class fun2 { public: fun2 (std::string s) { std::cout << s; } }; int main() }
how can send "message" constructor of other class?
c++ typically compiled top bottom. since attempt use fun2
before have defined it, compiler complaining. instead, can define fun2
class first:
class fun2 { public: fun2 (std::string s) { std::cout << s; } }; class fun1 { public: fun1 () { fun2("message"); } };
now when compiler sees use identifier fun2
, knows corresponds because has seen definition.
there occasions when compiler parses code in multiple passes , in these places identifiers can declared after used (members, example).
Comments
Post a Comment