错误LNK2019:尝试返回向量时

时间:2013-12-10 10:21:50

标签: c++ opencv lnk2019

我在类中创建了一个方法,该方法在opencv项目中返回一个向量。类cpp和头代码:

Detection::Detection(){}

vector<Rect> detection(string fileName)
{
    Mat image, gray_image;
    string path = "C:\\"+ fileName;
    image = imread( fileName, 1 );

    //create a vector array to store the face found
    vector<Rect> faces;

    while(true)
    {
    ...
    }
    return faces;
}

标题文件:

class Detection
{
public:
    Detection();
    vector<Rect> detection(string fileName);
};

在另一个cpp文件中的main函数中我包含“Detection.h”,创建一个检测对象和一个Rect向量,当我尝试分配它们时我得到了错误

 error LNK2019: unresolved external symbol "public: class std::vector<class cv::Rect_<int>,class std::allocator<class cv::Rect_<int> > > __thiscall Detection::detection(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?detection@Detection@@QAE?AV?$vector@V?$Rect_@H@cv@@V?$allocator@V?$Rect_@H@cv@@@std@@@std@@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@3@@Z) referenced in function _main

主要功能代码:

vector<Rect> detections;
Detection object;
detections = object.detection("C:\\opencvAssets/BioID_0102.pgm");
// ALTERNATIVES 
//object.detection("C:\\opencvAssets/BioID_0102.pgm").swap(detections);
//detections(object.detection("C:\\opencvAssets/BioID_0102.pgm"));

我的代码中缺少什么???

1 个答案:

答案 0 :(得分:3)

您的意思是实现成员方法:

vector<Rect> Detection::detection(string fileName)
{
    //...
}

而不是自由功能

vector<Rect> detection(string fileName)

请注意,如果您在没有任何编译器错误的情况下到达链接阶段,则意味着detection可能被标记为static或甚至可能是自由函数,因为它似乎不是直接的与单个Detection对象绑定。

另外,请考虑通过fileName引用传递const