c++ - 错误 LNK2019 : when trying to return a vector

标签 c++ opencv lnk2019

我在一个类中创建了一个方法,该方法返回一个 opencv 项目中的一个 vector 。类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 文件中的主函数中,我包含“Detection.h”,创建一个检测对象和一个 Rect vector ,当我尝试分配它们时出现错误

 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"));

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

最佳答案

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

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

代替自由函数

vector<Rect> detection(string fileName)

?

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

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

关于c++ - 错误 LNK2019 : when trying to return a vector,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20491797/

相关文章:

c++ - boost 多阵列段错误

c++ - 尝试递归地重新绘制所有图像像素

opencv - Facebook 个人资料图片中有多少人?

c++ - 未解析的外部符号(构造函数)

c++ - LNK2001 和 LNK2019 错误 - DirectX 未解析的外部符号

c++ - 本地化测试,用 XXXXX 格式化所有字符串

c++ - 检查元组是否包含某种类型的元素

python - 使用Canny功能时,可以先敷面膜吗?

android - JNI,NDK和OpenCV

c++ - LNK2019 Unresolved ext 符号错误(包括项目文件)