I was trying to create a simple AI based on some tutorial (the code is not mine, so it should be working), but it won't compile.
Here are my files:
voice.h
1 2 3 4 5 6 7 8 9 10 11 12 13
#ifndef _VOICE_H
#define _VOICE_H
#include <iostream>
usingnamespace std;
class Voice {
public:
void say(string phrase); // Used to textually and audibly communicate a phrase
};
#endif
learner.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#ifndef _LEARNER_H
#define _LEARNER_H
#include <iostream>
#include <fstream>
#include "voice.h"
usingnamespace std;
class Learner {
public:
void respond(string phrase); // Used to get, or teach a response
void say(string phrase); // Used to textually and audibly communicate a phrase
Voice voice; // The learner's voice that will audibly communicate a response
};
#endif
voice.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include "Voice.h"
#include <iostream>
#include <windows.h>
usingnamespace std;
/*
The following function textually and audibly communicates a phrase.
The open source eSpeak speech synthesizer is used to create the audible message.
If the eSpeak exe is not located in the directory, no audible message will be heard.
*/
void Voice::say(string phrase){
string command = "espeak \"" + phrase + "\""; // Concat the phrase to the command
constchar* charCommand = command.c_str(); // Convert to a const char*
cout << phrase << endl; // Textually output phrase
system(charCommand); // Send the command to cmd to execute espeak with the phrase an argument
}
#include "learner.h"
#include <iostream>
#include <fstream>
usingnamespace std;
/*
The following function will look for the passed phrase in the memory file.
If there is a match, the accompanying response, stored below the initial phrase,
will be outputed.
If the response cannot be found, the learner will repeat the phrase, and prompt
the user to enter an ideal response. This response will be stored in the memory
file along with the initial phrase.
*/
void Learner::respond(string phrase){
fstream memory;
memory.open("memory/memory.txt", ios::in); // Open the memory file for input
// Search through the file until the end is reached
while( !memory.eof() ){ // While not at end of file
string identifier;
getline(memory,identifier); // Get next phrase
if(identifier == phrase){ // Is it the phrase we are looking for
string response;
getline(memory,response); // If so, get the response
voice.say(response); // Textually and audibly output the response!
return; // Leave the function
}
}
memory.close(); // Looks like we couldn't find the phrase in memory. Close the file!
memory.open("memory/memory.txt", ios::out | ios::app); // Now open for output, and append at end of file
memory << phrase << endl; // Record initial phrase in memory
voice.say(phrase); // Repeat the phrase the user entered
string userResponse;
cout << "YOU: ";
getline(cin, userResponse); // Get the ideal response
memory << userResponse << endl; // Write the ideal response to memory
memory.close(); // Close the file!
}
/*
This function simply communicates a phrase textually and audibly
*/
void Learner::say(string phrase){
this->voice.say(phrase);
}
#include <iostream>
#include "learner.h"
usingnamespace std;
main(){
Learner AI; // Create a learner object
/*
The following is the main loop. It will continue until the application is closed.
The user enters their input, and then the learner will respond.
*/
for(;;){
cout << "\nYOU: "; // User prompt
string phrase;
getline(cin, phrase); // Using getline for multi word input, then store in phrase.
cout << "COMPUTER: ";
AI.respond(phrase); // Pass the user input to the learner and see if he can respond
}
}
When I try to compile one of the .cpp files it gives me a couple of "undefined reference to" errors. What am I doing wrong? I'n using Dev C++ 5.11