Putting class declaration in .cpp file

前端 未结 3 2032
不思量自难忘°
不思量自难忘° 2020-12-09 09:13

Is it possible to have class declaration and implementation in same .cpp file?

I want to do some unit-testing with help of mock object. Here is some example of my te

相关标签:
3条回答
  • 2020-12-09 10:04

    Yes, it's entirely legitimate and allowable to define a class and its member functions in a single file. In fact, from the viewpoint of the compiler that's essentially always the case -- you have the class definition in a header, and include that header in the source file where you implement its member functions.

    The errors you've encountered look like linker errors, not compiler errors. Exactly what's missing isn't entirely clear from what you've posted. One possibility is that your base class has some pure virtuals that you've failed to implement in the derived class, but I'm not at all sure that's correct.

    0 讨论(0)
  • 2020-12-09 10:07

    The Q_OBJECT macro declares a set of meta-object member functions. The MOC build tool is responsible for parsing .h files and defining these function declarations. Note that it does not parse .cpp files. In your case, the vtable could not be found because the MOC tool did not parse your .cpp file. The solution is to move your class definition inside a header file and add the header to your .pro file. A second solution - a bit "hacky" - is to do the following:

    #include <QObject>
    #include <QtDebug>
    
    class Counter : public QObject
    {
      Q_OBJECT
    
    public:
      Counter() { value = 0; }
      int getValue() const { qDebug() << "getValue()"; return value; }
    
    public slots:
      void setValue(int value);
    
    signals:
      void valueChanged(int newValue);
    
    private:
      int value;
    };
    
    #include "main.moc"
    
    void Counter::setValue(int value)
    {
      qDebug() << "setValue()";
      if (this->value != value) {
        this->value = value;
        emit valueChanged(value);
      }
    }
    
    int main()
    {
      Counter a, b;
    
      QObject::connect(
        &a, &Counter::valueChanged,
        &b, &Counter::setValue);
    
      a.setValue(12);
      b.setValue(48);
    
      return 0;
    }
    

    Notice the `#include "myfile.moc" under the class definition.

    This works because qmake will invoke the MOC tool on any files with a #include directive. Thus, MOC will parse the .cpp file and generate the meta-object function definitions, resolving your linker error.

    0 讨论(0)
  • 2020-12-09 10:14

    When Base class has any virtual function which is not pure, it's definition needs to be included while compiling the final binary, otherwise it gives linker error for vtable or typeinfo. Look at below example:

    // Base.h
    struct Base {
      virtual void fun() = 0;
      virtual ~Base();
    };
    
    // Base.cpp
    #include"Base.h"
    Base::~Base () {}
    
    // Derived.cpp
    #include"Base.h"
    struct Derived : Base {
      void fun () {}
    };
    
    int main () {
      Derived d;
    }
    

    Now compile-link for Derived.cpp and Base.cpp will work fine. Both .cpp files also can be compiled separately for creating object files and then linked together.

    From your question, what I feel is that, you are not somehow attaching the .cpp/object file of class AbstractConnection, which still contains one non pure virtual function -- its destructor. If you compile that definition also along with your ConnectionMockup then the linker error should not appear. Either you can compile the file including destructor body or define destructor body in the class definition itself.

    0 讨论(0)
提交回复
热议问题