'function' 的外联定义与 'Class' 中的任何声明都不匹配
out-of-line definition of 'function' does not match any declaration in 'Class'
所以我正在使用 OpenCV 开发一个 iOS 项目,目前正在尝试将现有 c++ 项目的一部分导入 iOS 应用程序,最近出现了这个错误。我对 C++ 和 objective C 仍然很陌生,所以也许我遗漏了一些非常明显的东西。
我注意到尝试在 Contour 命名空间中定义和实现任何新函数都会导致相同的错误,添加虚拟说明符似乎并没有改变这一点。绘图功能虽然没有遇到问题。我也尝试按照类似问题中的建议退出并重新启动 xcode,但问题仍然存在。
函数 writeToFile(string fname) 在头文件中定义,如下所示,但在实现文件中,错误提示 "out-of-line definition of 'writeToFile' does not match any declaration in 'Contour'":
2DContour.h:
#ifndef TWODCONTOUR_H
#define TWODCONTOUR_H
#include <vector>
using std::vector;
#include <opencv2/core.hpp>
using namespace cv;
class Contour
{
protected:
vector<Vec2f> points;
virtual void process(){} // virtual function interface for after-creation/edit processing (eg. refinement/validation)
public:
inline Vec2f at(int index){return points[index];}
inline void clear(){points.clear();}
inline void addPoint(Vec2f p){points.push_back(p);}
inline void finish(){process();}
inline void randomize(int num)
{
num--;
points.clear();
int cycles=6;//rand()%6+1;
float offset=(float)rand()/(float)RAND_MAX*2.0f*3.141592654f;
float noisemag=(float)rand()/(float)RAND_MAX;
for(int i=0;i<num;i++)
{
float a=(float)i/(float)num;
addPoint(
Vec2f(sin(a*2.0f*3.141592654f),cos(a*2.0f*3.141592654f))+
noisemag*Vec2f(sin(cycles*a*2.0f*3.141592654f+offset),cos(cycles*a*2.0f*3.141592654f+offset)));
}
addPoint(points.front());
process();
}
void writeToFile(String fname);
virtual Mat draw(Mat canvas, bool center=false, Scalar colour=Scalar(255,255,255), int thickness=1);
inline int numPoints(){return points.size();}
inline Vec2f getPoint(int i){return points[i];}
};
#endif
2DContour.cpp:
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <iostream>
#include <fstream>
#include "2DContour.h"
using namespace std;
using namespace cv;
//error occurs here
void Contour::writeToFile(string fname)
{
ofstream out;
out.open(fname.c_str());
for(unsigned int i=0;i<points.size();i++)
out << points[i][0]<<" "<<points[i][1]<<endl;
out.close();
std::cout<<"Wrote: "<<fname<<std::endl;
}
//draw() function does not experience the same error however
Mat Contour::draw(Mat canvas, bool center, Scalar colour, int thickness)
{
Mat r=canvas.clone();
cv::Point c(center?r.cols/2:0,center?r.rows/2:0);
for( unsigned int j = 0; j < points.size(); j++ )
{
line(r,c+ cv::Point(points[j]*50),c+ cv::Point(points[(j+1)%points.size()]*50),colour,thickness, 8 );
}
return r;
}
如有任何帮助,我们将不胜感激。
您的声明
void writeToFile(String fname);
与实现不匹配
void Contour::writeToFile(string fname)
声明使用了大写字母-S "String",而实现使用了小写字母-s "string."匹配它们应该可以修复它。
所以我正在使用 OpenCV 开发一个 iOS 项目,目前正在尝试将现有 c++ 项目的一部分导入 iOS 应用程序,最近出现了这个错误。我对 C++ 和 objective C 仍然很陌生,所以也许我遗漏了一些非常明显的东西。
我注意到尝试在 Contour 命名空间中定义和实现任何新函数都会导致相同的错误,添加虚拟说明符似乎并没有改变这一点。绘图功能虽然没有遇到问题。我也尝试按照类似问题中的建议退出并重新启动 xcode,但问题仍然存在。
函数 writeToFile(string fname) 在头文件中定义,如下所示,但在实现文件中,错误提示 "out-of-line definition of 'writeToFile' does not match any declaration in 'Contour'":
2DContour.h:
#ifndef TWODCONTOUR_H
#define TWODCONTOUR_H
#include <vector>
using std::vector;
#include <opencv2/core.hpp>
using namespace cv;
class Contour
{
protected:
vector<Vec2f> points;
virtual void process(){} // virtual function interface for after-creation/edit processing (eg. refinement/validation)
public:
inline Vec2f at(int index){return points[index];}
inline void clear(){points.clear();}
inline void addPoint(Vec2f p){points.push_back(p);}
inline void finish(){process();}
inline void randomize(int num)
{
num--;
points.clear();
int cycles=6;//rand()%6+1;
float offset=(float)rand()/(float)RAND_MAX*2.0f*3.141592654f;
float noisemag=(float)rand()/(float)RAND_MAX;
for(int i=0;i<num;i++)
{
float a=(float)i/(float)num;
addPoint(
Vec2f(sin(a*2.0f*3.141592654f),cos(a*2.0f*3.141592654f))+
noisemag*Vec2f(sin(cycles*a*2.0f*3.141592654f+offset),cos(cycles*a*2.0f*3.141592654f+offset)));
}
addPoint(points.front());
process();
}
void writeToFile(String fname);
virtual Mat draw(Mat canvas, bool center=false, Scalar colour=Scalar(255,255,255), int thickness=1);
inline int numPoints(){return points.size();}
inline Vec2f getPoint(int i){return points[i];}
};
#endif
2DContour.cpp:
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <iostream>
#include <fstream>
#include "2DContour.h"
using namespace std;
using namespace cv;
//error occurs here
void Contour::writeToFile(string fname)
{
ofstream out;
out.open(fname.c_str());
for(unsigned int i=0;i<points.size();i++)
out << points[i][0]<<" "<<points[i][1]<<endl;
out.close();
std::cout<<"Wrote: "<<fname<<std::endl;
}
//draw() function does not experience the same error however
Mat Contour::draw(Mat canvas, bool center, Scalar colour, int thickness)
{
Mat r=canvas.clone();
cv::Point c(center?r.cols/2:0,center?r.rows/2:0);
for( unsigned int j = 0; j < points.size(); j++ )
{
line(r,c+ cv::Point(points[j]*50),c+ cv::Point(points[(j+1)%points.size()]*50),colour,thickness, 8 );
}
return r;
}
如有任何帮助,我们将不胜感激。
您的声明
void writeToFile(String fname);
与实现不匹配
void Contour::writeToFile(string fname)
声明使用了大写字母-S "String",而实现使用了小写字母-s "string."匹配它们应该可以修复它。