c++ - 在另一个类中实例化一个类

标签 c++ qt opencv ps3

对于 Qt,我是 C++ 的新手。我有以下问题:我想在 Qt header 公共(public)部分中实例化一个 CLEyeCameraCapture 对象,但是我从 CLEyeCameraCapture.h 中得到大量语法错误。

我收到以下(德语)语法错误:

CLEyeCameraCapture.h(7) : error C2146: Syntaxfehler: Fehlendes ';' vor Bezeichner '_windowName' CLEyeCameraCapture.h(7) : error C4430: Fehlender Typspezifizierer - int wird angenommen. Hinweis: "default-int" wird von C++ nicht unterstützt.

CLEyeCameraCapture.h(7): error C4430: Fehlender Typspezifizierer - int wird angenommen. Hinweis: "default-int" wird von C++ nicht unterstützt.

CLEyeCameraCapture.h(8) : error C2146: Syntaxfehler: Fehlendes ';' vor Bezeichner '_cameraGUID' CLEyeCameraCapture.h(8) : error C4430: Fehlender Typspezifizierer - int wird angenommen. Hinweis: "default-int" wird von C++ nicht unterstützt.

……等等……

在此先感谢您的帮助。这是我的代码:

qtdevc.h(我的应用程序类的头文件)

#ifndef QTDEVC_H
#define QTDEVC_H

#include <QtGui/QMainWindow>
#include "ui_qtdevc.h"
#include <QString>
#include <QDebug>
#include <CLEyeCameraCapture.h>
#include <stdafx.h>

class qtDEVC : public QMainWindow
{
    Q_OBJECT

public:
    qtDEVC(QWidget *parent = 0, Qt::WFlags flags = 0);
    ~qtDEVC();
    Ui::qtDEVCClass ui;
    CLEyeCameraCapture::CLeyeCameraCapture cam;

private:
    QPushButton *PushButton_startCam;
    QPushButton *PushButton_stopCam;
    QPushButton *PushButton_startLogging;
    QPushButton *PushButton_quit;
    QLineEdit *lineEditID;


// begin new code
public slots:
    int startCam();
    void stopCam();
    void quit();
// end new code

};

#endif // QTDEVC_H

CLEyeCameraCapture.h

#ifndef CLEYECAMERACAPTURE_H
#define CLEYECAMERACAPTURE_H

// Sample camera capture class
class CLEyeCameraCapture
{
    CHAR _windowName[256];
    GUID _cameraGUID;
    CLEyeCameraInstance _cam;
    CLEyeCameraColorMode _mode;
    CLEyeCameraResolution _resolution;
    float _fps;
    HANDLE _hThread;
    bool _running;
    std::string _participant;
public:
    CLEyeCameraCapture(LPSTR windowName, GUID cameraGUID, CLEyeCameraColorMode mode, CLEyeCameraResolution resolution, float fps) :
        _cameraGUID(cameraGUID), _cam(NULL), _mode(mode), _resolution(resolution), _fps(fps), _running(false)
    {
        strcpy(_windowName, windowName);
    }

    double GetRandomNormalized();
    bool StartCapture(std::string ID);
    void StopCapture();
    void IncrementCameraParameter(int param);
    void DecrementCameraParameter(int param);
    void Run();
    static DWORD WINAPI CaptureThread(LPVOID instance);
};

我的 Qt 应用程序(尚未清理)

#include "qtdevc.h"
#include <QtGui>
#include <QDebug>
#include <QtGui/QApplication>
#include "stdafx.h"
#include "CLEyeCameraCapture.h"
using namespace std;

qtDEVC::qtDEVC(QWidget *parent, Qt::WFlags flags)
    : QMainWindow(parent, flags)
{
    ui.setupUi(this);
    connect (ui.pushButton_startCam, SIGNAL( clicked() ),this,SLOT( startCam() ) );
    connect (ui.pushButton_quit, SIGNAL( clicked() ),this,SLOT( quit() ) );
    connect (ui.pushButton_stopCam, SIGNAL ( clicked() ),this,SLOT( stopCam() ) );
}

qtDEVC::~qtDEVC()
{
}

//get ID of participant
int qtDEVC::startCam()
{
    //qt part

    //ui.startCam->setText("Hi!");
    QString ID;
    //get qString Participant Number
    ID = ui.lineEditID->text();
    //convert to standard string
    std::string IDString = ID.toLocal8Bit().constData();
    //qDebug()<<ID;
    ui.lineEditID->setDisabled(true);
    ui.pushButton_startCam->setDisabled(true);


    //moved this to here from main
    CLEyeCameraCapture *cam[2] = { NULL };
    srand(GetTickCount());
    // Query for number of connected cameras

...

编辑

包括

"#include " before "#include " in qDEVC.h

解决了编译中语法错误的问题,但现在我得到错误 C2146,C3210C2602尝试实例化时

CLEyeCameraCapture::CLeyeCameraCapture cam;

什么是正确的方法?

CLEyeCameraCapture::CLeyeCameraCapture *cam[2]; ??

最佳答案

要修复编译错误,

Please Include "#include <stdafx.h>" before "#include <CLEyeCameraCapture.h>" in qDEVC.h.

了解更多信息 Compiler Error C2146

规则:最佳做法是首先包含标准 header ,然后是您自己的 header 。 C++ Header order

关于c++ - 在另一个类中实例化一个类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18461687/

相关文章:

c++ - 在 IsClassT<T> 中,为什么使用 "int C::*"?我对 int 类型感到困惑

c++ - QSqlQuery::value: 未定位在有效记录上

c++ - 有没有更快的方法在 OpenCV 中应用亮度?

python - PyPlot 与 OpenCV 中的工件 imshow

c++ - 数组相关错误 - 无效类型..?

c++ - 调试期间将鼠标悬停在变量上不显示值?

c++ - “Promoted Widgets”对话框中的 "Global include"复选框是什么意思?

c++ - 如何从 Qt 中的标准 C++ 函数中获取值

python - 聚类边界框并在其上画线(OpenCV、Python)

c++ - 在函数返回类型中返回右值引用与按值返回