|
-
April 1st, 2008, 04:43 AM
#1
Maps
Here is an example by author:
---------------------------------------------
/* Filename: diction.cpp
Author: Br. David Carlson
Date: July 14, 1999
Last Revised: December 23, 2001
This interactive program creates a map (table), where each entry
is a pair containing a word and its definition. Thus the map is
a small dictionary. The program then allows the user to repeatedly
look up the meaning of words.
In the code, change CTRL z to CTRL d for Linux.
*/
and the website if your keen to see the real code....
http://cis.stvincent.edu/html/tutori...maps/maps.html
and finally the code with the small change I made that gave me an error at line number 88...I don't understand why the compiler wont compile. All I did was add another member to the private section called recNum. The error says that it cannot convert char[] to class LineClass...
Code:
#include <iostream>
#include <map>
using namespace std;
const int LineMax = 72;
const int NULLCHAR = '\0';
typedef char LineType[LineMax];
class LineClass
{
private:
LineType Info;
int recordNum;
public:
LineClass(void);
LineClass(LineType Str, int recNum);
void GetLine(LineType Line) const;
int GetRecordNum() const {return recordNum;};
};
LineClass::LineClass(void)
{
Info[0] = NULLCHAR;
recordNum = 0;
}
LineClass::LineClass(LineType Str, int recNum)
: recordNum(recNum)
{
strcpy(Info, Str);
}
void LineClass::GetLine(LineType Line) const
{
strcpy(Line, Info);
}
bool operator<(LineClass LHS, LineClass RHS)
{
LineType Left, Right;
LHS.GetLine(Left);
RHS.GetLine(Right);
if (strcmp(Left, Right) < 0)
return true;
else
return false;
}
// Function prototypes:
void LoadData(map<LineClass, LineClass> & Dictionary);
void Lookup(LineType Target, map<LineClass, LineClass> & Dictionary);
int main(void)
{
map<LineClass, LineClass> Dictionary;
LineType Target;
LoadData(Dictionary);
// Change to CTRL d for Linux:
cout << "Enter word to look for (or CTRL z to quit): ";
cin >> Target;
while (! cin.fail())
{
Lookup(Target, Dictionary);
// Change to CTRL d for Linux:
cout << "Enter word to look for (or CTRL z to quit): ";
cin >> Target;
}
return 0;
}
void Lookup(LineType Target, map<LineClass, LineClass> & Dictionary)
{
map<LineClass, LineClass>::iterator p;
LineType Meaning;
p = Dictionary.find(LineClass(Target)); // <===========LINE88
//p is pointing to the found target object
if (p != Dictionary.end())
{
p->second.GetLine(Meaning); //second is the mapped value so the mapped value is also a class.
cout << "Meaning is: " << Meaning << endl;
}
else
cout << "Word not found" << endl;
}
void LoadData(map<LineClass, LineClass> & Dictionary)
//the dictionary is holding a list/map of lineClass'es
{
// Note the 2 different ways of creating a pair to insert:
Dictionary.insert(pair<LineClass, LineClass>(LineClass("hot", 1),
LineClass("having a high temperature", 1)));
Dictionary.insert(make_pair(LineClass("cold",1),
LineClass("having a low temperature" ,1)));
Dictionary.insert(make_pair(LineClass("jog" ,1),
LineClass("run slowly", 1)));
Dictionary.insert(make_pair(LineClass("intelligent", 1),
LineClass("smart", 1)));
Dictionary.insert(make_pair(LineClass("register" ,1),
LineClass("a high-speed storage location on a computer's CPU chip", 1)));
}
-
April 1st, 2008, 05:28 AM
#2
Re: Maps
Code:
p = Dictionary.find(LineClass(Target));
You do not have a constructor for LineClass that just takes a LineType
as a first argument. You either need to provide an int argument or
provide a default one in the construtor.
Code:
p = Dictionary.find(LineClass(Target,0)); // <===========LINE88
// OR
LineClass(LineType Str, int recNum=0);
-
April 1st, 2008, 05:31 AM
#3
Re: Maps
Your LineClass has constructors for zero or two parameters. The line in error is only supplying one.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|