Home > Software design >  C Call of overloaded function is ambiguous
C Call of overloaded function is ambiguous

Time:09-26

I am trying to make a code applying function overload. The two functions are supposed to be distinguished by the type of input and output variables. (The user either inputs an element symbol (std::string) and gets the atomic number (int) or vice-versa). I tried to apply this to my code, but I get the error message that the call of the overloaded function is ambiguous. I don't understand what I could change; as I have my code now, I am converting the variables applying to the given case to the correct type, so why is it still ambiguous?

The error I get is:

/var/lib/cxrun/projectfiles/main.cpp: In function 'int main()': /var/lib/cxrun/projectfiles/main.cpp:40:48: error: call of overloaded 'lookup(int)' is ambiguous (std::string)output = lookup(std::stoi(input)); ^ /var/lib/cxrun/projectfiles/main.cpp:25:13: note: candidate: 'std::__cxx11::string lookup(int)' std::string lookup(int atomicNumber); ^~~~~~ /var/lib/cxrun/projectfiles/main.cpp:12:15: note: candidate: 'std::__cxx11::string exercises::lookup(int)' std::string lookup(int atomicNumber); ^~~~~~ /var/lib/cxrun/projectfiles/main.cpp:45:48: error: call of overloaded 'lookup(std::__cxx11::string)' is ambiguous std::stoi(output) = lookup((std::string)input); ^ /var/lib/cxrun/projectfiles/main.cpp:24:5: note: candidate: 'int lookup(std::__cxx11::string)' int lookup(std::string name); ^~~~~~ /var/lib/cxrun/projectfiles/main.cpp:11:7: note: candidate: 'int exercises::lookup(std::__cxx11::string)' int lookup(std::string name); ^~~~~~

// use functions from the standard library (like cout)
#include <iostream>
#include <cctype>
#include <string>
#include <iomanip>

// to avoid name-clashes, create a namespace
namespace exercises
{
  // add elements   lookup functionality
  int lookup(std::string name);
  std::string lookup(int atomicNumber);
  
} /* end of namespace exercises */

// I want to use the functions from our new namespace...
using namespace exercises;

//arrays
std::string t[4] = {"H", "Rh", "Cl", "C"};
int s[4] = {1, 45, 17, 6};

//Function declarations
int lookup(std::string name);
std::string lookup(int atomicNumber);

int main(void)
{
//declare input and output variable as string temporarily 
std::string input;
std::string output;

//lookup something
std::cout << "Enter element or atomic number to look up:" << std::endl;
std::cin >> input;

//if input is atomic number, make it of type integer to match function input type
if(std::isdigit(input[0])){
  //std::stoi(input);
  (std::string)output = lookup(std::stoi(input));
}
//else, make output of type integer
else{
  //std::stoi(output);
  std::stoi(output) = lookup((std::string)input);
};
//std::string output;
//output = lookup(input);

if(std::isdigit(output[0])){
  if(std::stoi(output) == 1){
    std::cout << "Invalid input!" << std::endl;
  };
  return 0;
};

if(output == "error"){
  std::cout << "Invalid input!" << std::endl;
  return 0;
}

//print output
std::cout << "|**Sample Input**|**Sample Output**|" << std::endl;
std::cout << "|" << std::setfill('-') << std::setw(16) << "|" << std::setfill('-') << std::setw(16) << "|" << std::endl;
std::cout << "|" << std::left << std::setw(16) << input << "|" << std::left << std::setw(16) << output << "|" << std::endl;

return 0;
}

//function definition element symbol --> atomic number
int lookup(std::string input){
  int output; 
  //check if input is valid, ie if exists in array t
  for(unsigned int i=0; i<sizeof(t); i  ){
    if(input == t[i]){
      output = s[i];
      
  return output;
    };
  };
  output = 1;
  return output;
}
  
//function definition atomic number --> element symbol
std::string lookup(int input){
  //check if input is valid, ie if exists in array s
  for(unsigned int i=0; i<sizeof(s); i  ){
    if(input == s[i]){
      std::string output;
      output = t[i];
      
  return output;
    };
  };
  std::string output = "error";
  return output;
}
 

CodePudding user response:

Your problem is that you declare the functions in the namespace excercises, then you add using namespace exercises;, after that you declare them again in the default namespace. Due to the using-directive, the compiler does not know, which version of the declaration to refer to, the ones declared inside the namespace or the ones declared in the default namespace.

The functions declared in the namespace exercises remain undefined, you only define the ones in the default namespace.

Try solving the problem first without namespace usage, then add the namespace but do not use the using namespace declaration.

  • Related