To use a struct in a function outside of main(), can you use forward declaration and define it in main(), or does it have to be defined outside of a block?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If you define a structure inside of
main(), it will hide the global name of the structure. So the function outside ofmain()will only be able to reference the global name. This example is taken from the C++ 2011 draft, Section 9.1 p2:There is no syntax to refer to the locally defined type from outside the function scope.
Because of that, even using a template will fail, because there is no way to express the instantiation of the template:Actually, this is now allowed in C++11, as explained in this answer.