C++ 压倒一切<&书信电报;模板化结构的运算符
C++ 压倒一切<&书信电报;模板化结构的运算符,c++,C++,numcp.h文件 #include "iostream" namespace numcpp{ template<typename T> struct Vector { std::vector<T> v; }; template<typename T> struct Matrix { std::vector<T
numcp.h
文件
#include "iostream"
namespace numcpp{
template<typename T>
struct Vector
{
std::vector<T> v;
};
template<typename T>
struct Matrix
{
std::vector<T> m;
//template<typename T>
friend std::ostream& operator << (std::ostream& out, const mf& mat);
};
typedef Vector<float> vf;
typedef Matrix<vf> mf;
}
#include "numcpp.h"
namespace numcpp{
std::ostream& operator << (std::ostream& out, const mf& mat)
{
//overloaded out here
return out;
}
}
main.cpp
#include "iostream"
#include "numcpp.h"
int main()
{
numcpp::mf inputs;
// inputs is filled with random numbers here
std::cout << inputs;
}
#包括“iostream”
#包括“numcpp.h”
int main()
{
numcpp::mf输入;
//输入在这里用随机数填充
std::cout尝试将numcp.h更改为
名称空间numcpp{
模板
结构向量
{
std::向量v;
};
typedef向量vf;
模板
结构矩阵
{
std::向量m;
//模板
内联friend std::ostream&operator尝试将numcp.h更改为
名称空间numcpp{
模板
结构向量
{
std::向量v;
};
typedef向量vf;
模板
结构矩阵
{
std::向量m;
//模板
inline friend std::ostream&operator类型定义在类的定义中不可用Matrix
。您可以在重载运算符的类定义(@Alloces'answer)之前声明类型定义:
#include <vector>
#include <sstream>
#include <iostream>
namespace numcpp {
template<typename T>
struct Vector
{
std::vector<T> v;
};
template<typename T>
struct Matrix
{
std::vector<T> m;
template<typename K> // template parameter must not be named T
inline friend std::ostream& operator << (std::ostream& out, const Matrix<K>& mat);
};
template<typename K>
inline std::ostream& operator << (std::ostream& out, const Matrix<K>& mat)
{
return out << "Just a test";
}
typedef Vector<float> vf;
typedef Matrix<vf> mf;
}
int main() {
numcpp::mf inputs;
std::cout << inputs << "\n"; // Just a test
}
#包括
#包括
#包括
名称空间numcpp{
模板
结构向量
{
std::向量v;
};
模板
结构矩阵
{
std::向量m;
模板//模板参数不能命名为T
inline friend std::ostream&operator类型定义在类的定义中不可用Matrix
。您可以在重载运算符的类定义(@Alloces'answer)之前声明类型定义:
#include <vector>
#include <sstream>
#include <iostream>
namespace numcpp {
template<typename T>
struct Vector
{
std::vector<T> v;
};
template<typename T>
struct Matrix
{
std::vector<T> m;
template<typename K> // template parameter must not be named T
inline friend std::ostream& operator << (std::ostream& out, const Matrix<K>& mat);
};
template<typename K>
inline std::ostream& operator << (std::ostream& out, const Matrix<K>& mat)
{
return out << "Just a test";
}
typedef Vector<float> vf;
typedef Matrix<vf> mf;
}
int main() {
numcpp::mf inputs;
std::cout << inputs << "\n"; // Just a test
}
#包括
#包括
#包括
名称空间numcpp{
模板
结构向量
{
std::向量v;
};
模板
结构矩阵
{
std::向量m;
模板//模板参数不能命名为T
inline friend std::ostream&operator您通常需要在头文件中定义模板。该定义需要在调用站点上提供,以允许编译器安装模板
如果可能,最简单的方法是将定义放入类定义中
template<typename T>
struct Matrix
{
std::vector<T> m;
// using Matrix here is allowed and refers to the current instatiation, equivalent to writing Matrix<T>
friend std::ostream& operator << (std::ostream& out, const Matrix& mat) {
...
return out;
}
};
模板
结构矩阵
{
std::向量m;
//这里使用矩阵是允许的,指的是当前的状态,相当于写入矩阵
friend std::ostream&operator您通常需要在头文件中定义模板。该定义需要在调用站点上提供,以允许编译器安装模板
如果可能,最简单的方法是将定义放入类定义中
template<typename T>
struct Matrix
{
std::vector<T> m;
// using Matrix here is allowed and refers to the current instatiation, equivalent to writing Matrix<T>
friend std::ostream& operator << (std::ostream& out, const Matrix& mat) {
...
return out;
}
};
模板
结构矩阵
{
std::向量m;
//这里使用矩阵是允许的,指的是当前的状态,相当于写入矩阵
friend std::ostream&operator您的numcpp.cpp不包括iostream或numcpp.h,并且您的numcpp.h不包括vector。(或者它包括,在这种情况下,您应该将您的问题包括一个。)@Snetel是的,我只是编辑了它。我只是想让问题简短一些。在numcp.cpp
中的名称空间中缺少名称是否是一种拼写错误?您可以在朋友声明中用Matrix
替换mf
。在定义中使用它而不使用模板参数总是指当前安装。您可以但是,不要将模板的定义放在.cpp
文件中(您可以,但需要显式地为每种应可使用的类型恢复该模板)。您的numcpp.cpp不包括iostream或numcpp.h,并且您的numcpp.h不包括vector。(或者确实如此,在这种情况下,您应该将您的问题包括在内。)@Snetel是的,我只是编辑了它。我只是想让问题简短一些。在numcp.cpp
中的名称空间中缺少名称是否是一种拼写错误?您可以在朋友声明中用Matrix
替换mf
。在定义中使用它而不使用模板参数总是指当前安装。您可以但是,不要将模板的定义放在.cpp
文件中(您可以,但是需要为每种应该可用的类型显式地恢复该模板).@我刚刚注意到即使删除了内联,它也能工作。有理由在这里内联吗?没有。inline
将允许存在多个定义,因此如果您想将函数的定义放在头文件中并包含在多个位置,它是有用的,但当定义在类中时,它就不需要了。@我只是注意到了d即使删除了内联,它也可以工作。有理由在这里内联吗?没有。inline
将允许存在多个定义,因此如果您希望将函数的定义放在头文件中并将其包含在多个位置,它是有用的,但当定义在类中时,它就不需要了。