如何从另一个文件中调用与另一个文件中同名的函数(当两者都包含时)

C++ How to call a function from another file with the same name as a function in another file when both are included?

本文关键字:另一个 文件 函数 两者都 包含时 调用      更新时间:2023-10-16

我想知道如何从另一个文件中调用与另一个文件中函数同名的函数,当两者都包含时。例子:

main.cpp

#include "a.h"
#include "b.h"
using namespace std;
int main()
{
start();
return 0;
}

a.h

#ifndef _A_H
#define _A_H
#pragma once
int start();
#endif

a.cpp

#include "stdafx.h"
using namespace std;
int start()
{
//code here
return 0;
}

b.h

#ifndef _B_H
#define _Win32_H
#pragma once
int start();
#endif

b.cpp

#include "stdafx.h"
using namespace std;
int start()
{
//code here
return 0;
}

start ();在main.cpp中将使用start();from a.h,但我想使用start();从b.h如何选择start();在b.h吗?

假设函数在各自的.cpp文件中定义,即一个在a.cpp中定义,一个在b.cpp中定义,则不可能发生这种情况。一旦您尝试链接您的代码,您将得到start()被定义两次的错误。因此,您不必考虑如何调用其中一个;除非两个函数是相同的(即在相同的CPP文件中定义),否则代码将不会链接。如果是这种情况,那么调用哪个都无关紧要(因为只有一个)。

相关文章: