p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   BOOK: Ivor Horton's Beginning Visual C++ 2005 (http://p2p.wrox.com/forumdisplay.php?f=249)
-   -   LinkedList<T>, AddTail, EX9_22, pages 557-559 (http://p2p.wrox.com/showthread.php?t=45039)

Nick Y July 10th, 2006 08:59 AM

LinkedList<T>, AddTail, EX9_22, pages 557-559
 
An error caused by use of AddTail(), which I think can only be used with integers.
The following is returned when compiling EX9_22:

error C2039: 'AddTail' : is not a member of 'System::Collections::Generic::LinkedList<T>'
        with
        [
            T=double
        ]


The solution is to replace AddTail() with AddLast()

AddTail is in the 'Code from the book' and hence EX9_22 fails to compile.

=====================
Hence: Page 557 would appear to be incorrect with regards references to both AddHead() and AddTail(), which require integers. Replace respectively with AddFirst() and AddLast() for doubles.

You could add values to the list like this:

       for(int i = 0 ; i<1000 ; i++)
       values->AddLast(2.5*i);

The AddLast() function adds an item to the end of the list. You can add items to the beginning of the list by using the AddFirst() function. Alternatively, you can use the AddHead() and AddTail() functions
to do the same things.


=====================
Page 559 (EX9_22)
Change from

// Using LinkedList<T>
  Console::WriteLine(L"\nCreating a LinkedList<T> of double values:");
  LinkedList<double>^ values = gcnew LinkedList<double>;
  for(int i = 0 ; i<1000 ; i++)
  values->AddTail(2.5*i);


to

 // Using LinkedList<T>
  Console::WriteLine(L"\nCreating a LinkedList<T> of double values:");
  LinkedList<double>^ values = gcnew LinkedList<double>;
  for(int i = 0 ; i<1000 ; i++)
  values->AddLast(2.5*i);

=====================

Additionally: Page 561, third paragraph should replace AddTail() with AddLast() and hence ignore the sentence following ('You could equally well use the AddLast() function to do the same thing.')

abilify January 26th, 2010 07:03 PM

error C2248
 
I get this error for the following code and would like to know what's wrong[:confused:] I'm using standard 2008


Error 1 error C2248: 'System::Collections::Generic::Dictionary<TKey,TVa lue>::values' : cannot access private member declared in class 'System::Collections::Generic::Dictionary<TKey,TVa lue>' c:\Users\Dan\Documents\Visual Studio 2008\Projects\Chapter9\Ex9_22a\Ex9_22a\Ex9_22a.cpp 90 Ex9_22a

Code:


// Ex9_22.cpp : main project file.
// Using generic collection classes

#include "stdafx.h"

using namespace System;
using namespace System::Collections::Generic;                //For generic collections

//class encapsulating a name
ref class Name
{
public:
        Name(String^ name1, String^ name2) : First(name1), Second(name2){}
        virtual String^ ToString() override
        {
                return First + L" " + Second;
        }
private:
        String^ First;
        String^ Second;
};

//Class encapsulating a phone number
ref class PhoneNumber
{
public:
        PhoneNumber(int area, int local, int number):
          Area(area), Local(local), Number(number) {}
        virtual String^ ToString() override
        {
                return Area + L" " + Local + L" " + Number;
        }

private:
        int Area;
        int Local;
        int Number;
};

int main(array<System::String ^> ^args)
{
    //Using List<T>
        Console::WriteLine(L"Creating a List<T> of integers:");
        List<int>^ numbers = gcnew List<int>;
        for( int i = 0 ; i < 1000; i++)
                numbers->Add(2*i+1);

        //Sum the contents of the list
        int sum = 0;
        for(int i = 0; i < numbers->Count; i++)
                sum += numbers[i];
        Console::WriteLine(L"Total = {0}", sum);

        //Using LinkedList<T>
        Console::WriteLine(L"\nCreating a LinkedList<T> of double values:");
        LinkedList<double>^ values = gcnew LinkedList<double>;
        for(int i = 0; i < 1000; i++)
                values->AddLast(2.5*i);

        double sumd = 0.0;
        for each(double v in values)
                sumd += v;

        Console::WriteLine(L"Total = {0}", sumd);

        LinkedListNode<double>^ node = values->Find(20.0);                //Find node containing 20.0
        values->AddBefore(node, 19.9);
        values->AddAfter(values->Find(30.0), 30.1);

        //Sum the contents of the linked list again
        sumd = 0.0;
        for each(double v in values)
                sumd += v;

        Console::WriteLine(L"Total after adding values = {0}", sumd);

        //Using Dictionary<K,V>
        Console::WriteLine(L"\nCreating a Dictionary<K,V> of name/number pairs:");
        Dictionary<Name^, PhoneNumber^>^ phonebook = gcnew Dictionary<Name^, PhoneNumber^>;

        //Add name/number pairs to dictionary
        Name^ name = gcnew Name("Jim", "Jones");
        PhoneNumber^ number = gcnew PhoneNumber(914, 316, 2233);
        phonebook->Add(name, number);
        phonebook->Add(gcnew Name("Fred", "Fong"), gcnew PhoneNumber(123, 234, 3456));
        phonebook->Add(gcnew Name("Janet", "Smith"), gcnew PhoneNumber(515, 224,6864));

        //List all numbers
        Console::WriteLine(L"List all the numbers:");
        for each(PhoneNumber^ number in phonebook->values)
                Console::WriteLine(number);

        //List all names and numbers
        Console::WriteLine(L"Access the keys to list all name/number pairs:");
        for each(Name^ name in phonebook->Keys)
                Console::WriteLine(L"{0} : {1}", name, phonebook[name]);

    return 0;
}



All times are GMT -4. The time now is 11:10 AM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.