'function'的外联定义与'Class'中的任何声明都不匹配

out-of-line definition of 'function' does not match any declaration in 'Class'

本文关键字:任何 不匹配 声明 定义 function Class      更新时间:2023-10-16

所以我正在使用OpenCV处理一个iOS项目,目前正试图将现有c++项目的一部分导入到iOS应用程序中,最近出现了这个错误。我对C++和目标C都还很陌生,所以也许我错过了一些显而易见的东西。

我注意到,试图在Contour命名空间中定义和实现任何新函数都会导致同样的错误,添加虚拟说明符似乎不会改变这一点。draw函数没有遇到问题。我也尝试过按照类似问题中的建议退出并重新启动xcode,但问题仍然存在。

函数writeToFile(字符串fname)在头文件中定义,如下所示,但在实现文件中,错误报告"‘writeToFile’的越界定义与‘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"字符串",而实现使用了小写-S"字符串"。匹配这些应该可以修复它。