[英]C++ How to call a function from another file with the same name as a function in another file when both are included?
I am wondering how I could call a function from another file with the same name as a function in another file when both are included. 我想知道当同时包含两个文件时,如何从另一个文件中调用与另一个文件中的函数同名的函数。 Example:
例:
main.cpp main.cpp中
#include "a.h"
#include "b.h"
using namespace std;
int main()
{
start();
return 0;
}
ah 啊
#ifndef _A_H
#define _A_H
#pragma once
int start();
#endif
a.cpp a.cpp
#include "stdafx.h"
using namespace std;
int start()
{
//code here
return 0;
}
bh BH
#ifndef _B_H
#define _Win32_H
#pragma once
int start();
#endif
b.cpp b.cpp
#include "stdafx.h"
using namespace std;
int start()
{
//code here
return 0;
}
the start(); 开始(); in main.cpp will use start();
在main.cpp中将使用start(); from ah but I want it to use start();
从啊,但我希望它使用start(); from bh How do I do to select start();
from bh我该如何选择start(); in bh?
在bh?
Assuming that the functions are defined in the respective .cpp
files, ie, one is defined in a.cpp
and one in b.cpp
, then this cannot happen. 假定函数在相应的
.cpp
文件中定义,即,一个在a.cpp
定义,一个在b.cpp
中b.cpp
,则这不会发生。 Once you try to link your code, you will get the error that start()
is defined twice. 尝试链接代码后,将收到错误,两次定义了
start()
。 Therefore, you do not have to reason about how to call one of them; 因此,您不必推理如何调用其中之一。 the code will not link anyway unless the two functions are the same (ie defined in the same cpp file).
除非两个函数相同(即在同一个cpp文件中定义),否则代码将不会链接。 If this is the case, then it doesn't matter which one is called (as there is only one).
如果是这种情况,那么调用哪一个都没有关系(因为只有一个)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.