简体   繁体   中英

No reference parameter for bool function in C++?

So I was writing a program, and one of the functions is to determine whether the character is a vowel or not. The program is as follows:

bool is_vowel(string s)
{
    if (s == "A" || s == "a") return true;
    if (s == "E" || s == "e") return true;
    if (s == "I" || s == "i") return true;
    if (s == "O" || s == "o") return true;
    if (s == "U" || s == "u") return true;
    if (s == "Y" || s == "y") return true;

    return false;
}

So my question comes out when I tried to turn the string s into a reference parameter string& s. After the change, whenever I tried to call this function the program (I'm using Xcode on Mac btw) tells me "No matching function for call to 'is_vowel'" , even when the object inside IS a string object. So why can't I use a reference parameter here? Isn't the "s" referred to whichever string I use to call this function? I used reference parameters for most of the functions in which I'm not changing anything because I think referring instead of copying the value into a new parameter may be more efficient. So why it won't work here? BTW, is it true that "referring instead of copying the value into a new parameter is more efficient"?

Edit: per the request of many, I'll just add one other function that calls this one; and for the purpose of simplicity, I've cut off a big chunk of the code. So don't dwell on logic of this part too much.

int FRI_Syllables(vector<string>& s)
{
    int syllables = 0;

    for (int i = 0; i < s.size(); i++)
    {
        string word = s[i];

        for (int n = 0; n < word.length(); n++)
        {
            if (is_vowel(word.substr(n, 1)))
                syllables ++; //Rule 1: count each vowel as a syllable
        }
    }

    return syllables;
}

And as for the change to the bool function, everything else is the same, except the first line is

bool is_vowel(string& s)

and the error Xcode gives me is "No matching function for call to 'is_vowel'".

First of all, you would like to use const reference as opposed to reference when you do not change the value like in this particular case.

Secondly, string for a character is an overkill.

Thirdly, the code works just fine for me as follows:

#include <iostream>
#include <string>

using namespace std;

bool is_vowel(const string &s)
{
    if (s == "A" || s == "a") return true;
    if (s == "E" || s == "e") return true;
    if (s == "I" || s == "i") return true;
    if (s == "O" || s == "o") return true;
    if (s == "U" || s == "u") return true;
    if (s == "Y" || s == "y") return true;

    return false;
}

int main()
{
    string mystring = "b";
    string mystring2 = "A";
    cout << is_vowel(mystring) << endl;
    cout << is_vowel(mystring2) << endl;
    return 0;
}

I can reproduce your issue if I just pass a string literal as follows:

main.cpp:24:25: error: invalid initialization of non-const reference of type 'std::string& {aka std::basic_string&}' from an rvalue of type 'const char*' cout << is_vowel("f") << endl;

If that is your case, this is another reason for using const reference instead of reference. You could use value semantics, too, but I agree with you about your reference conclusion.

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