B
Bert Aerts (rm X)
With following files, the print statement
cout << amt.getElem(2) << endl;
can not be compiled.
Though getElem returns Type, in this case class MyType, which has print
functions defined.
It does work for int and long double types...
What am I missing? Thanks for any help!
/*
* chapter11.h
*
* Created on: Sep 13, 2009
* Author: bert
*/
#ifndef CHAPTER11_H_
#define CHAPTER11_H_
// template defining an array
template <class Type>
class Array
{
public:
Array();
~Array();
void setSize(size_t value);
size_t getSize();
void setElem(size_t index, Type value);
Type getElem(size_t index);
private:
Type *elems;
size_t numElems;
};
// a user-defined type
class MyType
{
public:
MyType();
// a member function that modifies instances of MyType
void sanitize();
void print(ostream *os);
private:
int value;
};
#endif /* CHAPTER11_H_ */
/*
* chapter11.cpp
*
* Created on: Sep 13, 2009
* Author: bert
*/
#include <iostream>
using namespace std;
#include <stdlib.h> // to get exit()
#include "chapter11.h"
#define SIZE 10
template<class Type>
Array<Type>::Array()
{
elems = 0;
numElems = 0;
}
template<class Type>
Array<Type>::~Array()
{
if( elems != 0 )
{
cout << "delete[] " << (void *) elems << endl;
delete[] elems;
}
}
template<class Type>
void Array<Type>::setSize(size_t value)
{
if( elems != 0 ) delete[] elems;
numElems = value;
elems = new Type[value];
if( elems != 0 )
{
cout << "elems pointer is " << (void *)elems << endl;
}
else
{
cout << "new[] failed" << endl;
exit(1);
}
}
template<class Type>
size_t Array<Type>::getSize()
{
return numElems;
}
template<class Type>
void Array<Type>::setElem(size_t index, Type value)
{
if( index < numElems )
{
elems[index] = value;
}
else
{
cout << "setElem bad index " << index << " >= " << numElems << endl;
exit(1);
}
}
template<class Type>
Type Array<Type>::getElem(size_t index)
{
if( index >= numElems )
{
cout << "getElem bad index " << index << " >= " << numElems << endl;
exit(1);
}
else
{
return elems[index];
}
}
void MyType:rint(ostream *os)
{
*os << "MyType{" << value << '}';
}
MyType::MyType()
{
value = 0;
}
void MyType::sanitize()
{
value = 22;
}
ostream &operator<<(ostream &os, MyType &theMyType)
{
theMyType.print(&os);
return os;
}
int main()
{
Array<int> ai;
Array<long double> ald;
Array<MyType> amt;
MyType refmt;
ai.setSize(SIZE);
ai.setElem(2,5);
cout << "ai[2] = " << ai.getElem(2) << endl;
ald.setSize(SIZE);
ald.setElem(2,3.1415);
cout << "ald[2] = " << ald.getElem(2) << endl;
amt.setSize(SIZE);
cout << "refmt is " << refmt << endl;
amt.setElem(2,refmt);
cout << amt.getElem(2) << endl; // DOES NOT COMPILE
refmt.sanitize();
cout << "refmt.sanitize gives " << refmt << endl;
}
cout << amt.getElem(2) << endl;
can not be compiled.
Though getElem returns Type, in this case class MyType, which has print
functions defined.
It does work for int and long double types...
What am I missing? Thanks for any help!
/*
* chapter11.h
*
* Created on: Sep 13, 2009
* Author: bert
*/
#ifndef CHAPTER11_H_
#define CHAPTER11_H_
// template defining an array
template <class Type>
class Array
{
public:
Array();
~Array();
void setSize(size_t value);
size_t getSize();
void setElem(size_t index, Type value);
Type getElem(size_t index);
private:
Type *elems;
size_t numElems;
};
// a user-defined type
class MyType
{
public:
MyType();
// a member function that modifies instances of MyType
void sanitize();
void print(ostream *os);
private:
int value;
};
#endif /* CHAPTER11_H_ */
/*
* chapter11.cpp
*
* Created on: Sep 13, 2009
* Author: bert
*/
#include <iostream>
using namespace std;
#include <stdlib.h> // to get exit()
#include "chapter11.h"
#define SIZE 10
template<class Type>
Array<Type>::Array()
{
elems = 0;
numElems = 0;
}
template<class Type>
Array<Type>::~Array()
{
if( elems != 0 )
{
cout << "delete[] " << (void *) elems << endl;
delete[] elems;
}
}
template<class Type>
void Array<Type>::setSize(size_t value)
{
if( elems != 0 ) delete[] elems;
numElems = value;
elems = new Type[value];
if( elems != 0 )
{
cout << "elems pointer is " << (void *)elems << endl;
}
else
{
cout << "new[] failed" << endl;
exit(1);
}
}
template<class Type>
size_t Array<Type>::getSize()
{
return numElems;
}
template<class Type>
void Array<Type>::setElem(size_t index, Type value)
{
if( index < numElems )
{
elems[index] = value;
}
else
{
cout << "setElem bad index " << index << " >= " << numElems << endl;
exit(1);
}
}
template<class Type>
Type Array<Type>::getElem(size_t index)
{
if( index >= numElems )
{
cout << "getElem bad index " << index << " >= " << numElems << endl;
exit(1);
}
else
{
return elems[index];
}
}
void MyType:rint(ostream *os)
{
*os << "MyType{" << value << '}';
}
MyType::MyType()
{
value = 0;
}
void MyType::sanitize()
{
value = 22;
}
ostream &operator<<(ostream &os, MyType &theMyType)
{
theMyType.print(&os);
return os;
}
int main()
{
Array<int> ai;
Array<long double> ald;
Array<MyType> amt;
MyType refmt;
ai.setSize(SIZE);
ai.setElem(2,5);
cout << "ai[2] = " << ai.getElem(2) << endl;
ald.setSize(SIZE);
ald.setElem(2,3.1415);
cout << "ald[2] = " << ald.getElem(2) << endl;
amt.setSize(SIZE);
cout << "refmt is " << refmt << endl;
amt.setElem(2,refmt);
cout << amt.getElem(2) << endl; // DOES NOT COMPILE
refmt.sanitize();
cout << "refmt.sanitize gives " << refmt << endl;
}