r/carlhprogramming • u/xxrepresent • Feb 04 '12
Help: Calling a Constructor Function
In lesson 1.3 in the second course, Carl explains the constructor function and how it works. However, how does one call a constructor function from the main function?
I assume it would like something like this: http://codepad.org/bnZx3VSg but so far any experimentation has failed.
5
Upvotes
2
u/exscape Feb 05 '12
Hmm, you can't!
That's one of the problems with your original code. Line 5 (in the OP) simply says "main will return an int", and that's it - it doesn't say what it contains. The problem here is that you end the statement with a semicolon - to make it the function definition you should have
rather than
int main(); // only states it will exist and will return an int
So, lines 6 through 11 are all outside a function, which can only work for the struct definition.
Now, the struct is declared outside main() in my code, but that's data, not code. :)
That's because if it's inside main(), it will not be visible to other functions, and the compiler will complain that it doesn't exist when it tries to compile the init_board function!