聚集,模板和动态向量
Aggregates, templating, and dynamic vectors
我不断遇到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
之前,将一堆标题文件(包括标准库标头文件)。这是一种糟糕的编程实践,通常会产生微妙的,并且很难找出编译错误,即使不是彻底的错误代码。您也必须摆脱using namespace std;
,尤其是涉及一堆#include
S时。
相关文章:
- std::向量与传递值的动态数组
- C++ 将抽象类型的动态分配对象传递给函数并存储在向量中
- C++字符串数组的动态向量
- 是否可以使用宏来访问动态数组或向量中的元素或为其赋值
- 动态分配特征向量的模因
- 在存储库中,筛选器函数返回动态向量或其他存储库
- 超载添加操作员和动态向量的分割故障
- 聚集,模板和动态向量
- C++中的特征库有一个动态向量或矩阵吗
- 使用特征库填充动态向量
- C++对的动态向量
- 一维数组是否比特征动态向量快
- 如何在C++中将文本文件读入两个动态向量
- 了解我有多少内存可用于一个动态向量c++
- 指向对象的指针的向量的动态向量,C++
- 动态向量的期望内容
- 初始化动态向量列表
- 真正的动态向量类型
- 在遍历动态向量时使用auto的异常行为
- 动态向量的特征库内存使用情况