使用正确的语法在命名空间中实现非成员、重载运算符 免责声明:我是C++编程新手,我读过几十个论坛,找不到我具体问题的答案。
我在下面介绍了Point类的头文件和定义文件,以及一个调用重载ostream打印点的主函数。我找不到非成员重载运算符定义的正确语法您需要将使用正确的语法在命名空间中实现非成员、重载运算符 免责声明:我是C++编程新手,我读过几十个论坛,找不到我具体问题的答案。,c++,namespaces,overloading,operator-keyword,friend,C++,Namespaces,Overloading,Operator Keyword,Friend,我在下面介绍了Point类的头文件和定义文件,以及一个调用重载ostream打印点的主函数。我找不到非成员重载运算符定义的正确语法您需要将std::ostream&operatorm_值[I]={0}这是一个无效指针。 #ifndef CLUSTERING_POINT_H #define CLUSTERING_POINT_H #include <iostream> namespace Clustering { class Point { int m_di
std::ostream&operatorm_值[I]={0}代码>这是一个无效指针。
#ifndef CLUSTERING_POINT_H
#define CLUSTERING_POINT_H
#include <iostream>
namespace Clustering {
class Point {
int m_dim; // number of dimensions of the point
double *m_values; // values of the point's dimensions
public:
Point(int);
friend std::ostream &operator<<(std::ostream &, const Point &);
};
}
#endif //CLUSTERING_POINT_H
#include "Point.h"
//Constructor
Clustering::Point::Point(int i)
{
m_dim = i;
m_values[i] = {0};
}
std::ostream &operator<<(std::ostream &os, const Clustering::Point &point) {
os << "Test print";
return os;
}
#include <iostream>
#include "Point.h"
using namespace std;
using namespace Clustering;`
int main() {
Point p1(5);
cout << p1;
return 0;
}
namespace Clustering {
std::ostream &operator<<(std::ostream &os, const Clustering::Point &point)
{
// ...
}
}