Main class in cpp

Regarding C++ Include another class

Got respond of splitting my File2.cpp into another .h file but if i am declaring a class, how do i split it into another .h file as i need to maintain the public and private of the variable(private) and functions(public) and how do i get ClassTwo ctwo to my File1.cpp at main method

5 Answers 5

What is the basic problem in your code?

Your code needs to be separated out in to interfaces(.h) and Implementations(.cpp).
The compiler needs to see the composition of a type when you write something like

This is because the compiler needs to reserve enough memory for object of type ClassTwo to do so it needs to see the definition of ClassTwo . The most common way to do this in C++ is to split your code in to header files and source files.
The class definitions go in the header file while the implementation of the class goes in to source files. This way one can easily include header files in to other source files which need to see the definition of class who’s object they create.

Why can’t I simply put all code in cpp files and include them in other files?

You cannot simple put all the code in source file and then include that source file in other files.C++ standard mandates that you can declare a entity as many times as you need but you can define it only once(One Definition Rule(ODR)). Including the source file would violate the ODR because a copy of the entity is created in every translation unit where the file is included.

How to solve this particular problem?

Your code should be organized as follows:

#include #include class ClassTwo < private: string myType; public: void setType(string); std::string getType(); >; 
#include"File1.h" Implementation of ClassOne 
#include"File2.h" void ClassTwo::setType(std::string sType) < myType = sType; >void ClassTwo::getType(float fVal)
#include #include #include "file1.h" #include "file2.h" using namespace std; int main() < ClassOne cone; ClassTwo ctwo; //some codes >

Is there any alternative means rather than including header files?

If your code only needs to create pointers and not actual objects you might as well use Forward Declarations but note that using forward declarations adds some restrictions on how that type can be used because compiler sees that type as an Incomplete type.

@baokychen: Make one? You should be declaring your classes/structs in header files and defining them in implementation files (.cpp)

@baokychen: When you create a class object the compiler needs to know the definition of that type.This is achieved by separating class declarations in header files and their implementations in cpp files.Further you include header files in cpp files where you need to create objects.If you merely include cpp files in other files then you end up violating One Definition rule.

C++ (and C for that matter) split the «declaration» and the «implementation» of types, functions and classes. You should «declare» the classes you need in a header-file (.h or .hpp), and put the corresponding implementation in a .cpp-file. Then, when you wish to use (access) a class somewhere, you #include the corresponding headerfile.

#include "ClassOne.hpp" // implementation of constructor ClassOne::ClassOne() :member(0) <> // implementation of "method" int ClassOne::method()
#include "ClassOne.hpp" // Bring the ClassOne declaration into "view" of the compiler int main(int argc, char* argv[])

The thing with compiling two .cpp files at the same time, it doesnt’t mean they «know» about eachother. You will have to create a file, the «tells» your File1.cpp, there actually are functions and classes like ClassTwo. This file is called header-file and often doesn’t include any executable code. (There are exception, e.g. for inline functions, but forget them at first) They serve a declarative need, just for telling, which functions are available.

When you have your File2.cpp and include it into your File1.cpp , you see a small problem: There is the same code twice: One in the File1.cpp and one in it’s origin, File2.cpp .

Therefore you should create a header file, like File1.hpp or File1.h (other names are possible, but this is simply standard). It works like the following:

void SomeFunc(char c) //Definition aka Implementation < //do some stuff >
void SomeFunc(char c); //Declaration aka Prototype 

And for a matter of clean code you might add the following to the top of File1.cpp :

And the following, surrounding File1.hpp ‘s code:

#ifndef FILE1.HPP_INCLUDED #define FILE1.HPP_INCLUDED // //All your declarative code // #endif 

This makes your header-file cleaner, regarding to duplicate code.

you need to forward declare the name of the class if you don’t want a header:

Important: This only works in some cases, see Als’s answer for more information..

Forward declaring the class ClassTwo before the code statement ClassTwo ctwo; won’t solve the problem.

When you want to convert your code to result( executable, library or whatever ), there is 2 steps:
1) compile
2) link
In first step compiler should now about some things like sizeof objects that used by you, prototype of functions and maybe inheritance. on the other hand linker want to find implementation of functions and global variables in your code.

Now when you use ClassTwo in File1.cpp compiler know nothing about it and don’t know how much memory should allocate for it or for example witch members it have or is it a class and enum or even a typedef of int, so compilation will be failed by the compiler. adding File2.cpp solve the problem of linker that look for implementation but the compiler is still unhappy, because it know nothing about your type.

So remember, in compile phase you always work with just one file( and of course files that included by that one file ) and in link phase you need multiple files that contain implementations. and since C/C++ are statically typed and they allow their identifier to work for many purposes( definition, typedef, enum class, . ) so you should always identify you identifier to the compiler and then use it and as a rule compiler should always know size of your variable!!

