类为什么需要main

类为什么需要main

本文介绍了c ++类为什么需要main?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我感到惊讶的是,我不能有一个简单的类没有一个main(),我想有一个实例化的类,它的方法可以被调用,做的事情,但我不需要)一个main()在类实现中。
下面是一个我想要的例子:



文件animal.h:

  class animal 
{
public:
animal();
〜animal();

public:
int method1(int arg1);

private:
int var1;
};

file animal.cpp:

  #includeanimal.h

animal :: animal(){...}
animal ::〜animal
int animal :: method1(int arg1){return var1;}
}

我想调用动物类形式另一个文件,并使其工作,像这样:
文件app.cpp:

  #include< neededlib> 
#includeanimal.h

int main()
{
animal dog;
cout<< dog.method1(42);
return 0;
}

但编译器给了我

  /usr/lib/gcc/i686-pc-linux-gnu/4.3.3 /../../../ crt1.o:在函数_start:

(.text + 0x18):未定义引用`main`

collect2:ld返回1退出状态

对于animal.cpp,但我不需要一个main,或者我需要它吗?



我在哪里错了?

解决方案

函数 main 是您的入门点。这就是执行开始的地方。你需要有一个这样的函数。

现在,您的问题看起来像没有链接 app.cpp animal.cpp 的编译形式。

您不编译头文件。所以不要使用: g ++ animal.h



当你编译animal.cpp时,g ++创建了一个对象文件。您还需要编译 app.cpp ,因为您需要 main 。一旦你编译 app.cpp 文件,你必须链接它与之前创建的 animal 目标文件。但是如果这些没有链接,特别是包含 main 函数的文件,你将会遇到你现在得到的错误。



但是, g ++ 会照顾我上面描述的内容。尝试这样:

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

这将创建一个名为 test 的可执行文件,您可以使用以下命令运行应用程序:

  ./ test 


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:

class animal
{
 public:
   animal();
  ~animal();

 public:
   int method1(int arg1);

 private:
   int var1;
};

file animal.cpp:

#include "animal.h"

animal::animal(){...}
animal::~animal(){...}
int animal::method1(int arg1){return var1;}
}

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

#include <neededlib>
#include "animal.h"

int main()
{
 animal dog;
 cout << dog.method1(42);
 return 0;
}

But compiler give me

/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

for animal.cpp, but I don't need a main there, or do I need it?

Where Am I wrong?

解决方案

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

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

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:

./test

这篇关于c ++类为什么需要main?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 09:18