2012-04-03 4 views
1

私は小さなアプリケーションを作成しようとしており、auto_ptrを使ってコンパイル時エラーが発生しました。ISO C++で型なしの 'auto_ptr'宣言が禁じられています

私が作成したクラスでスマートなポインタを作成するのはもともと疲れましたが、タイプがint型のスマートポインタを作成しようとすると同じエラーが発生します。私はhere.与えられた例に従っていた。

私は自分自身を叩いてしまう結果になると感じています。

スマートポインタをこのファイルの末尾に宣言します。

#ifndef MAINWINDOW_H 
#define MAINWINDOW_H 

#include <memory.h> 
#include <QMainWindow> 
#include "dose_calac.h" 

namespace Ui { 
class MainWindow; 
} 

class MainWindow : public QMainWindow 
{ 
    Q_OBJECT 

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

private slots: 
    /* 
    Some QT stuff here, removed for clarity/size... 
    */ 

private: 
    Ui::MainWindow *ui; 

    /* 
     Object for storage of data and calculation of DOSE index score. 
    */ 

    std::auto_ptr<int> pdoseIn(new int); // A simple set case, but sill produces an error!?! 

    std::auto_ptr<DOSE_Calac> pdoseIn(new DOSE_Calac); // Original code, error found here at first. 
}; 

#endif // MAINWINDOW_H 

これは私のクラス、dose_calac.hです。

#ifndef DOSE_CALAC_H 
#define DOSE_CALAC_H 

class DOSE_Calac 
{ 
public: 
// constructor 
    DOSE_Calac(); 
// set and get functions live here, removed for clarity/size. 

// function for caulating DOSE indexpoints 
    int CalcDOSEPoints(); 
private: 
    unsigned int dyspnoeaScale; 
    unsigned int fev1; 
    bool smoker; 
    unsigned int anualExacerbations; 
    unsigned int doseIndexPoints; 

}; 

#endif // DOSE_CALAC_H 

喜んでいただいた助けや提案。

+3

サイドノート: 'auto_ptr'はC++ 11では非推奨です。 – iammilind

答えて

3

エラーは、間違ったヘッダーを含めることによって発生します。

std::auto_ptr<int> pdoseIn(new int); 

あなたが持っている:

#include <memory.h> 

あなたはまた

#include <memory> 

書く必要があります代わりに、あなたは、このようにクラスのメンバを初期化することはできませんので、あなたのクラス定義ではより深刻な間違いがあり、それを別途宣言してコンストラクタで初期化する:

std::auto_ptr<int> pdoseIn; 
MainWindow() 
    : pdoseIn(new int) 
{} 
2

クラスメンバー変数を初期化することはできません。クラス宣言でstd::auto_ptr<int> a;を定義し、a(new int)を使用して初期化する必要があります。

2

次のようなクラス宣言内のデータメンバを初期化することはできません。

class MainWindow 
{ 
    std::auto_ptr<int> pdoseIn(new int); 
}; 

あなたはこのようにメンバーを宣言し、コンストラクタでデータメンバを初期化する必要があります。

class MainWindow 
{ 
    std::auto_ptr<int> pdoseIn; 
    MainWindow() 
     : pdoseIn(new int) 
    { 
    } 
}; 
関連する問題