CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Maps

  1. #1
    Join Date
    Jan 2008


    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....

    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...

    #include <iostream>
    #include <map>
    using namespace std;
    const int LineMax = 72;
    const int NULLCHAR = '\0';
    typedef char LineType[LineMax];
    class LineClass
    	  LineType Info;	
    	  int recordNum;
          LineClass(LineType Str, int recNum);
          void GetLine(LineType Line) const;
    	  int  GetRecordNum() const {return recordNum;};
       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;
       if (strcmp(Left, Right) < 0)
          return true;
          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;
       // 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;
          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)));
          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)));

  2. #2
    Join Date
    Aug 2000
    West Virginia

    Re: Maps

     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.

       p = Dictionary.find(LineClass(Target,0));      // <===========LINE88
    // OR
          LineClass(LineType Str, int recNum=0);

  3. #3
    Join Date
    Jul 2002
    Portsmouth. United Kingdom

    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

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)