在mian()函数调用静态函数static void saveXYZ(const char* filename, const Mat& mat);会报出一个“error c2129:静态函数已声明但未定义”的错误。这是因为静态函数只能在声明它的文件中可见,不能够其他文件所调用,也就是不能被其他的.cpp文件所调用,只能被自己的.cpp所调用。
为了解决这个问题,需要再对静态函数用普通函数进行一层封装,调用的时候,调用这个普通函数就可以。
错误的例子:
1、在saveXYZ.h中
#pragma once #include <opencv2/opencv.hpp> #include <iostream> #include "saveXYZ.h" using namespace std; using namespace cv; static void saveXYZ(const char* filename, const Mat& mat);
2、在saveXYZ.cpp中
#include <opencv2/opencv.hpp> #include <iostream> using namespace std; using namespace cv; static void saveXYZ(const char* filename, const Mat& mat) { const double max_z = 16.0e4; FILE* fp = fopen(filename, "wt"); printf("%d %d \n", mat.rows, mat.cols); for (int y = 0; y < mat.rows; y++) { for (int x = 0; x < mat.cols; x++) { Vec3f point = mat.at<Vec3f>(y, x); if (fabs(point[2] - max_z) < FLT_EPSILON || fabs(point[2]) > max_z) continue; fprintf(fp, "%f %f %f\n", point[0], point[1], point[2]); } } fclose(fp); }
3、main.cpp中
直接调用 saveXYZ("xyz.xls", xyz);会报错
修改:
1、在saveXYZ.h中
#pragma once #include <opencv2/opencv.hpp> #include <iostream> #include "saveXYZ.h" using namespace std; using namespace cv; static void saveXYZ(const char* filename, const Mat& mat); void savestaticXYZ(const char* filename, const Mat& mat);
2、在saveXYZ.cpp中
#include <opencv2/opencv.hpp> #include <iostream> using namespace std; using namespace cv; static void saveXYZ(const char* filename, const Mat& mat) { const double max_z = 16.0e4; FILE* fp = fopen(filename, "wt"); printf("%d %d \n", mat.rows, mat.cols); for (int y = 0; y < mat.rows; y++) { for (int x = 0; x < mat.cols; x++) { Vec3f point = mat.at<Vec3f>(y, x); if (fabs(point[2] - max_z) < FLT_EPSILON || fabs(point[2]) > max_z) continue; fprintf(fp, "%f %f %f\n", point[0], point[1], point[2]); } } fclose(fp); } void savestaticXYZ(const char* filename, const Mat& mat) { saveXYZ(filename, mat); }3、main.cpp中
变成调用普通函数savestaticXYZ("xyz.xls", xyz);