c++ - 聚合、模板和动态 vector

标签 c++ templates vector aggregate-functions

我不断收到 3 个错误。它们都与我假设的聚合模板的方式有关,但我找不到任何可以帮助我解决这个问题的方法。我的老师并不清楚我们应该如何获得他希望我们获得的输出。

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

这是我写的

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;
}

最佳答案

"RowAray"是一个模板,有一个模板参数:

template <class T>
class RowAray

看到了吗?它是一个模板,一个模板参数。

现在,在这里:

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

看到了吗?在此处引用 RowAray 模板时没有模板参数。使用模板时,还必须指定其参数(除非它们有默认值,此处无关紧要)。

事实上,在这里,您正在使用一个模板参数定义一个新模板 Table - 这是无关紧要的。

你可能打算使用

RowAray<T> **records;

在这里;但这只是基于对这堆代码的粗略浏览,所以不要自动相信我的话。您需要弄清楚您打算在这里做什么,并指定正确的模板参数。

这不是所示代码中唯一的无参数引用。您需要找到并修复所有这些问题。

此外,你还甩了:

using namespace std;

在继续#include一堆头文件之前,包括标准库头文件。 This is a bad programming practice ,并且经常会产生微妙的、难以找出的编译错误,即使不是完全错误的代码。您还必须摆脱 using namespace std;,尤其是当涉及到一堆 #include 时。

关于c++ - 聚合、模板和动态 vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42730870/

相关文章:

C++ 强制在子类中实现方法但具有不同的签名

c++ - 在C++中获取调用者的地址

c++ - 未找到架构 x86_64 的符号隐含类型转换不起作用

带有引用参数的 C++ 模板隐式实例化

c++ - 在常数时间内提取子 vector

c++ - 如何用 ffi 包装自定义结构的 std::vector?

c++ - Vector - 按引用调用 c++

c++ - Visual Studio 201 0's strange "警告 LNK4042"

c++ - Boost 1.55 不适用于 Visual Studio 2013

templates - 使用 std::make_shared 拥有自己的 std::shared_ptr