ProofTheEngi
ProofTheEngi

Reputation: 87

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

#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;
}

the start(); in main.cpp will use start(); from a.h but I want it to use start(); from b.h How do I do to select start(); in b.h?

Upvotes: 2

Views: 2365

Answers (1)

gexicide
gexicide

Reputation: 40058

Assuming that the functions are defined in the respective .cpp files, i.e., one is defined in a.cpp and one in b.cpp, then this cannot happen. Once you try to link your code, you will get the error that start() is defined twice. 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 (i.e. defined in the same cpp file). If this is the case, then it doesn't matter which one is called (as there is only one).

Upvotes: 3

Related Questions