![](/img/trans.png)
[英]Initialize simple members in class declaration or in constructor initializer list
[英]Simple constructor with initializer list?
下面我已經包含了我的h文件,我的問題是編譯器不喜歡我的簡單異常類的構造函數與初始化列表。 它也說string is undeclared identifier
,即使我在h文件的頂部有#include <string>
。 你看到我做錯了嗎? 有關進一步說明,這是我在Windows上集成到wxWidgets GUI應用程序中的域類之一。
謝謝!
#pragma once
#include <string>
#include <iostream>
// global constants for use in calculation
const int HOURS_TO_MINUTES = 60;
const int MINUTES_TO_HOURS = 100;
class Time
{
public:
// default Time class constructor
// initializes all vars to default values
Time(void);
// ComputeEndTime computes the new delivery end time
// params - none
// preconditions - vars will be error-free
// postconditions - the correct end time will be returned as an int
// returns an int
int ComputeEndTime();
// GetStartTime is the getter for var startTime
// params - none
// returns an int
int GetStartTime() { return startTime; }
// GetEndTime is the getter for var endTime
// params - none
// returns an int
int GetEndTime() { return endTime; }
// GetTimeDiff is the getter for var timeDifference
// params - none
// returns a double
double GetTimeDiff() { return timeDifference; }
// SetStartTime is the setter for var startTime
// params - an int
// returns void
void SetStartTime(int s) { startTime = s; }
// SetEndTime is the setter for var endTime
// params - an int
// returns void
void SetEndTime(int e) { endTime = e; }
// SetTimeDiff is the setter for var timeDifference
// params - a double
// returns void
void SetTimeDiff(double t) { timeDifference = t; }
// destructor for Time class
~Time(void);
private:
int startTime;
int endTime;
double timeDifference;
};
class HourOutOfRangeException
{
public:
// param constructor
// initializes message to passed paramater
// preconditions - param will be a string
// postconditions - message will be initialized
// params a string
// no return type
HourOutOfRangeException(string pMessage) : message(pMessage) {}
// GetMessage is getter for var message
// params none
// preconditions - none
// postconditions - none
// returns string
string GetMessage() { return message; }
// destructor
~HourOutOfRangeException() {}
private:
string message;
};
class MinuteOutOfRangeException
{
public:
// param constructor
// initializes message to passed paramater
// preconditions - param will be a string
// postconditions - message will be initialized
// params a string
// no return type
MinuteOutOfRangeException(string pMessage) : message(pMessage) {}
// GetMessage is getter for var message
// params none
// preconditions - none
// postconditions - none
// returns string
string GetMessage() { return message; }
// destructor
~MinuteOutOfRangeException() {}
private:
string message;
};
class PercentageOutOfRangeException
{
public:
// param constructor
// initializes message to passed paramater
// preconditions - param will be a string
// postconditions - message will be initialized
// params a string
// no return type
PercentageOutOfRangeException(string pMessage) : message(pMessage) {}
// GetMessage is getter for var message
// params none
// preconditions - none
// postconditions - none
// returns string
string GetMessage() { return message; }
// destructor
~PercentageOutOfRangeException() {}
private:
string message;
};
class StartEndException
{
public:
// param constructor
// initializes message to passed paramater
// preconditions - param will be a string
// postconditions - message will be initialized
// params a string
// no return type
StartEndException(string pMessage) : message(pMessage) {}
// GetMessage is getter for var message
// params none
// preconditions - none
// postconditions - none
// returns string
string GetMessage() { return message; }
// destructor
~StartEndException() {}
private:
string message;
};
string
在命名空間std
,因此您需要對其進行限定: std::string
。 初始化列表與問題無關。
在一個不相關的說明中,您可能會考慮讓您的異常類派生自標准庫異常之一,例如std::runtime_error
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.