No matching member perform for name to ‘push_back’ vector error may come up because of conflicts between mutual and fixed sorts and the varied knowledge sorts that look related. You can see that the error assertion clearly mentions the no matching member perform displaying the compiler’s lack of ability to match the forms of the info and the vector.
In this text, you’ll find out about completely different situations that trigger this error and a few wonderful options to eradicate it completely. So, wait no extra and browse until the tip to have programming expertise.
No Matching Member Function For Call To ‘push_back’: Causes
Interchangeably utilizing constants and mutable sorts or characters and strings could cause the no matching member perform error. So, whether or not deliberately or mistakenly, in case your code confuses the compiler between the differing types, you’ll get the identical error in your display screen, indicating a confused compiler.
– The Confusion Between the Mutable Type and Constant
Having a pointer that factors towards a mutable kind and passing a pointer pointing towards a continuing to the push_back() perform can put you in bother. The interchangeable use of the mutable kind and fixed confuses the compiler concerning the kind’s actual identification, and it throws the given error.
For instance, you’ve got a mutable kind “cars” in your program. There is a vector containing a pointer that factors towards the vehicles. Next, you might be passing a continuing vehicles pointer to the push_back() perform.
Note that the fixed pointer doesn’t exist in your code. As a end result, you’ll obtain the no matching perform for name to ‘std::vector error.
Here is the code that depicts the identical state of affairs:
class theCarShop
{ public: // declaring the add_car methodology bool add_car(automotive const &c); // making a vector with a mutable vehicles pointer personal: std::vector<automotive*> vehicles; }; // implementing the add_car methodology bool theCarShop::add_car(automotive const & c){ // calling the push_back() perform with a continuing vehicles pointer vehicles.push_back(&c); // the error will happen right here return true; } |
Similarly, take into consideration a state of affairs wherein you’ve got a vector containing a continuing vehicles pointer, however the push_back() perform is receiving a non-constant pointer to the “cars” kind.
In such a situation, the identical battle between the fixed and mutable kind will come up, and also you’ll obtain the no matching member error. The reference code is connected under for a greater understanding:
class theCarShop
{ public: // declaring the add_car methodology bool add_car(automotive &c); // making a vector with a continuing vehicles pointer personal: std::vector<const automotive*> vehicles; }; // implementing the add_car methodology bool theCarShop::add_car(automotive & c){ // calling the push_back() perform with a mutable vehicles pointer vehicles.push_back(&c); // the inaccurate line of code return true; } |
Keep in thoughts that mismatching members also can result in the next errors.
- No matching member perform for name to ‘insert.’
- No matching member perform for name to resize.
- No matching member perform for name to ‘erase.’
- No matching member perform for name to pathto errors.
– A Conflict Between Character and String Data Types
If a vector in your program expects a string and also you cross a personality to it whereas utilizing the push_back() perform, you’ll see the vector push_back C++ error in entrance of you. You can’t use the string and character kind of values interchangeably.
Imagine having a string variable “vowels” containing “aeiou” and a vector of string knowledge kind “vowelvector.” Now, you need to cross a selected vowel from the identical string to the vector through the use of the push_back() perform.
Therefore, you add an index worth, reminiscent of 3, after specifying the vowels string. Here, solely the “o” character might be fetched from the string and handed to the push_back().
Consequently, the compiler will present you an error indicating a non-matching member perform. The normal script for the mentioned state of affairs will be seen under:
#embody <iostream>
#embody <fstream> #embody <string> #embody <vector> std::string vowels = “aeiou”; std::vector<string> vowelvector; // the problematic line of code vowelvector.push_back(vowels[3]) |
How To Fix the No Matching Member Function For Call To ‘push_back’?
– Maintain Consistency: Go for Either Mutable or Constant
Maintaining consistency between the mutable sorts and constants will help you eradicate the acknowledged error. If you’ve got created a mutable pointer, then you must cross a mutable pointer to the push_back() perform. Pass no matter exists, and keep away from no matter is inexistent.
Joining the thread with the earlier examples, you possibly can take away the error by creating a continuing pointer or passing a mutable pointer to the push_back() C++.
Here are the modified code snippets that’ll work simply positive.
Snippet primary:
class theCarShop
{ public: // declaring the add_car methodology bool add_car(automotive &c); // making a vector with a mutable vehicles pointer personal: std::vector<automotive*> vehicles; }; // implementing the add_car methodology bool theCarShop::add_car(automotive & c){ // calling the push_back() perform with a mutable vehicles pointer vehicles.push_back(&c); return true; } |
Snippet quantity two:
class theCarShop
{ public: // declaring the add_car methodology bool add_car(automotive const &c); // making a vector with a continuing vehicles pointer personal: std::vector<const automotive*> vehicles; }; // implementing the add_car methodology bool theCarShop::add_car(automotive const & c){ // calling the push_back() perform with a continuing vehicles pointer vehicles.push_back(&c); return true; } |
– Call emplace_back() Instead of push_back()
You can use the emplace_back() perform as an alternative of the push_back() perform to do away with the identical error. The former perform accepts the depend of repetitions and the required character. Consequently, it’ll convert the required character right into a string primarily based on the variety of repetitions.
Please contemplate the vowels string and the vowelvector created earlier. You can get your required end result through the use of the emplace_back() perform. You’ll have to cross 1 and the vowels[3] as arguments to the given perform. The vector will settle for the newly generated one-character string, and also you gained’t face any errors.
The coding illustration of the emplace_back() perform doing wonders has been supplied under.
#embody <iostream>
#embody <fstream> #embody <string> #embody <vector> std::string vowels = “aeiou”; std::vector<string> vowelvector; vowelvector.emplace_back(1, vowels[3]) |
– Leverage the std::string Constructor
You ought to leverage the std::string constructor to generate a string out of character and kill the error in your system. Using this method, you gained’t want to modify to the emplace_back() perform. Thus, you possibly can proceed utilizing push_back() whereas staying away from the given error.
For instance, you’ve got a string “name” initialized to “James.” Now, you need to get its first character and cross it to the namevector of the string kind.
In this case, you should utilize the std::string constructor that accepts the repetition depend and a personality just like the emplace_back() perform. Next, you’ll have to wrap the constructor contained in the push_back() perform to carry out your activity.
Here is the code snippet that codes the above instance and works fairly effectively.
#embody <iostream>
#embody <fstream> #embody <string> #embody <vector> std::string title = “James”; std::vector<string> namevector; vowelvector.push_back(std::string(1, vowels[3])); |
Conclusion
The error within the title happens because of mismatching forms of the info and the vector. The conflicting kind handed to push_back() perform not being accepted by the vector is the core trigger. You can learn the necessary particulars of the publish within the following listicle:
- Maintaining consistency between the forms of knowledge and the vector is the important thing to resolving the no-matching member error.
- Replacing push_back() with emplace_back() will help convert a personality right into a string to repair the given error.
- You can go for the std::string constructor to clear the battle between the character and string knowledge sorts and take away the error.
Lastly, don’t neglect that you simply can’t push a contrasting kind of information to the vector no matter your vector’s kind.
References
- https://stackoverflow.com/questions/52734630/no-matching-member-function-for-call-to-push-back-error
- https://www.reddit.com/r/cpp_questions/feedback/n4o1ho/no_matching_error_for_function_call_to_push_back/