1

小さなアプリケーションを作成しようとしていて、auto_ptr を使用してコンパイル時エラーが発生しました。

私は元々、作成したクラスでスマート ポインターを作成するのにうんざりしていましたが、int 型のスマート ポインターを作成しようとすると同じエラーが発生するので、他に間違っていることがあるはずです。私はここで与えられた例に従っていました。.

これに対する答えは、自分を平手打ちする結果になると感じています。

このファイルの最後でスマート ポインターを宣言します。

#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

感謝して受け取った助けや提案。

4

3 に答える 3

3

エラーは、不適切なヘッダーが含まれていることが原因です。それ以外の

#include <memory.h>

あなたは書くべきです

#include <memory>

また、この方法ではクラス メンバーを初期化できないため、クラス定義にはさらに重大な誤りがあります。

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

個別に宣言し、コンストラクターで初期化する必要があります。

std::auto_ptr<int> pdoseIn;
MainWindow()
    : pdoseIn(new int)
{}
于 2012-04-03T05:05:41.110 に答える
2

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

于 2012-04-03T04:58:30.690 に答える
2

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

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

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

class MainWindow
{
    std::auto_ptr<int> pdoseIn;
    MainWindow ()
        : pdoseIn(new int)
    {
    }
};
于 2012-04-03T05:00:02.727 に答える