Источник

c++ class why need main?

Hello I’m writing a little project in c++ where I would like to have some classes that does some work, I wrote the interfaces and the implementation of the classes. The thing that surprises me is that I cannot have a simple class without a main(), I would like to have a class that once instantiated, It’s methods can be called, do things, but I don’t need (nor want) a main() in the class implementation. Here’s an example I have in my head of what I’d like to have: file animal.h:

#include "animal.h" animal::animal() animal::~animal() int animal::method1(int arg1) > 

And I would like to call the animal class form another file and have it work, something like this: file app.cpp:

#include #include "animal.h" int main()
/usr/lib/gcc/i686-pc-linux-gnu/4.3.3/../../../crt1.o: In function _start: "(.text+0x18): undefined reference to `main`" collect2: ld returned 1 exit status 

«Don’t forget to delete dog; !» There is no need for that because it isn’t a pointer, actually, deleting dog should normally in this case result in an error.

5 Answers 5

but I don’t need (nor want) a main() in the class implementation.

The function main is your entry-point. That is where execution begins. You need to have one and only one such function.

But compiler give me «undefined reference to main» for animal.cpp, but I don’t need a main there, or do I need it?

Now, your problem looks like you have not linked the compiled forms of app.cpp and animal.cpp .

I’m not so strong in Makefiles, I used something like g++ animal.h -o animal and g++ animal.cpp but it gives me the error above

You don’t compile headers. So don’t use: g++ animal.h

When you compiled the animal.cpp separately, g++ created an object file. You will also need to compile the app.cpp because you do need the main . Once you compile the app.cpp file you will have to link it with the animal object file created earlier. But if these did not get linked in, specially, the file containing the main function you will hit the error you are getting now.

However, g++ takes care of what I have described above. Try something like this:

g++ animal.cpp app.cpp -o test 

This will create an executable called test and you can run your application using:

Источник

How can I implement a ‘main’ method in a class in C++?

C++ differs from Java here. You don’t have a «public static void main()» here, just an «int main()» that you place at file level.

I don’t understand why this question was well received in the first place. What OP was trying to do a) makes little sense and b) has nothing to do with the cause of the reported error.

What was the intent? Having the main entry point to the program being the (static) member function? Something related to (automatic) initialisation at program startup? Order of initialisation at program startup?

The intent is important. Some of the answers are good guesses and good interpretations, but without the actual intent it is impossible to know what the real answer should be. Perhaps the OP should provide some input? He or she is still active («Last seen this week»).

8 Answers 8

main is just main . It is simply a function:

The error is referring to the use of the static keyword before the class definition — the compiler expects a variable name after that (as in C++ there is no such thing as a static class ).

And if you want to use static int EntranceMain::main(void) as your program’s entry point, then one way to do it is to tell that to your linker, i.e., give it a full, decorated name of that function. This is highly dependent on which compiler and linker you use, so you need to refer to their documentation. But using that will probably mean you need to include the startup code (e.g., CRT initialisation).

Note that this is not so standard-compliant, though.

You don’t tell it to the linker. What you do is something like «int main() < return EntranceMain::main(); >» at global level. Otherwise, you’re writing an unportable hack (which may go away with any change to the implementation) instead of C++.

main was already mentioned by others, this is just another way. I didn’t say it’s the best; and if you think the wording is bad, feel free to edit.

Its not true, that there are no static methods in C++. A classmember as i.e. static int myFunction() is perfectly legal.

According to the standard, you’re not writing a true main function.

Section 3.6.1, paragraph 3: «The function main shall not be used (3.2) within a program. The linkage (3.5) of main is implementation-defined. A program that declares main to be inline or static is ill-formed. The name main is not otherwise reserved. [Example: member function, classes, and enumerations can be called main , as can entities in other namespaces.]»

This means that, by declaring a member function main , you’re just declaring a member function. It has no special significance, and doesn’t mean anything in the class can be called independently. The program would mean entirely the same thing if you substituted snicklefrazz for that function name and all references.

Stylistically, snicklefrazz would be better, since it wouldn’t lead to any possible conclusion with the standard main function.

Section 3.6.1, paragraph 1: «A program shall contain a global function called main , which is the designated start of the program. It is implementation-defined whether a program in a freestanding environment is required to define a main function.»

This means that, unless you’re writing in what the standard calls a freestanding environment (typically used in writing embedded systems), you need to define a main global function, and this is where the program starts.

In Java, a class can have a main method, which is where the program begins when the class is invoked. This is not the case in C++, and there is no way to accomplish this directly.

(And, as others have mentioned, a class cannot be static , and a class definition ends with a semicolon.)

Источник

Читайте также:  Html elements with text attribute
Оцените статью