[問題] 重載運算子與cout<<
開發平台(Platform): (Ex: VC++, GCC, Linux, ...)
DEV
問題(Question):
matrix<int> A, B;
重載^之後, cout<<(A^B)<<endl; 不行
預期的正確結果(Expected Output):
錯誤結果(Wrong Output):
no match for 'operator<<' in 'std::cout <<
A.matrix<T>::operator^<int>((*(const matrix<int>*)(& B)))'
程式碼(Code):(請善用置底文網頁, 記得排版)
重載^的部分
matrix operator^(const matrix<T> &m1)
{
matrix<T> m(m1.len);
for(int i = 0;i < m1.len;i++)
for(int j = 0;j < m1.len;j++)
{
m[i][j] = 0;
for(int k = 0;k < m1.len;k++)
m[i][j] = m[i][j] + m1[i][k]*this->mat[k][i];
}
return m;
}
重載<<的部分
friend ostream &operator<<(ostream &out,matrix<T> &mm)
{
for(int i = 0;i < mm.len;i++)
{
for(int j = 0;j < mm.len;j++)
out << mm[i][j] << " ";
out << endl;
}
return out;
}
int main()
{
matrix<int> A, B;
....
cout <<(A^B)<< endl;
}
補充說明(Supplement):
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 223.139.169.158
※ 編輯: heyguys123 來自: 223.139.169.158 (04/06 11:06)
→
04/06 12:26, , 1F
04/06 12:26, 1F
推
04/06 12:36, , 2F
04/06 12:36, 2F
→
04/06 12:37, , 3F
04/06 12:37, 3F
→
04/06 13:44, , 4F
04/06 13:44, 4F
→
04/06 14:25, , 5F
04/06 14:25, 5F
→
04/06 15:21, , 6F
04/06 15:21, 6F
推
04/06 16:02, , 7F
04/06 16:02, 7F
推
04/06 16:38, , 8F
04/06 16:38, 8F
推
04/06 19:37, , 9F
04/06 19:37, 9F
→
04/06 23:24, , 10F
04/06 23:24, 10F
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章