[英]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.