🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

1 total messages Started by David Jones Tue, 09 Jan 1996 00:00
Bug in gcc-2.7.2 g++ templates
#6197
Author: David Jones
Date: Tue, 09 Jan 1996 00:00
109 lines
3671 bytes
Hello,

I am new to C++ and am trying to compile a sample program from Lippmans
book "C++ Primer", 2 ed. on page 63. I get the following compiler error:

p63.C: In function `int main()':
p63.C:27: type unification failed for function template `template <class Type>
void swap(Array<...> &, int, int)'	

This code compiles find on the Borland C++ compiler and the AIX xlC C++ 
compiler.

The problem seems to be with invoking a template function with a derived class.

I compiled like : g++ -o p63 pg3.C

The code (from Lippmans book) :

-------------Array.h-----------------------------
/*#################################################################
# Copyright (c) 1991 AT&T Bell Laboratories, All Rights Reserved   
# Published in ``A C++ Primer''  by Stanley Lippman, Addison-Wesley
#################################################################*/

#ifndef ARRAY_H
#define ARRAY_H

const int ArraySize = 12; 

template <class Type>
class Array {
public: 
    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
------------end Array.h-------------------

-------------Arr_RC.h---------------------------
/*#################################################################
# Copyright (c) 1991 AT&T Bell Laboratories, All Rights Reserved   
# Published in ``A C++ Primer''  by Stanley Lippman, Addison-Wesley
#################################################################*/

#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
------------end Arr_RC.h--------------------------

------------p63.C---------------------------
/*#################################################################
# Copyright (c) 1991 AT&T Bell Laboratories, All Rights Reserved   
# Published in ``A C++ Primer''  by Stanley Lippman, Addison-Wesley
#################################################################*/

#include <iostream.h>
#include "Array.h"
#include "Arr_RC.h"

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

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

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

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

    return 0;
}
-----------end P63.C------------------------------
-----------------------------------------------------------------------
David Jones                  Tel: (607)751-3638
Loral Federal Systems        Fax: (607)751-3621
Route 17C  MD 0534           Email: dgj@lfs.loral.com
Owego, NY 13827
-----------------------------------------------------------------------
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads