将流运算符与模板一起使用 Class
Using stream operator with Template Class
我正在创建一个矩阵 class 并有以下声明。目的是构建一个可扩展的矩阵 class,该矩阵具有灵活的算法并且可以 运行 在各种平台上 -
template<typename T> class xss_matrix{
public:
xss_matrix(int i=0, int j=0):
max_row(i), max_col(j)
{
/*create space for the (0,0) entry*/
matrix[0]= (T*)(malloc(sizeof(T)));
};
~xss_matrix()
{
};
void add_entry(int row, int col, T val);
T get_entry(int row, int col);
friend ostream& operator<<(ostream &out, const xss_matrix<T> &m_xss_matrix);
private:
/*Internal variables*/
int max_row, max_col;
/*Internal data structures*/
T* matrix[];
/*Internal methods*/
void add_columns(int row, int col);
void add_rows(int row, int col);
};
#endif
然后我重载流运算符 -
/*Overloaded stream operators*/
template<typename T> std::ostream& operator<<(ostream &out, const xss_matrix<T> &m_xss_matrix)
{
for(int ii = 0; ii < m_xss_matrix.max_row+1; ii+=1){
for(int jj = 0; jj < m_xss_matrix.max_col+1; jj+=1){
std::cout <m_xss_matrix.matrix[ii][jj] << " ";
}
std::cout << std::endl;
}
}
但是当我 运行 这个 -
#include "xss_matrix.hpp"
int main(int argc, char** argv)
{
xss_matrix<double>* foo = new xss_matrix<double>;
xss_matrix<double> bar;
foo->add_entry(0,0,2.35);
foo->add_entry(0,1,1.75);
foo->add_entry(1,0,1.50);
foo->add_entry(1,1,2.00);
std::cout << *foo;
}
我收到链接器错误 -
[mitra@vlch-mitra xss_src]$ make
g++ -c -o main.o main.cpp -g -I. -fpermissive
In file included from xss_matrix.hpp:1,
from main.cpp:1:
xss_matrix.h:36: warning: friend declaration `std::ostream& operator<<(std::ostream&, const xss_matrix<T>&)' declares a non-template function
xss_matrix.h:36: note: (if this is not what you intended, make sure the function template has already been declared and add <> after the function name here)
g++ -o main main.o -g -I. -fpermissive
main.o: In function `main':
/home/mitra/dv/libparser/xss_src/main.cpp:15: undefined reference to `operator<<(std::basic_ostream<char, std::char_traits<char> >&, xss_matrix<double> const&)'
collect2: ld returned 1 exit status
make: *** [main] Error 1
[mitra@vlch-mitra xss_src]$
我不明白我确信会导致链接器失败的编译器警告。有人可以帮忙吗? gcc的版本是4.4.7-4
谢谢,
拉吉
friend
函数模板的正确声明是:
template<class U>
friend ostream& operator<<(ostream &out, const xss_matrix<U> &m_xss_matrix);
T* matrix[];
的内存分配不正确。将 T* matrix[];
替换为 std::vector<T> matrix;
。这也会自动修复所有编译器生成的 copy/move constructors/assignments 和析构函数(你可以删除你的)。按行和列索引是 matrix[row * max_col + col]
.
友元声明声明了一个名为operator<<
的非模板函数; xss_matrix
的每个实例化都会产生一个新的声明。 None 这些函数实际上已定义。
然后是函数模板的单一定义,重载了所有这些声明。未声明为好友。
但是,在重载解析期间,非模板重载获胜,其他条件相同。所以编译器选择其中之一。但它从未被定义,所以链接器抱怨。
如果你真的想与模板成为朋友,必须这样做:
template<typename T> class xss_matrix;
template<typename T>
std::ostream& operator<<(ostream &out, const xss_matrix<T>& m_xss_matrix);
template<typename T> class xss_matrix {
// Implementation here
template <typename U>
friend std::ostream& operator<<(ostream &out, const xss_matrix<U>& m_xss_matrix);
};
template<typename T>
std::ostream& operator<<(ostream &out, const xss_matrix<T> &m_xss_matrix) {
// Implementation here
}
但是,将非友元函数模板委托给 public 成员函数通常更容易:
template<typename T> class xss_matrix {
public:
void print(ostream& out) {
// Write data to out
}
};
template<typename T>
std::ostream& operator<<(ostream &out, const xss_matrix<T> &m_xss_matrix) {
m_xss_matrix.print(out);
return out;
}
我正在创建一个矩阵 class 并有以下声明。目的是构建一个可扩展的矩阵 class,该矩阵具有灵活的算法并且可以 运行 在各种平台上 -
template<typename T> class xss_matrix{
public:
xss_matrix(int i=0, int j=0):
max_row(i), max_col(j)
{
/*create space for the (0,0) entry*/
matrix[0]= (T*)(malloc(sizeof(T)));
};
~xss_matrix()
{
};
void add_entry(int row, int col, T val);
T get_entry(int row, int col);
friend ostream& operator<<(ostream &out, const xss_matrix<T> &m_xss_matrix);
private:
/*Internal variables*/
int max_row, max_col;
/*Internal data structures*/
T* matrix[];
/*Internal methods*/
void add_columns(int row, int col);
void add_rows(int row, int col);
};
#endif
然后我重载流运算符 -
/*Overloaded stream operators*/
template<typename T> std::ostream& operator<<(ostream &out, const xss_matrix<T> &m_xss_matrix)
{
for(int ii = 0; ii < m_xss_matrix.max_row+1; ii+=1){
for(int jj = 0; jj < m_xss_matrix.max_col+1; jj+=1){
std::cout <m_xss_matrix.matrix[ii][jj] << " ";
}
std::cout << std::endl;
}
}
但是当我 运行 这个 -
#include "xss_matrix.hpp"
int main(int argc, char** argv)
{
xss_matrix<double>* foo = new xss_matrix<double>;
xss_matrix<double> bar;
foo->add_entry(0,0,2.35);
foo->add_entry(0,1,1.75);
foo->add_entry(1,0,1.50);
foo->add_entry(1,1,2.00);
std::cout << *foo;
}
我收到链接器错误 -
[mitra@vlch-mitra xss_src]$ make
g++ -c -o main.o main.cpp -g -I. -fpermissive
In file included from xss_matrix.hpp:1,
from main.cpp:1:
xss_matrix.h:36: warning: friend declaration `std::ostream& operator<<(std::ostream&, const xss_matrix<T>&)' declares a non-template function
xss_matrix.h:36: note: (if this is not what you intended, make sure the function template has already been declared and add <> after the function name here)
g++ -o main main.o -g -I. -fpermissive
main.o: In function `main':
/home/mitra/dv/libparser/xss_src/main.cpp:15: undefined reference to `operator<<(std::basic_ostream<char, std::char_traits<char> >&, xss_matrix<double> const&)'
collect2: ld returned 1 exit status
make: *** [main] Error 1
[mitra@vlch-mitra xss_src]$
我不明白我确信会导致链接器失败的编译器警告。有人可以帮忙吗? gcc的版本是4.4.7-4 谢谢, 拉吉
friend
函数模板的正确声明是:
template<class U>
friend ostream& operator<<(ostream &out, const xss_matrix<U> &m_xss_matrix);
T* matrix[];
的内存分配不正确。将 T* matrix[];
替换为 std::vector<T> matrix;
。这也会自动修复所有编译器生成的 copy/move constructors/assignments 和析构函数(你可以删除你的)。按行和列索引是 matrix[row * max_col + col]
.
友元声明声明了一个名为operator<<
的非模板函数; xss_matrix
的每个实例化都会产生一个新的声明。 None 这些函数实际上已定义。
然后是函数模板的单一定义,重载了所有这些声明。未声明为好友。
但是,在重载解析期间,非模板重载获胜,其他条件相同。所以编译器选择其中之一。但它从未被定义,所以链接器抱怨。
如果你真的想与模板成为朋友,必须这样做:
template<typename T> class xss_matrix;
template<typename T>
std::ostream& operator<<(ostream &out, const xss_matrix<T>& m_xss_matrix);
template<typename T> class xss_matrix {
// Implementation here
template <typename U>
friend std::ostream& operator<<(ostream &out, const xss_matrix<U>& m_xss_matrix);
};
template<typename T>
std::ostream& operator<<(ostream &out, const xss_matrix<T> &m_xss_matrix) {
// Implementation here
}
但是,将非友元函数模板委托给 public 成员函数通常更容易:
template<typename T> class xss_matrix {
public:
void print(ostream& out) {
// Write data to out
}
};
template<typename T>
std::ostream& operator<<(ostream &out, const xss_matrix<T> &m_xss_matrix) {
m_xss_matrix.print(out);
return out;
}