你好,
我来自C#背景,没有很多C++经验.为了生成干净的代码,我尝试将实现和接口分开并尽可能使用继承.当我尝试将典型的C#概念应用于C++时,我遇到了一个迄今为止我无法解决的问题.我认为对于经验丰富的C++程序员来说这可能是微不足道的,但它已经让我疯狂了很长一段时间.
首先我声明一个基类(它目前不包含任何逻辑但将来会包含它)
class PropertyBase : public IProperty
{
};
Run Code Online (Sandbox Code Playgroud)
然后我为Properties定义了一个接口
class IProperty
{
public:
virtual ~IProperty() {};
virtual PropertyBase correct(const ICorrector &corrector) = 0;
virtual PropertyBase joinWith(const PropertyBase &partner, const IRecombinator &recombinator) = 0;
};
Run Code Online (Sandbox Code Playgroud)
这就是问题所在:编译器返回两个虚函数的错误,说不允许声明一个返回抽象类的函数.当然我不想返回该类型的对象PropertyBase
.我想声明从其继承的其他类PropertyBase
返回自己的实例.
现在我已经读过一个可能的方法是修改IProperty
这样来返回指针:
class IProperty
{
public:
virtual ~IProperty() {};
virtual PropertyBase* correct(const ICorrector &corrector) = 0;
virtual PropertyBase* joinWith(const PropertyBase &partner, const IRecombinator &recombinator) = 0;
};
Run Code Online (Sandbox Code Playgroud)
但是,如果可能的话,我想避免这种情况,以防止内存泄漏.如果有人能够更好地处理这个问题,那就太棒了.
非常感谢你
每当您将静态库链接到 Visual C++ 项目时,您必须确保项目运行时库的值与用于编译库的值相匹配。例如,如果库是使用静态运行时选项编译的,而您尝试使用动态运行时编译项目,则链接器将抛出错误。
显然,链接器有一种方法可以确定库是使用静态运行时还是动态运行时编译的。我想知道是否有一个命令行工具可以使用该库并可以直接告诉我在创建过程中使用了哪些运行时?
我再次遇到内存分配问题,我无法弄清楚原因.
当我在调试模式下运行程序时,我收到以下错误消息(我试图尽可能准确地翻译它):
Windows已在LogoColorDetector.exe中触发了断点.这可能是由于堆损坏引起的,这表示LogoColorDetector.exe或其中一个加载的DLL [...]存在问题
当我调试程序时,我发现问题似乎发生在以下行:
std::string tmp = imgTrain2[j]->getFilepath();
Run Code Online (Sandbox Code Playgroud)
getFilepath() - Function实现如下:
const std::string& support::Image::getFilepath() const
{
return this->_filePath;
}
Run Code Online (Sandbox Code Playgroud)
我已经检查过imgTrain [j]中的Image对象是否有正确的_filePath字符串.所以我认为问题出在其他地方.有趣的是,包含有问题的行的功能似乎有效.这是我第二次调用它失败的函数,这表明问题不在函数本身.我没有分配任何内存,也没有删除函数中的任何内容,除非可以通过std :: string间接完成
如果它是任何人的帮助,这里的堆栈跟踪:
msvcr100d.dll!_heap_alloc_base(unsigned int size) Zeile 55 C
msvcr100d.dll!_heap_alloc_dbg_impl(unsigned int nSize, int nBlockUse, const char * szFileName, int nLine, int * errno_tmp) Zeile 431 + 0x9 Bytes C++
msvcr100d.dll!_nh_malloc_dbg_impl(unsigned int nSize, int nhFlag, int nBlockUse, const char * szFileName, int nLine, int * errno_tmp) Zeile 239 + 0x19 Bytes C++
msvcr100d.dll!_nh_malloc_dbg(unsigned int nSize, int nhFlag, int nBlockUse, …
Run Code Online (Sandbox Code Playgroud)