template <class elementType>
class Table {
public:
Table( int = 10, int = 10 ); // default constructor
Table( const Table< elementType > & ); // copy constructor
~Table();
void print() const;
void inputValues();
const Table< elementType > &operator=(
const Table< elementType > & );
booloperator==( const Table< elementType > & ) const;
// Determine if two arrays are not equal and
// return true, otherwise return false (uses operator==).
booloperator!=( const Table< elementType > &right ) const
{ return !( *this == right ); }
elementType &operator()( int, int );
const elementType &operator()( int, int ) const;
private:
int rows; // number of rows in array
int columns; // number of columns in array
/* write declaration for private data member ptr, a pointer
that contains the dynamically allocated array */
elementType *ptr;
};
Assuming that I have correctly done this:
1 2 3 4 5
/* write declaration for private data member ptr, a pointer
that contains the dynamically allocated array */
elementType *ptr;
How can I:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
template < class elementType >
Table< elementType >::Table( int r, int c )
{
int rows = ( r > 0 ? r : 10 );
int columns = ( c > 0 ? c : 10 );
/* write code to allocate dynamically an array with rows *
columns elements of type elementType */
int size = rows*columns;
assert( ptr != 0 );
for ( int i = 0; i < rows * columns; i++ )
ptr[ i ] = 0; // initialize array
}
I tried
ptr (new elementType[size]);
Which doesn't seem to work because Table expects multiple parameters.
Can someone point me in the right direction? What am I missing here..
destructor declared. When I compile the code I get linker error 2019...
1 2
Table.obj : error LNK2019: unresolved external symbol "public: char & __thiscall Table<char>::operator()(int,int)" (??R?$Table@D@@QAEAADHH@Z) referenced in function _main
Table.obj : error LNK2019: unresolved external symbol "public: int & __thiscall Table<int>::operator()(int,int)" (??R?$Table@H@@QAEAAHHH@Z) referenced in function _main
I finished declaring my functions however - get some bizarre output. Actually the debugger stops it and I get a screen full of 0's and other strange floating point numbers.
I would guess that I did something wrong in the implementation of my overloads here
Lines 48 and 49 shadow the member variables of the same names.
asserting the return value of new against non-NULL is pointless since the throwing
version of new never returns NULL (it throws std::bad_alloc instead, which you don't
catch).
operator== is totally wrong; it is comparing the address of the righthand side against the
address of the lefthand side, not the contents of each.
We won't even go into exception safety, as I'm sure you aren't worried about it.
Lines 48 and 49 are declaring local variables named row and col and assigning values to them, but I'm
sure the intention is to assign the class members row and col those values instead.
The operator==() implementation needs a second look, as well. It is mistakenly using the assignment operator (=) in the conditions on line 8.
Also, counting how many are equivalent and comparing that count to the total number of items compared is kind of silly. All you have to do is loop through them and compare until either 1) an element is not equivalent or 2) all of them were equivalent.
EDIT: My mistake, it's not even comparing the data it is only comparing the size--which does not require a loop at all.