I have following function in my include (.h
) file:
string genPassword(char[] alphabet = {}, int length=0)
{
string s = "";
for (int i = 0; i < length; ++i) {
s += alphabet[rand() % (sizeof(alphabet) - 1)];
}
random_shuffle(s.begin(), s.end());
return s;
}
When I try to compile it with g++
(to check is everything working) I see following errors:
g++ -Wall -c "passfunctions.h" (w katalogu /home/bla/programs)
passfunctions.h:22:27: error: expected ‘,’ or ‘...’ before ‘alphabet’
string genPassword(char[] alphabet = {}, int length=0)
^
passfunctions.h: In function ‘std::string genPassword(char*)’:
passfunctions.h:25:23: error: ‘length’ was not declared in this scope
for (int i = 0; i < length; ++i) {
^
passfunctions.h:26:14: error: ‘alphabet’ was not declared in this scope
s += alphabet[rand() % (sizeof(alphabet) - 1)]; ^
Compilation failed.
I do not understand those errors. Why semicolon is expected before alphabet
in function declaration?...
Why two next errors shows the variables declared in function are "not declared in this scope"? Please explain me what do I do wrong.