Topic: C++ Primer


Author: larryh@acuson.com (Larry Hale)
Date: 1995/06/23
Raw View
alex.ewing@almac.co.uk (ALEX EWING) writes:

>I'm having problems with one of the examples in Lippman's "C++ Primer 2e" p63.  Two template classes are created - the base class defined in array.h:
>-------------------------------------------------------------------------------------------------------
>#ifndef ARRAY_H
>#define ARRAY_H

>const int ArraySize = 12; // default size

>template <class Type>
>class Array {
>public:
> // operations performed on arrays
> Array( int sz = ArraySize )
>  { size=sz; ia=new Type[ size ]; }
> virtual ~Array() { delete [] ia; }
> int getSize() { return size; }
> virtual Type& operator[]( int index ) { return ia[index]; }
>protected:
> int size;
> Type *ia;
>};
>#endif
>-------------------------------------------------------------------------------------------------------

>and ArrayRC, which adds range-checking, is derived from Array:
>-------------------------------------------------------------------------------------------------------
>#ifndef ARRAYRC_H
>#define ARRAYRC_H
>#include <assert.h>
>#include "array.h"

>template <class Type>
>class ArrayRC : public Array<Type> {
>public:
> ArrayRC(int sz = ArraySize) : Array<Type>( sz ) {};
> Type& operator[](int index) {
>  assert( index >= 0 && index < size );
>  return ia[ index ];
> }
>};

>#endif
>-------------------------------------------------------------------------------------------------------
>The sample program includes a template function,
>void swap(Array<Type> &array, int I, int j), which should be accessable by objects of both Array and Array RC. Here it is:
>-------------------------------------------------------------------------------------------------------
>#include <iostream.h>

>#include "array.h"
>#include "arrayrc.h"

>template <class Type>
>void swap( Array<Type> &array, int i, int j )
>{
> Type tmp = array[ i ];
> array [ i ] = array [ j ];
> array [ j ] = tmp;
>}

>main() {
> Array<int> ia1;
> ArrayRC<int> ia2;

> cout << "swap() with Array<int> ia1\n";
> int size = ia1.getSize();
> swap( ia1, 1, size );

> cout << "swap() with ArrayRC<int> ia2\n";
> size = ia2.getSize();
> swap( ia2, 1, size );

> return 0;
>}
>-------------------------------------------------------------------------------------------------------

>Unfortunately, this won't compile (using Borland C++ 3.1). I get this
>error message: Could not find a match for
>   'swap(ArrayRC<int>,int,int)'

>But the whole point of the example was to demonstrate that it WOULD
>match!  Now, I'm only too aware that I've possibly goofed somewhere
>in typing this in, but I really don't think so.  I also can't believe
>I'm the only one who has come across this.  Any help will be greatly
>appreciated - thanks :-).

>Alex              *+* e-mail: <alex.ewing@almac.co.uk> *+*
>---
> * 1st 2.00n #5138 * C program run.  C program crash.  C programmer quit

 Alex;

 This doesn't compile because the compiler can't find the specialized
 swap template function for an ArrayRC<int> type. I can't find where
 in Lippman this parameterized swap stuff is, but the answer to your
 riddle is on page 197 ( 2nd Edition). You must add the following for
 the compilation to succeed:

template <class Type>
void swap( ArrayRC<Type> &array, int i, int j )
{
        Type tmp = array[ i ];
        array [ i ] = array [ j ];
        array [ j ] = tmp;
}


 Keep in mind that the runtime will assertion botch. I'll leave you
 to your devices to figure out why.


        Larry H





Author: alex.ewing@almac.co.uk (ALEX EWING)
Date: 1995/06/09
Raw View
I'm having problems with one of the examples in Lippman's "C++ Primer 2e" p63.  Two template classes are created - the base class defined in array.h:
-------------------------------------------------------------------------------------------------------
#ifndef ARRAY_H
#define ARRAY_H

const int ArraySize = 12; // default size

template <class Type>
class Array {
public:
 // operations performed on arrays
 Array( int sz = ArraySize )
  { size=sz; ia=new Type[ size ]; }
 virtual ~Array() { delete [] ia; }
 int getSize() { return size; }
 virtual Type& operator[]( int index ) { return ia[index]; }
protected:
 int size;
 Type *ia;
};
#endif
-------------------------------------------------------------------------------------------------------

and ArrayRC, which adds range-checking, is derived from Array:
-------------------------------------------------------------------------------------------------------
#ifndef ARRAYRC_H
#define ARRAYRC_H
#include <assert.h>
#include "array.h"

template <class Type>
class ArrayRC : public Array<Type> {
public:
 ArrayRC(int sz = ArraySize) : Array<Type>( sz ) {};
 Type& operator[](int index) {
  assert( index >= 0 && index < size );
  return ia[ index ];
 }
};

#endif
-------------------------------------------------------------------------------------------------------
The sample program includes a template function,
void swap(Array<Type> &array, int I, int j), which should be accessable by objects of both Array and Array RC. Here it is:
-------------------------------------------------------------------------------------------------------
#include <iostream.h>

#include "array.h"
#include "arrayrc.h"

template <class Type>
void swap( Array<Type> &array, int i, int j )
{
 Type tmp = array[ i ];
 array [ i ] = array [ j ];
 array [ j ] = tmp;
}

main() {
 Array<int> ia1;
 ArrayRC<int> ia2;

 cout << "swap() with Array<int> ia1\n";
 int size = ia1.getSize();
 swap( ia1, 1, size );

 cout << "swap() with ArrayRC<int> ia2\n";
 size = ia2.getSize();
 swap( ia2, 1, size );

 return 0;
}
-------------------------------------------------------------------------------------------------------

Unfortunately, this won't compile (using Borland C++ 3.1). I get this
error message: Could not find a match for
   'swap(ArrayRC<int>,int,int)'

But the whole point of the example was to demonstrate that it WOULD
match!  Now, I'm only too aware that I've possibly goofed somewhere
in typing this in, but I really don't think so.  I also can't believe
I'm the only one who has come across this.  Any help will be greatly
appreciated - thanks :-).

Alex              *+* e-mail: <alex.ewing@almac.co.uk> *+*
---
 * 1st 2.00n #5138 * C program run.  C program crash.  C programmer quit