Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ compile multiple files with same function name

Tags:

c++

c

In the printNumbers.h file

//Write a function getNumber() and put your favourite number inside! 
//I will then compile the program and print everyone's favourite number!
int getNumber(void);

If i want something like to keep printing out three people's favourite numbers

int main() {
    int counter = 0;
    while(counter < 9999) {
         if(counter % 3 == 0) {
             //Print out the first person's number
             printf("My number is %d\n", getNumber();
         } else if (counter % 3 == 1) {
             //Print out the second person's number
             printf("My number is %d\n", getNumber();
         } else {
             //Print out the third person's number
             printf("My number is %d\n", getNumber();
         }

         //Keep printing everyone's numbers until counter reaches 9999

         counter++;
    }
    return 0;
}

3 people give me C++ files they wrote and I rename each to A.cpp B.cpp C.cpp. It could be in random order so they don't know if their file will end up being A B or C

Example of the c++ file they could have written

int getNumber() {
    //Each person can write their own function to return whatever number they want
    int myNumber = 1;
    return myNumber;
}

Is there anyway, for me to compile the programs and use getNumber() from A.cpp for the first call, from B.cpp for the second call and from C.cpp from the third call, then repeat.

I could name the function names in the while loop getNumber1() getNumber2() getNumber3() and change the function names in the three C++ files but when someone writes the getNumber() function they don't know if it will end up being A B or C. Everyone's getNumber() function should be called getNumber().

.

EDIT: Lots of people are suggesting namespaces. I have no control over the .h file and the ADT the people are given, so the program they write wont use namespaces. Also many people might submit C++ files to me and I have to randomly group theem into groups of 3 (since my program can only print 3 people's numbers) so they don't know what namepspace to use. .

EDIT: A few people are suggesting modifying the .h file or the program the people write. We all get given the .h file and we cannot change it. We submit the files(like the example) to the school and they are able to do what my main function does (I have no idea how they do it). My main function will allow the users to test their getNumber() function better before they submit, so it is a separate program to the school that the users can optionally use.

If anything seems confusing or unclear please ask me questions to clarify my question!

Thank you.

like image 653
Friedpanseller Avatar asked Mar 25 '26 06:03

Friedpanseller


2 Answers

Assuming you are getting their cpp files, which you do not want to change, instead of compiling and linking them, you could include them in your cpp, like this

namespace A {
  #include "A.cpp"
}
namespace B {
  #include "B.cpp"
}
namespace C {
  #include "C.cpp"
}

int main ()
{
  A::getNumber();
  B::getNumber();
  C::getNumber();
}
like image 51
Karsten Koop Avatar answered Mar 26 '26 21:03

Karsten Koop


There is no way to put multiple different functions with identical name and parameter lists into global namespace, even from different translation units.

However, C++ lets you use namespaces to distinguish among identically-named functions:

a.cpp

namespace a {
    int getNumber() {
        return 1;
    }
}

b.cpp

namespace b {
    int getNumber() {
        return 205;
    }
}

The caller can distinguish between getNumber functions by using context resolution operator:

 if(counter % 3 == 0) {
     //Print out the first person's number
     printf("My number is %d\n", a::getNumber();
 } else if (counter % 3 == 1) {
     //Print out the second person's number
     printf("My number is %d\n", b::getNumber();
 } else {
     //Print out the third person's number
     printf("My number is %d\n", c::getNumber();
 }
like image 22
Sergey Kalinichenko Avatar answered Mar 26 '26 21:03

Sergey Kalinichenko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!