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

时间:2022-03-17 15:58:51

I am using netbeans IDE for my C++ implementation. I have two source files main.cpp and univ.cpp. And i defined a function show() in univ.cpp. How can i call this function from main. When i call normally like below, i get "show() not in scope".

我正在使用netbeans IDE进行C ++实现。我有两个源文件main.cpp和univ.cpp。我在univ.cpp中定义了一个函数show()。我如何从main调用此函数。当我像下面那样正常打电话时,我得到“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.

2 个解决方案

#1


1  

Declare the function:

声明函数:

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

#2


2  

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>

#1


1  

Declare the function:

声明函数:

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

#2


2  

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>