简体   繁体   English

C ++如何同时从另一个文件中调用与另一个文件中的函数同名的函数?

[英]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.cppb.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.

相关问题 如何从C ++中的另一个头文件调用函数? - How to call a function from another header file in C++? 如何从C ++调用另一个文件中定义的python函数? - How to call a python function defined in another file from c++? 在android ndk中的同一.cpp文件上调用另一个C ++函数 - Call another C++ function on the same .cpp file in android ndk 如何从另一个文件中的函数调用main的同一文件中的函数 - How to call a function in the same file of main from a function in another file C++ 从另一个 cpp 文件调用内联函数 - C++ call inline function from another cpp file 如何在一个文件中用C ++定义一个函数并在另一个文件中调用它? - How to define a function in C++ in one file and call it in another one? 尝试从另一个头文件(C ++)调用函数时出错 - Getting Error When Trying to Call Function from Another Header File (C++) 从另一个文件调用一个函数到另一个函数C ++ - Calling a function from another file into another function C++ 从另一个文件调用C ++函数时出现“未定义符号” - “Undefined Symbols” when calling C++ function from another file 在 C/C++ 中,当两个函数具有相同的名称时它会起作用,一个最终调用另一个 - in C/C++, it is going to work when two function has same name, one end up call another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM