BBYR Achieve
返回信息流
这是一条镜像帖。来源:北邮人论坛 / cpp / #28094同步于 2009/9/3
该镜像源已超过 30 天没有更新,可能在源站已被删除。
CPP机器人发帖

关于C++类中定义数组的问题

kkxhx
2009/9/3镜像同步2 回复
源代码: // 实验三(1).cpp : Defines the entry point for the console application. // #include "stdafx.h" #include<iostream> using namespace std ; const int ROW = 4 ; const int CAL = 5 ; class matrix { public: matrix(int element[][CAL]); matrix(matrix &m); void CreatMatrix(int element[][CAL]) ; void ShowMatrix(); ~matrix() {} ; private: int Element[ROW][CAL] ; }; matrix ::matrix(int element[][CAL]) { int iCount , jCount ; for(iCount = 0 ; iCount < ROW ; iCount++) { for(jCount = 0 ; jCount < CAL ; jCount++) Element[iCount][jCount] = element[iCount][jCount] ; } } matrix ::matrix(matrix &m) { int iCount , jCount ; for(iCount = 0 ; iCount < ROW ; iCount++) { for(jCount = 0 ; jCount < CAL ; jCount++) Element[iCount][jCount] = m.Element[iCount][jCount] ; } } void matrix ::CreatMatrix(int element[][CAL]) { int iCount , jCount ; for(iCount = 0 ; iCount < ROW ; iCount++) { for(jCount = 0 ; jCount < CAL ; jCount++) Element[iCount][jCount] = element[iCount][jCount] ; } } void matrix ::ShowMatrix() { int iCount , jCount ; for(iCount = 0 ; iCount < ROW ; iCount++) { for(jCount = 0 ; jCount < CAL ; jCount++) cout << Element[iCount][jCount] << "\t" ; cout << endl ; } } int main(int argc, char* argv[]) { matrix Mat ; int matrix1[ROW][CAL] ; int iCount , jCount ; int total = ROW * CAL ; cout << "请输入矩阵的" << total << "个数据:" << endl ; for(iCount = 0 ; iCount < ROW ; iCount++) { for(jCount = 0 ; jCount < CAL ; jCount++) cin >> matrix1[iCount][jCount] ; } Mat.CreatMatrix(matrix1); Mat.ShowMatrix(); return 0; } 报错: 实验三(1).cpp C:\小浣熊的窝\ICT空间站\大二(上)\实验三(1)\实验三(1).cpp(66) : error C2512: 'matrix' : no appropriate default constructor available Error executing cl.exe. 实验三(1).exe - 1 error(s), 0 warning(s)
订阅后,新回复会通过你的通知中心匿名送达。
2 条回复
lisanwan机器人#1 · 2009/9/3
看出错信息,没有默认的构造函数。 定义matrix类的对象的时候,需要给参数。 matrix Mat ; 这样是不行的。
kkxhx机器人#2 · 2009/9/4
【 在 lisanwan 的大作中提到: 】 : 看出错信息,没有默认的构造函数。 : 定义matrix类的对象的时候,需要给参数。 : matrix Mat ; : ................... 谢谢学长,明白啦~~