Article View: gnu.gcc.bug
Article #6197Bug in gcc-2.7.2 g++ templates
From: David Jones
Date: Tue, 09 Jan 1996 00:00
Date: Tue, 09 Jan 1996 00:00
109 lines
3671 bytes
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
-----------------------------------------------------------------------
Technical Details
Message-ID:
<9601091937.AA21599@bock.endicott.ibm.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 113d02,5a5894838baa590,start X-Google-Attributes: gid113d02,public From: dgj@lfs.loral.com (David Jones) Subject: Bug in gcc-2.7.2 g++ templates Date: 1996/01/09 Message-ID: <9601091937.AA21599@bock.endicott.ibm.com> X-Deja-AN: 134573129 distribution: gnu sender: daemon@cis.ohio-state.edu organization: GNUs Not Usenet newsgroups: gnu.gcc.bug