c++ - 带有初始化列表的简单构造函数?

标签 c++ constructor ctor-initializer undeclared-identifier

下面我包含了我的 h 文件,我的问题是编译器不喜欢我的带有初始化列表的简单异常类的构造函数。它还说 string is undeclared identifier ,即使我有 #include <string>在 h 文件的顶部。你看到我做错了什么吗?为了进一步解释,这是我正在集成到 Windows 上的 wxWidgets GUI 应用程序中的域类之一。

谢谢!

时间.h

#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

关于c++ - 带有初始化列表的简单构造函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2505277/

相关文章:

c++ - 如何在 gcc (g++) 中编译 C++ 代码以查看重载函数的名称修改?

c++ - 在创建 std::tuple 时从 const char* 或 char* 推导 std::string

c++ - C++ 中的 int[pointer-to-array] 是标准的吗?

python - 如何防止 Python 2.7 的 <Python.h> 在 SWIG 生成的 Python 包装器中包含 <inttypes.h>?

javascript - OOP Javascript - Getter & Setters

c++ - 创建一个包含类的堆栈

c++ - 找不到用户创建的类的构造函数

c++ - 初始化列表使变量未初始化?

c++ - 初始化器列表 *argument* 评估顺序

c++ - 从没有默认构造函数的虚拟基类派生类