This code will compile, but will produce the above error when I try to run.
Why is this?
Code:class Test { public: int main (int, char **); }; int Test::main (int argc, char *argv[]) { return 0; }
This code will compile, but will produce the above error when I try to run.
Why is this?
Code:class Test { public: int main (int, char **); }; int Test::main (int argc, char *argv[]) { return 0; }
>Why is this?
Because you don't have a main function. Below the code you posted, you would need to add:
Code:int main(int argc, char *argv[]) { Test main(argc, argv); return 0; }
As it says, there is no main function in this code.
Thanks for the replies.
Swoopy, I tried adding your code exactly below my code, but I get compiler errors:
But if I take my original code, and then remove the 'Test::main' and replace with just 'main' then it works.Code:no matching function for call to `Test::Test (int &, char **&)'
What is the difference between defining a function 'int Test::main()' and just 'int main()'?
>Swoopy, I tried adding your code exactly below my code, but I get compiler errors:
Oops, I goofed. It should be:
>What is the difference between defining a function 'int Test::main()' and just 'int main()'?Code:int main(int argc, char *argv[]) { Test main; main.main(argc, argv); return 0; }
Test::main is just a member function named main inside of a class called Test. int main() is the function called to invoke any C++ program. Every program must have either a main (for a Console application) or a WinMain (for a windows application).
Ah ok I think I got it.
Although naming the Test object delared in main() as main was a little confusing.
It strikes me that C++'s OOP capabilities are really just a hack. Not as complete as say Java or .NET which are entirely based around OOP.
That's mainly what sucks about them. You can't choose the approach you wish to take in your algorithms. If you want to go all procedural you're f***ed I guess.
>Although naming the Test object delared in main() as main was a little confusing.
True maybe this is better:
Code:int main(int argc, char *argv[]) { Test test1; test1.main(argc, argv); return 0; }
You could also make a constructor that takes argc and argv as arguments, which would automatically get called when a Test class is constructed.
Code:#include <iostream> class Test { public: Test(int, char **); }; Test::Test(int argc, char *argv[]) { std::cout << "In Test constructor." << std::endl; } int main(int argc, char *argv[]) { Test test1(argc, argv); return 0; }
Last edited by swoopy; 09-29-2006 at 09:14 AM. Reason: Removed "return 0" from constructor
C++'s OO capabilities are just as complete, if not more so, as those of Java or C#. It's just that unlike these languages, C++ has other things to offer, too.Originally Posted by Frobozz
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
Exactly!Originally Posted by CornedBee
"The Internet treats censorship as damage and routes around it." - John Gilmore