C++ 从C+;中的友元函数访问私有成员时出错+;
我在重载“时遇到了一个问题。对于任何想知道的人,我发现了这个问题C++ 从C+;中的友元函数访问私有成员时出错+;,c++,C++,我在重载“时遇到了一个问题。对于任何想知道的人,我发现了这个问题 在操作符类表中没有操作符[]。你是说t.table[0][0]?哇。我真傻。非常感谢。关于第一部分你知道吗?不,问题出在你没有展示的代码部分。你能发布一个最小的,完整的,可验证的例子吗?在这个例子中,你使用这个操作符。的返回值是什么 #ifndef INT_TABLE_H #define INT_TABLE_H #include<cstdlib> #include<iostream> class Tab
在操作符
类表中
没有操作符[]
。你是说t.table[0][0]
?哇。我真傻。非常感谢。关于第一部分你知道吗?不,问题出在你没有展示的代码部分。你能发布一个最小的,完整的,可验证的例子吗?在这个例子中,你使用这个操作符。的返回值是什么
#ifndef INT_TABLE_H
#define INT_TABLE_H
#include<cstdlib>
#include<iostream>
class Table {
public:
Table();
Table(int n);
Table(const Table& orig);
virtual ~Table();
friend std::ostream& operator<<(std::ostream& out, const Table& t);
private:
int** table;
int rows;
int cols;
};
#endif /* INT_TABLE_H */
std::ostream& operator<<(std::ostream& out, const Table& t)
{
for(int i=0;i<t.rows;i++)
{
for(int j=0; j<t.cols; j++)
{
std::cout<<"["<<"."<<"]";
}
std::cout<<std::endl;
}
}
1 [main] algproject1 6360 cygwin_exception::open_stackdumpfile: Dumping stack trace to algproject1.exe.stackdump
this is a table
[.][.][.][.][.]
[.][.][.][.][.]
[.][.][.][.][.]
[.][.][.][.][.]
[.][.][.][.][.]
RUN FAILED (exit value 35,584, total time: 1s)
int_table.cpp: In function 'std::ostream& operator<<(std::ostream&, const Table&)':
int_table.cpp:50:34: error: no match for 'operator[]' (operand types are 'const Table' and 'int')
std::cout<<"["<<t[0][0]<<"]";
^