在C++中使用指向内部类对象的指针作为外部类构造函数参数



正如我在标题中所说,我不能将内部类对象作为外部类构造函数的参数传递。下面是类标题;

class Corner {
public:
Corner(cv::Mat imageMat, int flag, feat::Corner::cornerDetectorHarris* cdh = nullptr);
...
class cornerDetectorHarris {...};
...
};  

Visual Studio社区对上面的代码没有问题。问题是当我试图在.cpp文件中定义函数时;

feat::Corner::Corner(cv::Mat imageMat, int flag, feat::Corner::cornerDetectorHarris* cdh) {}

VSC在第二Corner"no instance of overloaded function "feat::Corner::Corner" matches the specified type"下声明E0493错误。这是错误代码;

Severity    Code    Description Project File    Line    Suppression State
Error (active)  E0493   no instance of overloaded function "feat::Corner::Corner" matches the specified type    bpv2    C:UsersASUSsourcereposbpv2bpv2feat.cpp   533 

如果我删除cornerDetectorHarris指针,错误就会消失,所以我很确定这是问题所在。我尝试删除参数的默认值,但它没有改变任何内容。

只需在任何依赖于内部类的方法之前声明内部类。当然,如果您愿意,您甚至可以在头中定义方法。

但不太清楚你为什么会这样做;为什么不简单地在Corner之前声明cornerDetectorHarris呢?

这里有一些关于这个主题的有用讨论:为什么要在C++中使用嵌套类?

class Corner {
public:
class cornerDetectorHarris {...};
Corner(cv::Mat imageMat, int flag, feat::Corner::cornerDetectorHarris* cdh = nullptr)
{
//do stuff
}
// etc
};  

最新更新