No matching function to call?

Hi,

The random method in here should return a random value inside given array.
The program constantly crashes when I send the array object as full.
It will only allow for you to choose the first 4 objects in the array, if you choose object number 8, for example, the program crashes.
So Im trying to send the pointer to the array instead. But now the compiler claims not to know of any such function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
    static string getRandom(string* avalible[]){
        cout << "Get random";
         cout << "index: " << (*avalible)->length() << endl;
        int index = random((*avalible)->length()-1);
        cout << "returned" + index;
        cout << "index: " << (*avalible[index]) << endl;
        return (*avalible[index]);
    }

    static string getWorldName(){
        string first[10] = {"Wicked","Epic","Blue","Horrible","Soft","Lol","Fallen","Wild","Calm","Fierce"};
        string second[9] = {"Corn","Horse","Man","Cheese","Forest","Shade","Box","Waterwheel","Village"};
        string third[3] = {"Mountains","Fields","Shores"};
        string world_name = getRandom(&first) + " " + getRandom(&second) + " " + getRandom(&third);
        return world_name;
    }

I can remove the "10" from the string first[10] but it doesn't make a difference.
What is wrong with this?
The "length" you are getting in getRandom is the length of the first string in the array. It doesn't have anything to do with the number of strings that are in the array.

Also getRandom requires an array of pointers to strings, and you're trying to feed it a pointer to an array of strings.

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
27
28
29
30
31
32
33
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>

using namespace std;

int random(unsigned limit)
{
    return rand() % limit;
}

string getRandom(string s[], unsigned size)
{
    return s[random(size)];
}

static string getWorldName(){
    string first[10] = { "Wicked", "Epic", "Blue", "Horrible", "Soft", "Lol", "Fallen", "Wild", "Calm", "Fierce" };
    string second[9] = { "Corn", "Horse", "Man", "Cheese", "Forest", "Shade", "Box", "Waterwheel", "Village" };
    string third[3] = { "Mountains", "Fields", "Shores" };

    string world_name = getRandom(first, 10) + " " + getRandom(second, 9) + " " + getRandom(third, 3);
    return world_name;
}

int main()
{
    std::srand(time(0));

    for (unsigned i = 0; i < 10; ++i)
        std::cout << getWorldName() << '\n';
}


http://ideone.com/oqg78r
Last edited on
Thanks man! You saved my life! :D
Topic archived. No new replies allowed.