Cpp文件包括,同时也可以在其中调试



在线程池实现中:

有一个threadpool.h,它有一个虚拟的类threadpool。

class ThreadPool {
     funcX (){} = 0
     ...
}
// ThreadPoolImpl not defined or declared in this file.

在相应的threadpool.cpp中,有继承自threadpool的实现threadpoolImpl。

//include threadpool.h header.
class ThreadPoolImpl : public ThreadPool {
      funcX() {....};  // The function I want to debug inside.
}
我问题:

在我的main函数中:如果我只包含threadpool.h,那么由于缺少threadpoolImpl定义,我得到

incomplete type not allowed error.

如果我包含threadpool.cpp而不是threadpool.h,我得到链接错误,因为threadpool.cpp被编译两次。为了解决这个问题,我从我的项目中删除了threadpool.cpp。但是,这样我就不能用断点调试threadpool.cpp了。

请有人告诉我是否可以使用threadpoolImpl,同时也能够在其中调试(与VS IDE工具),或者我必须重写它?

您应该创建一个ThreadPoolImpl .h并将类ThreadPoolImpl的定义添加到其中。在main.cpp中,你只需要包含threadpoolImpl.h。

threadpoolImpl.h:

#include <threadpool.h>
class ThreadPoolImpl : public ThreadPool {
      funcX();  // The function I want to debug inside.
}

threadpoolImpl.cpp:

void ThreadPoolImpl::funcX()
{
  //debug here
}

看起来ThreadPool在你的设计中是一个接口。有一些更好的工具,如设计模式,可以改进您的实现。

你让它听起来像你是#include的threadpool.cpp。不要这样做,只要确保它实际上是解决方案/项目的一部分,它显示在解决方案资源管理器中,并且正在编译。

相关内容

最新更新