2013-03-10 69 views
-2

基本上我想繼承了從摩擦到基地(甚至反過來),但是,它不識別我放在類。問題與Qt的繼承

base.h

#ifndef BASE_H 
#define BASE_H 
#include <QMainWindow> 

namespace Ui { 
    class Base; 
} 

class Base : public QMainWindow{ 
    Q_OBJECT 

public: 
    explicit Base(QWidget *parent = 0); 
    ~Base(); 

private: 
    Ui::Base *ui; 
}; 
#endif // BASE_H 

friction.h:

#ifndef FRICTION_H 
#define FRICTION_H 

class Friction : public Base{ // THIS IS WHERE THE ERROR IS 
    public: 
Friction(); 
}; 

#endif // FRICTION_H 

base.cpp

#include "friction.h" 
#include "base.h" 
#include "ui_base.h" 

Base::Base(QWidget *parent) :QMainWindow(parent),ui(new Ui::Base){ 
    ui->setupUi(this); 
} 

Base::~Base(){ 
    delete ui; 
} 

friction.cpp

#include "friction.h" 
#include "base.h" 
#include "ui_base.h" 

Friction::Friction(){ 
} 

最後的main.cpp

int main(int argc, char *argv[]){ 
    QApplication a(argc, argv); 
    Base w; 
    w.show(); 

    Friction f; 

    return a.exec(); 
} 

我收到「預期類名前‘{’令牌」的錯誤,我已削減了項目分解和我一樣可以和錯誤仍然出現,我真的不知道爲什麼。

我對C++相當陌生,但是我發現在一個基本程序上沒有太多問題的繼承,但是轉移到Qt後,我似乎無法使它工作。我已經嘗試了很多關於改變包含等等的東西,因爲我完全沒有意識到爲什麼它不能識別這個類。

回答

2

如果摩擦繼承基地,應該比你放:

#include "base.h" 

在friction.h文件,像這樣:

#ifndef FRICTION_H 
#define FRICTION_H 

#include "base.h" 

class Friction : public Base{ // THIS IS WHERE THE ERROR IS 
    public: 
Friction(); 
}; 
+1

和可能,你在'Friction' – troyane 2013-03-10 17:06:58

+1

忘了'Q_OBJECT'好的非常感謝。對不起,我的無知我只在2天前開始C++。 – iyop45 2013-03-10 17:08:00

+0

@ user1在學習Qt之前,您應該學習C++。 Qt具有強大的架構範式,主要基於'moc'可以做什麼和不可以做什麼,這意味着您可以學習相當多的傾斜和狹隘的語言視圖。 – cmannett85 2013-03-10 17:21:54