简体   繁体   中英

C++ more than one instance of overloaded function matches the argument list when creating a header file

Within my main file of my program I have the following declarations

int main()
{
    Customer c;
    Part p;
    Builder b;
    auto partsVec =  readpartFile();
    auto customerVec = readcustomerFile();
    auto builderVec = readbuilderFile();

    fexists("Parts.txt");
    complexity(c, partsVec);
    robotComplexity(partsVec,customerVec);
    writeFile(buildAttempt(b, complexity(c, partsVec), variability(customerVec, builderVec)));
}

My header file consists of the following

vector<Part> readpartFile();

vector<Customer> readcustomerFile();

vector<Builder> readbuilderFile();

float complexity(const Customer& c, const std::vector<Part>& parts);

void robotComplexity(vector<Part> vecB, vector<Customer> vecC);

double variability(const vector<Customer>& customerList, const vector<Builder>& builderList);

vector<double> buildAttempt(Builder b, double variaiblity, double complexityRobot);

void writeFile(vector<double> build);

All functions link up except for robotComplexity. My declaration of this function in main creates the following error.

more than one instance of overloaded function "robotComplexity" matches the argument list: -- function "robotComplexity(const std::vector> &parts, const std::vector> &customers)" -- function "robotComplexity(std::vector> vecB, std::vector> vecC)" -- argument types are: (std::vector>, std::vector>)

im not sure why im getting this error or how to resolve it

You have a mismatch between declaration in header and definition (which also serves as declaration):

  • void robotComplexity(vector<Part> vecB, vector<Customer> vecC);
  • void robotComplexity(const vector<Part>& vecB, const vector<Customer>& vecC);

Whereas parameter names can mismatch, types shouldn't, else, you create another overload.

Jarod42's Answer is quite correct, but I want to add to it and provide a helpful methodology for future C++ coding.

When function signatures differ, the compiler will not complain. At best, you will receive a cryptic or hard to read linker error. This is especially common with inheritance and virtual functions. Even worse, sometimes they will work but actually call a new function with a different signature on a child class that doesn't match the base class.

There is a keyword in C++11 (and newer versions) called override . Here is a more in depth discussion about what they keyword does: OverrideKeywordLink

When this keyword is used on a virtual function, it becomes a compile error when the intended virtual function override does not match the base class function signature. Also, it gives a very human readable reason why the function override did not occur.

You mentioned in a comment that 'How come const made it different"? The answer is it all comes down to function signatures. Consider these two functions within a class.

class MyClass {
    int CalculateSomething();
    int CalculateSomething() const;
}

The const actually changes the function signature. They are considered two different functions. The bottom line is, always try to make errors compile errors over run-time errors. Use keywords that protect you from some few tribal C++ knowledge traps that exist.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM