Function Parameter's

I typed in a function and added the missing main().

The book say's that the function can be called through:

auto index = find_char(s, 'c', ctr);

I suspected i would hit error's but cant work this out due to lack of a full program:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <string>

std::string::size_type find_char(const std::string &s, char c, std::string::size_type &occurs)
{
    auto ret = s.size();
    occurs = 0;
    for (decltype(ret) i = 0; i != s.size(); ++i)
    {
        if (s[i] == c)
        {
            if (ret == s.size())
                ret = i;
            ++occurs;
        }
    }
    return ret;
}

int main()
{
    int ctr = 0;
    const std::string s = "clock";
    auto index = find_char(s, 'c', ctr);
    std::cout << ctr;
}
And the errors are? What happens when you compile the program?
||=== String Size Type, Debug ===|

G:\DCSTemp\String Size Type\main.cpp||In function 'int main()':|

G:\DCSTemp\String Size Type\main.cpp|24|error: invalid initialization of reference of type 'std::basic_string<char>::size_type& {aka unsigned int&}' from expression of type 'int'|

G:\DCSTemp\String Size Type\main.cpp|4|error: in passing argument 3 of 'std::basic_string<char>::size_type find_char(const string&, char, std::basic_string<char>::size_type&)'|

G:\DCSTemp\String Size Type\main.cpp|24|error: unable to deduce 'auto' from '<expression error>'|

G:\DCSTemp\String Size Type\main.cpp|24|warning: unused variable 'index' [-Wunused-variable]|

||=== Build finished: 3 errors, 1 warnings (0 minutes, 0 seconds) ===|
1
2
3
4
5
6
7
int main()
{
    std::string::size_type ctr = 0;
    const std::string s = "clock";
    auto index = find_char(s, 'c', ctr);
    std::cout << ctr;
}
Last edited on
Thank's L B.
"Thank" does not own me ;)
Last edited on
Topic archived. No new replies allowed.