473,407 Members | 2,320 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

can i enter main() within a class in c++?

actually,im attending coaching for c++ and it was told to us that we can add main() inside a class,but when i tried doing so,i got a linker error ,so i want to know if we can include main() inside a class,please explain
Sep 21 '09 #1
3 6153
weaknessforcats
9,208 Expert Mod 8TB
Of course you can add main() inside a class.

If your class is MyClass, then the main() inside the class is really MyClass::main. That's a different function from ::main which is the one the linker is looking for.

You must always have a main() as a global function. No way around it.
Sep 21 '09 #2
k,thanks a lot.So,u say we can include main() using scope resolution operator.
Sep 22 '09 #3
weaknessforcats
9,208 Expert Mod 8TB
Exactly. That's what the scope resoulution operator is for.

C++ has function overloading. That is, more than than one function can have the same name. However, at the same time, all function names must be unique in order for the compiler and linker to operate.

So, each function name is preceded with a scope resolution operator and that is preceded by the name of the scope, such as a class name.

MyClass::main() is the main function inside the MyClass class.

Xyz::main() is the main function inside the Xyz class.

::main() is a function that has no named scope. This is the main() that is outside any scope. That is, the freestanding or global main(). This is the main() the linker is looking for. This is the main that has to be in every program.

Expand|Select|Wrap|Line Numbers
  1. int ::main()   //the global main()
  2. {
  3.      MyClass obj;
  4.      obj.main();         //calls MyClass::main()
  5.      obj.MyClass::main();  //does the same as the preceding line.
  6. }
Sep 22 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Ali Eghtebas | last post by:
Hi, I have 3 questions regarding the code below: 1) Why can't I trap the KEYDOWN while I can trap KEYUP? 2) Is it correct that I use Return True within the IF-Statement? (I've already read...
45
by: Steven T. Hatton | last post by:
This is a purely *hypothetical* question. That means, it's /pretend/, CP. ;-) If you were forced at gunpoint to put all your code in classes, rather than in namespace scope (obviously classes...
2
by: Bill D | last post by:
In a simple Windows forms application, the main form is called Form1. Within this form's class, I refer to the the running instance of this main class as "this" as in this.Text = "NEW TEXT" I...
8
by: Mountain Bikn' Guy | last post by:
I'm using async delegate calls. I would like to re-enter my initial thread after the callback. (I'm not using UI controls, so Control.Invoke is not the solution.) How can I manually implement...
3
by: markaelkins | last post by:
Hi. I am trying to enter a variable in the treenodesrc of a treenode. I am basically trying to send an ID variable into sql to return different records. I've searched everywhere and cannot find the...
0
by: Tom Edelbrok | last post by:
I'm using VS 2005 to develop an intranet asp.net web application and I get a weird situation. If I start out with any ASPX page that contains an ImageButton control followed by a TextBox control,...
7
by: Marc | last post by:
Hi, I want my user to be able to rename a button control by selcting rename from a menu. This then opens a text box in which to enter the new name in. I want the button control to...
8
by: garyusenet | last post by:
The following piece of code appears in an example i'm reading after the class has been named. public string userMessage; However if i move that into my main method, i'm told that the keyword...
3
by: bg_ie | last post by:
Hi, Lets say I have the following class - class MyClass: def __init__(self): print (__name__.split(".")) if __name__ == '__main__': MyClassName = "MyClass"
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.