简体   繁体   English

从main调用C ++源文件中的函数

[英]Calling function in a C++ source file from main

I am using netbeans IDE for my C++ implementation. 我正在将netbeans IDE用于我的C ++实现。 I have two source files main.cpp and univ.cpp. 我有两个源文件main.cpp和univ.cpp。 And i defined a function show() in univ.cpp. 我在univ.cpp中定义了一个函数show()。 How can i call this function from main. 我如何从main调用此函数。 When i call normally like below, i get "show() not in scope". 当我像下面正常调用时,我得到“ show()不在范围内”。

    int main(int argc, char**argv)
    {
       show();
       return 0;
    }

I don't want to use a separate header file and define the function. 我不想使用单独的头文件并定义函数。 Instead i want to define this function in cpp source file like stated above. 相反,我想在cpp源文件中定义此功能,如上所述。

Thanks. 谢谢。

You should create a header for univ called univ.h here would be the code: 您应该为univ创建一个名为univ.h的标头,下面是代码:

#ifndef _UNIV_H_
#define _UNIV_H_

void show();

#endif

The you will need to include it in both cpp files. 您将需要将其包括在两个cpp文件中。

#include <univ.h>

Declare the function: 声明功能:

int main(int argc, char **argv)
{
   extern void show();
   show();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM