![](/img/trans.png)
[英]ISO C++ forbids declaration of ‘it’ with no type for auto iterator?
[英]ISO C++ forbids declaration of 'auto_ptr' with no type
我正在嘗試編寫一個小型應用程序,並使用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
非常感謝您的任何幫助或建議。
您的錯誤是由於包含不正確的標題引起的。 代替
#include <memory.h>
你應該寫
#include <memory>
另外,您的類定義中存在更嚴重的錯誤,因為您不能以這種方式初始化類成員:
std::auto_ptr<int> pdoseIn(new int);
您必須分別聲明它並在構造函數中初始化:
std::auto_ptr<int> pdoseIn;
MainWindow()
: pdoseIn(new int)
{}
您不能像這樣初始化類成員變量,您需要通過執行std::auto_ptr<int> a;
在類聲明中對其進行定義std::auto_ptr<int> a;
並使用a(new int)
在ctor中對其進行初始化。
您不能像這樣在類聲明中初始化數據成員:
class MainWindow
{
std::auto_ptr<int> pdoseIn(new int);
};
您需要這樣聲明成員,並在構造函數中初始化數據成員:
class MainWindow
{
std::auto_ptr<int> pdoseIn;
MainWindow ()
: pdoseIn(new int)
{
}
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.