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

Popular posts from this blog

SPSS keyboard combination alters encoding -

Socket.connect doesn't throw exception in Android -

iphone - How do I keep MDScrollView from truncating my row headers and making my cells look bad? -