Trying to pass references to a function.

Hi, I'm getting an error I don't understand on line 26:

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>
#include <string>
using namespace std;

string askText(string prompt);
int askNumber(string prompt);
void tellStory(string& rName, string& rNoun, int& rNumber, string& rBodyPart, string& rVerb);

int main()
{
    cout << "Welcome to Mad Lib.\n\n";
    cout << "Answer the following questions to help create a new story.\n";

    string name = askText("Please enter a name: ");
    string noun = askText("Please enter a plural noun: ");
    int number = askNumber("Please enter a number: ");
    string bodyPart = askText("Please enter a body part: ");
    string verb = askText("Please enter a verb: ");

    string& rName = name;
    string& rNoun = noun;
    int& rNumber = number;
    string& rBodyPart = bodyPart;
    string& rVerb = verb;

    tellStory(rName, rNoun, rNumber, rBodyPart, rVerb);

    return 0;
}

string askText(string prompt)   {
    string text;
    cout << prompt;
    cin >> text;
    return text;
}

int askNumber(string prompt)    {
    int num;
    cout << prompt;
    cin >> num;
    return num;
}

void tellStory(string name, string noun, int number, string bodyPart, string verb) {
    cout << "\nHere’s your story:\n";
    cout << "The famous explorer ";
    cout << name;
    cout << " had nearly given up a life-long quest to find\n";
    cout << "The Lost City of ";
    cout << noun;
    cout << " when one day, the ";
    cout << noun;
    cout << " found the explorer.\n";
    cout << "Surrounded by ";
    cout << number;
    cout << " " << noun;
    cout << ", a tear came to ";
    cout << name << "’s ";
    cout << bodyPart << ".\n";
    cout << "After all this time, the quest was finally over. ";
    cout << "And then, the ";
    cout << noun << "\n";
    cout << "promptly devoured ";
    cout << name << ". ";
    cout << "The moral of the story? Be careful what you ";
    cout << verb;
    cout << " for.";
}


1
2
3
obj\Debug\main.o||In function `main':|
C:\Users\Calvin\Documents\C++ Projects\mad_lib\main.cpp|26|undefined reference to `tellStory(std::string&, std::string&, int&, std::string&, std::string&)'|
||=== Build finished: 1 errors, 0 warnings ===|



I just don't get it.
remove the
1
2
3
4
5
    string& rName = name;
    string& rNoun = noun;
    int& rNumber = number;
    string& rBodyPart = bodyPart;
    string& rVerb = verb;


and just pass the strings into the function

tellStory(Name, Noun, Number, BodyPart, Verb);

The & in the function definition tells the compiler to pass the variables in as referances.

so line 45 needs the & added like on line 7
Last edited on
Ah yes I forgot that you don't pass a reference, you pass a variable as/into a reference.

Thanks Moooce.
Topic archived. No new replies allowed.