将类声明放在.cpp文件中 [英] Putting class declaration in .cpp file

查看:166
本文介绍了将类声明放在.cpp文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有可能在同一个.cpp文件中有类声明和实现吗?



我想在mock对象的帮助下做一些单元测试。这里是我的测试的一些例子:

  //一些包括删除

#includeabstractconnection。 h

class ConnectionMockup:public AbstractConnection
{
Q_OBJECT
public:
explicit ConnectionMockup(QObject * parent = 0);

bool isReady()const;
void sendMessage(const QString& message);

void test_send_message(const QString& message);

bool ready;
QStringList messages;
};

ConnectionMockup :: ConnectionMockup(QObject * parent)
:AbstractConnection(parent)
{
ready = true;
}

bool ConnectionMockup :: isReady()const
{
return ready;
}

void ConnectionMockup :: sendMessage(const QString& message)
{
messages.append(message);
}

void ConnectionMockup :: test_send_message(const QString& message)
{
emit messageRecieved(message);
}

TestEmcProgram :: TestEmcProgram(QObject * parent):
QObject(parent)
{
}

TestEmcProgram :: open()
{
ConnectionMockup mockup;
EmcProgram程序(& mockup);
QCOMPARE(...
...
...

如你所见,ConnectionMockup类只用于TestConnection类,我不需要它在任何地方。所以,当我尝试编译这个程序,我得到以下错误:

 > testemcprogram.o:在函数
>`ConnectionMockup':
> / home / sasa / Desktop / QtPro / FocoKernel -build-desktop /../ FocoKernel / testemcprogram.cpp:29:
>未定义引用`vtable for
> ConnectionMockup'
> / home / sasa / Desktop / QtPro /FocoKernel-build-desktop/../FocoKernel/testemcprogram.cpp:29:
>未定义引用`vtable for
> ConnectionMockup'testemcprogram.o:In
> function `〜ConnectionMockup':
> /home/sasa/Desktop/QtPro/FocoKernel-build-desktop/../FocoKernel/testemcprogram.cpp:14:
>未定义引用`vtable for
> ConnectionMockup'

是否可以在这里留下声明,

编辑:由于Jerry Coffin先生(感谢Coffin先生)建议我可能没有一些虚函数实现,我将在这里声明AbstractConnection,以便我们可以审查这种可能性:

  #include< QObject> 

class AbstractConnection:public QObject
{
Q_OBJECT
public:
explicit AbstractConnection(QObject * parent = 0);
virtual〜AbstractConnection();

virtual bool isReady()const = 0;

信号:
void messageRecieved(const QString& message);

public slots:
virtual void sendMessage(const QString& message)= 0;

};解决方案:感谢@JCooper,@iammilind和@Jerry Coffin,我们将为您解决问题,我们将竭诚为您服务。 有解决方案。从AbstractConnection中删除析构函数(因为它实际上什么都不做)和从ConnectionMockup中删除Q_OBJECT它工作。

解决方案

并允许在单个文件中定义类及其成员函数。事实上,从编译器的角度来看,实际上总是这种情况 - 你在头文件中有类定义,并在实现其成员函数的源文件中包含该头文件。



您遇到的错误看起来像链接器错误,而不是编译器错误。正确的是,你所发布的内容并不完全清楚。一种可能性是,你的基类有一些纯虚拟,你无法在派生类中实现,但我根本不是确定是正确的。


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 test:

// Some includes removed

#include "abstractconnection.h"

class ConnectionMockup : public AbstractConnection
{
    Q_OBJECT
public:
    explicit ConnectionMockup(QObject *parent = 0);

    bool isReady() const;
    void sendMessage(const QString &message);

    void test_send_message(const QString &message);

    bool ready;
    QStringList messages;
};

ConnectionMockup::ConnectionMockup(QObject *parent)
    : AbstractConnection(parent)
{
    ready = true;
}

bool ConnectionMockup::isReady() const
{
    return ready;
}

void ConnectionMockup::sendMessage(const QString &message)
{
    messages.append(message);
}

void ConnectionMockup::test_send_message(const QString &message)
{
    emit messageRecieved(message);
}

TestEmcProgram::TestEmcProgram(QObject *parent) :
    QObject(parent)
{
}

void TestEmcProgram::open()
{
    ConnectionMockup mockup;
    EmcProgram program(&mockup);
    QCOMPARE(...
...
...

As you can see, the class ConnectionMockup is only used by class TestConnection, and I don't need it anywhere else. So, when I try to compile this program, I get following error:

> testemcprogram.o: In function  
> `ConnectionMockup':  
> /home/sasa/Desktop/QtPro/FocoKernel-build-desktop/../FocoKernel/testemcprogram.cpp:29:  
> undefined reference to `vtable for  
> ConnectionMockup'  
> /home/sasa/Desktop/QtPro/FocoKernel-build-desktop/../FocoKernel/testemcprogram.cpp:29:  
> undefined reference to `vtable for  
> ConnectionMockup' testemcprogram.o: In  
> function `~ConnectionMockup':  
> /home/sasa/Desktop/QtPro/FocoKernel-build-desktop/../FocoKernel/testemcprogram.cpp:14:  
> undefined reference to `vtable for  
> ConnectionMockup'

Is it possible to leave declaration here, or I must create header file and move declaration in to that file?

EDIT: Since Mr. Jerry Coffin (thank you Mr. Coffin) suggested that I may not have some virtual functions implemented, I will put here declaration of AbstractConnection so we could review that possibility:

#include <QObject>

class AbstractConnection : public QObject
{
    Q_OBJECT
public:
    explicit AbstractConnection(QObject *parent = 0);
    virtual ~AbstractConnection();

    virtual bool isReady() const = 0;

signals:
    void messageRecieved(const QString &message);

public slots:
    virtual void sendMessage(const QString &message) = 0;

};

SOLUTION: Thanks to @JCooper, @iammilind and @Jerry Coffin we have the solution. After removing destructor from AbstractConnection (since it actually does nothing) and removing Q_OBJECT from ConnectionMockup it works.

解决方案

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.

这篇关于将类声明放在.cpp文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