0

I keep getting 3 errors. They're all related to the way I'm aggregating templates I'm assuming but i can't find anything to help me figure this out. My teacher wasn't super clear on how we're supposed to get the output he wants us to get.

In file included from main.cpp:10:
./Table.h:15:9: error: use of class template 'RowAray' requires template arguments

Here is what i wrote

RowAray.cpp

#ifndef ROWARAY_H // if constant ROWARAY_H not defined do not execute
#define ROWARAY_H // defines constant ROWARAY_H
#include <iostream>
#include <new>       // Needed for bad_alloc exception
#include <cstdlib>   // Needed for the exit function

template <class T>
class RowAray{
    private:
        int size;
        T *rowData;
        void memError();  // Handles memory allocation errors
        void subError();  // Handles subscripts out of range
    public:
        RowAray(T); //used to construct row Array object
        ~RowAray(){delete [] rowData;} //used to deallocate dynamically allocated memory from Row array
        int getSize(){return size;} //inline accessor member function used to return length of Row array
        T getData(int i){return (( i >=0&& i < size)?rowData[i]:0);} //
        T &operator[](const int &);
};
template <class T>
RowAray<T>::RowAray(T colSize){
     size =colSize>1?colSize:1; 
   // Allocate memory for the array.
   try
   {
      rowData = new T [size];
   }
   catch (bad_alloc)
   {
      memError();
   }

   // Initialize the array.
   for (int count = 0; count < size; count++)
      *(rowData + count) = rand()%90+10; 
}

template <class T>
void RowAray<T>::memError()
{
   cout << "ERROR:Cannot allocate memory.\n";
   exit(EXIT_FAILURE);
}

template <class T>
void RowAray<T>::subError()
{
   cout << "ERROR: Subscript out of range.\n";
   exit(EXIT_FAILURE);
}

template <class T>
T &RowAray<T>::operator[](const int &sub)
{
   if (sub < 0 || sub >= size)
      subError();
   else
       return rowData[sub];
}
#endif  /* ROWARAY_H */

Table.cpp

#ifndef TABLE_H
#define TABLE_H

#include "RowAray.h"

template <class T>
class Table{
    private:
        int szRow;
        RowAray **records;

    public:
        Table(int,int); //used to construct Table object
        ~Table(); //used to deallocate dynamically allocated memory from Table object
        int getSzRow(){return szRow;} //used to return row size
        int getSize(int row){return records[row>=0?row:0]->getSize();} //used to return column size
        T getRec(int, int); //used to return inserted random numbers of 2d arrays
};

template <class T>
Table<T>::Table(int r, int c ){
   //Set the row size
    this->szRow = r;
    //Declare the record array
    records = new RowAray*[this->szRow];
    //Size each row
    int allCol = c;
    //Create the record arrays
    for(int i=0;i<this->szRow;i++){
        records[i]=new RowAray(allCol);
    }
}

template <class T>
T Table<T>::getRec(int row, int col){
     //if else statement used to return randomly generated numbers of array
   if(row >= 0 && row < this->szRow && col >= 0 && col < records[row]->getSize()){
        return records[row]->getData(col);
    }else{
        return 0;
    }
}

template <class T>
Table<T>::~Table(){
    //Delete each record
    for(int i=0;i<this->szRow;i++){
        delete records[i];
    }
    delete []records;
}

#endif  /* TABLE_H */

main.cpp

#include <cstdlib>
#include <ctime>
#include <iostream>
#include <iomanip>
using namespace std;

//User Libraries
#include "RowAray.h"
#include "Table.h"

//Global Constants

//Function Prototype
template<class T>
void prntRow(T *,int);
template<class T>
void prntTab(const Table<T> &);

//Execution Begins Here!
int main(int argc, char** argv) {
   //Initialize the random seed
   srand(static_cast<unsigned int>(time(0)));

   //Declare Variables
   int rows=3,cols=4;

   //Test out the Row with integers and floats
   RowAray<int> a(3);
   RowAray<float> b(4);
   cout<<"Test the Integer Row "<<endl;
   prntRow(&a,3);
   cout<<"Test the Float Row "<<endl;
   prntRow(&b,4);

   //Test out the Table with a float
   Table<float> tab1(rows,cols);
   Table<float> tab2(tab1);
   //Table<float> tab3=tab1+tab2;

   cout<<"Float Table 3 size is [row,col] = Table 1 + Table 2 ["
           <<rows<<","<<cols<<"]";
   //prntTab(tab3);

   //Exit Stage Right
   return 0;
}

template<class T>
void prntRow(T *a,int perLine){
    cout<<fixed<<setprecision(1)<<showpoint<<endl;
    for(int i=0;i<a->getSize();i++){
        cout<<a->getData(i)<<" ";
        if(i%perLine==(perLine-1))cout<<endl;
    }
    cout<<endl;
}

template<class T>
void prntTab(const Table<T> &a){
    cout<<fixed<<setprecision(1)<<showpoint<<endl;
    for(int row=0;row<a.getSzRow();row++){
        for(int col=0;col<a.getSize();col++){
            cout<<setw(8)<<a.getRec(row,col);
        }
        cout<<endl;
    }
    cout<<endl;
}

1 Answers1

1

"RowAray" is a template, with one template parameter:

template <class T>
class RowAray

See there? It's a template, one template parameter.

Now, over here:

template <class T>
class Table{
    private:
        int szRow;
        RowAray **records;

See there? No template parameter when referring to the RowAray template, here. When using a template, its parameters must also be specified (unless they have a default, which is irrelevant here).

The fact that, here, you are defining a new template, Table, with one template parameter -- that's irrelevant.

You probably intended to use

RowAray<T> **records;

here; but that's just based on a cursory look at this pile of code so don't automatically take my word for it. You need to figure out what you intended to do here, and specify the correct template parameter.

This is not the only parameter-less reference in the shown code. You need to find, and fix all of them.

Furthermore, you also dumped:

using namespace std;

before proceeding and #includeing a bunch of header files, including standard library header files. This is a bad programming practice, and often creates subtle, and difficult to figure out compilation errors, if not outright wrong code. You must get rid of using namespace std; as well, especially when a bunch of #includes are involved.

Community
  • 1
  • 1
Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148
  • Thank you, this assignment is for a class which is out of my coding league honestly. It's supposed to be a review assignment but the prerequisite class I took didn't expose me to object oriented programming at all, much less templates – EggplantMachina Mar 11 '17 at 03:49