为什么程序不起作用
Why programm does not work?
为数组创建了一个模板类。然后我创建了构造函数和析构函数,重载了输入和输出运算符。
using std::istream;
using std::ostream;
template <typename T> class myArray;
template< typename T> ostream& operator <<(ostream &, const myArray <T> &);
template< typename T> istream& operator >> (istream &, const myArray<T> &);
template <typename T> class myArray
{
private:
T**mas;
int line,
column;
friend istream& operator >> <T>(istream &, const myArray &);
friend ostream& operator << <T>(ostream &, const myArray &);
public:
myArray() : mas (0), line ( 0) column (0) {}
myArray(int n, int m);
myArray(const myArray &masToCopy);
~myArray()
{
for (int i = 0; i<line; i++)
{
delete[](mas[i]);
}
delete[](mas);
}
};
template <typename T> myArray <T>::myArray(int n ,int m )
{
mas = new T*[n];
for (int i = 0; i < n; i++)
{
mas[i] = new T[m];
}
}
template <typename T> myArray <T>:: myArray(const myArray &masToCopy)
{
line = masToCopy.line;
column = masToCopy.column;
mas = new T*[line];
for (int i = 0; i <line; i++)
mas[i] = new T[column];
for (int i = 0; i<line; i++)
for (int j = 0; j < column; j++)
mas[i][j] = masToCopy.mas[i][j];
}
template< typename T> istream& operator >> (istream &in, const myArray<T> &el)
{
for (int i = 0; i < el.line; i++)
{
for (int j = 0; j < el.column; j++)
{
in >> el.mas[i][j];
}
}
return in;
}
template< typename T> ostream& operator << (ostream &out, const myArray<T> &el)
{
for (int i = 0; i < el.line; i++)
{
cout << "n";
for (int j = 0; j < el.column; j++)
{
out << el.mas[i][j];
cout << " ";
}
}
return out;
}
当我尝试使用我的类时,程序不允许输入数组,然后不显示它。相反,它立即写着"要继续,请按任意键">
using std:: cin;
using std::cout;
int main()
{
myArray<int> intArray1(2,2);
cin >> intArray1;
cout << intArray1;
return 0;
}
我该如何解决这个问题?
好吧,这是你的问题:
template< typename T> ostream& operator << (istream &in, const myArray<T> &el)
应该是
template< typename T> ostream& operator << (ostream &out, const myArray<T> &el)
复制粘贴错误...编辑:所以,我在写这个答案时看到Some programmer dude
评论中说了这句话。对不起。
编辑2:删除它,因为它不是必需
的template <typename T> class myArray;
template< typename T> ostream& operator <<(ostream &, const myArray <T> &);
template< typename T> istream& operator >> (istream &, const myArray<T> &);
然后是更多错误:
friend istream& operator >> <T>(istream &, const myArray &);
应该是:
friend istream& operator >> (istream &, const myArray &)
{
[inlined code]
}
接下来:
mas = new int*[line];
for (int i = 0; i <line; i++)
mas[i] = new int[column];
应该是
mas = new T*[line];
for (int i = 0; i <line; i++)
mas[i] = new T[column];
然后结束
myArray<T>::myArray(int n, int m)
{
应该是:
myArray<T>::myArray(int n, int m)
: line(n)
, column(m)
{
等等等等,代码中有很多错误。
相关文章:
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- sigwait() 在多线程程序中不起作用
- 为什么使用数组元素查找最大数字的程序不起作用?
- C++实用程序::转换在静态链接库中不起作用
- Cuda 基本程序 (将值写入矩阵和 std:cout 不起作用) ;主功能不启动
- 视窗 10 :笔(手写笔)在 MFC 应用程序上不起作用
- win32 滚动条在 C/C++ 程序中不起作用
- 用于拆分空格字符串的程序不起作用
- 反转一个数字程序不起作用,为什么?
- C++ 程序在代码块 IDE 中不起作用
- 该程序将.csv文件中的一系列单词放入数组中,然后随机生成句子.但它不起作用
- 程序在CLion IDE中工作,但exe不起作用
- Opencv在2013年第5季度不起作用.应用程序总是在窗口中崩溃
- C++从句子中删除给定字符的程序不起作用
- C ++ GCC 内联程序集似乎不起作用
- 为什么当我选择>250000个采样点时,程序不起作用?
- 任务计划程序库的预处理不起作用 - 多定义错误
- C 为什么我的程序循环和"if-statements"不起作用?
- try() 和 catch() 不起作用;程序崩溃,从不执行 catch() 块
- Cin 执行不起作用 (程序.exe <文件与数据.txt)