【问题标题】:out-of-line definition of 'function' does not match any declaration in 'Class'“函数”的离线定义与“类”中的任何声明都不匹配
【发布时间】:2016-05-12 21:48:50
【问题描述】:

所以我正在使用 OpenCV 开发一个 iOS 项目,目前正在尝试将现有 c++ 项目的一部分导入 iOS 应用程序,最近出现了这个错误。我对 C++ 和目标 C 仍然很陌生,所以也许我错过了一些非常明显的东西。

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

函数 writeToFile(string fname) 定义在头文件中,如下所示,但在实现文件中,错误抱怨“'writeToFile' 的外线定义与'中的任何声明不匹配”轮廓'":

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;
}

任何帮助将不胜感激。

【问题讨论】:

  • 错字?? Stringstring.

标签: c++ objective-c opencv objective-c++


【解决方案1】:

你的声明

void writeToFile(String fname);

与实现不匹配

void Contour::writeToFile(string fname)

声明使用大写-S“字符串”,而实现使用小写-s“字符串”。匹配这些应该可以解决它。

【讨论】:

  • 做到了。谢谢,我什至没有想到会是这么简单的事情。
猜你喜欢
  • 2014-04-30
  • 1970-01-01
  • 2011-04-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多