Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/130.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
同时运行同一类的对象 我昨天学习C++,我想编写一个警报系统,让用户设置自己的警报,用警报对象。 我尝试的方法本身工作正常,但只允许用户运行一个:如果我尝试使用start方法启动第二个警报,程序将等待第一个警报“响”以启动第二个警报_C++_Xcode_Macos - Fatal编程技术网

同时运行同一类的对象 我昨天学习C++,我想编写一个警报系统,让用户设置自己的警报,用警报对象。 我尝试的方法本身工作正常,但只允许用户运行一个:如果我尝试使用start方法启动第二个警报,程序将等待第一个警报“响”以启动第二个警报

同时运行同一类的对象 我昨天学习C++,我想编写一个警报系统,让用户设置自己的警报,用警报对象。 我尝试的方法本身工作正常,但只允许用户运行一个:如果我尝试使用start方法启动第二个警报,程序将等待第一个警报“响”以启动第二个警报,c++,xcode,macos,C++,Xcode,Macos,如何同时运行两个警报? 感谢您花时间阅读本文。 (OSX Sierra,代码8.2) main.cpp using namespace std; int main(int argc, const char * argv[]) { Alarm test(12, 12, "foo"); test.start(); class Alarm { public: Alarm(int hour, int minute, std::string speech); v

如何同时运行两个警报? 感谢您花时间阅读本文。 (OSX Sierra,代码8.2)

main.cpp

using namespace std;

int main(int argc, const char * argv[]) {
    Alarm test(12, 12, "foo");
    test.start();
class Alarm {
    public:
    Alarm(int hour, int minute, std::string speech);
    void start();
    void stop();
    bool isStopped() const;
    std::string getStats() const;

    private:
    int m_hour;
    int m_minute;
    std::string m_speech;
    bool m_stopped;
};
using namespace std;

Alarm::Alarm(int hour, int minute, string speech) {
    m_hour = hour;
    m_minute = minute;
    m_speech = speech;
    m_stopped = false;
}

void Alarm::start() {
    int currentHour, currentMinute;

    while (!Alarm::isStopped()) {
        time_t now = time(NULL);
        struct tm *current = localtime(&now);

        currentHour = current->tm_hour;
        currentMinute = current->tm_min;

        if (currentHour == m_hour && currentMinute == m_minute) {
            cout << m_speech << endl;
            m_stopped = true;
        }
        else {
           this_thread::sleep_for(chrono::milliseconds(60000));
        }
     }
}    

void Alarm::stop() {
    m_stopped = true;
}

bool Alarm::isStopped() const {
    return m_stopped;
}
警报.hpp

using namespace std;

int main(int argc, const char * argv[]) {
    Alarm test(12, 12, "foo");
    test.start();
class Alarm {
    public:
    Alarm(int hour, int minute, std::string speech);
    void start();
    void stop();
    bool isStopped() const;
    std::string getStats() const;

    private:
    int m_hour;
    int m_minute;
    std::string m_speech;
    bool m_stopped;
};
using namespace std;

Alarm::Alarm(int hour, int minute, string speech) {
    m_hour = hour;
    m_minute = minute;
    m_speech = speech;
    m_stopped = false;
}

void Alarm::start() {
    int currentHour, currentMinute;

    while (!Alarm::isStopped()) {
        time_t now = time(NULL);
        struct tm *current = localtime(&now);

        currentHour = current->tm_hour;
        currentMinute = current->tm_min;

        if (currentHour == m_hour && currentMinute == m_minute) {
            cout << m_speech << endl;
            m_stopped = true;
        }
        else {
           this_thread::sleep_for(chrono::milliseconds(60000));
        }
     }
}    

void Alarm::stop() {
    m_stopped = true;
}

bool Alarm::isStopped() const {
    return m_stopped;
}
报警.cpp

using namespace std;

int main(int argc, const char * argv[]) {
    Alarm test(12, 12, "foo");
    test.start();
class Alarm {
    public:
    Alarm(int hour, int minute, std::string speech);
    void start();
    void stop();
    bool isStopped() const;
    std::string getStats() const;

    private:
    int m_hour;
    int m_minute;
    std::string m_speech;
    bool m_stopped;
};
using namespace std;

Alarm::Alarm(int hour, int minute, string speech) {
    m_hour = hour;
    m_minute = minute;
    m_speech = speech;
    m_stopped = false;
}

void Alarm::start() {
    int currentHour, currentMinute;

    while (!Alarm::isStopped()) {
        time_t now = time(NULL);
        struct tm *current = localtime(&now);

        currentHour = current->tm_hour;
        currentMinute = current->tm_min;

        if (currentHour == m_hour && currentMinute == m_minute) {
            cout << m_speech << endl;
            m_stopped = true;
        }
        else {
           this_thread::sleep_for(chrono::milliseconds(60000));
        }
     }
}    

void Alarm::stop() {
    m_stopped = true;
}

bool Alarm::isStopped() const {
    return m_stopped;
}
使用名称空间std;
报警::报警(整数小时、整数分钟、字符串语音){
m_小时=小时;
分钟=分钟;
m_speech=语音;
m_stopped=假;
}
无效报警::开始(){
int currentHour,currentMinute;
而(!Alarm::isStopped()){
time\u t now=时间(空);
struct tm*current=localtime(&now);
当前小时=当前->tm\u小时;
currentMinute=当前->tm_min;
如果(currentHour==m_hour&¤tMinute==m_minute){

cout我能想到的最简单的方法是使用s:

并使用或类似的方法保护
m_stopped
成员变量:

class Alarm {
    public:
    Alarm(int hour, int minute, std::string speech);
    Alarm(const Alarm& other);
    Alarm& operator=(const Alarm& other);
    void start();
    void stop();
    bool isStopped() const;
//    std::string getStats() const;

    private:
    int m_hour;
    int m_minute;
    std::string m_speech;
    std::atomic<bool> m_stopped;
};
类报警{
公众:
报警(整小时、整分钟、标准::字符串语音);
警报(常数警报和其他);
报警和操作员=(持续报警和其他);
void start();
无效停止();
布尔常数;
//std::string getStats()常量;
私人:
国际货币单位小时;
国际分钟;
std::字符串m_语音;
std::原子m_停止;
};

Alarm::Alarm(整数小时、整数分钟、字符串语音)
:m_小时(小时)、m_分钟(分钟)、m_演讲(演讲)、m_停止(错误){
}
报警::报警(常数报警和其他)
:m_hour(other.m_hour)、m_minute(other.m_minute)、m_speech(other.m_speech)、m_stopped(other.isStopped()){
}
报警和报警::操作员=(常数报警和其他){
m_小时=其他m_小时;
m_分钟=其他m_分钟;
m_语音=其他m_语音;
m_stopped.store(other.isStopped());
归还*这个;
}
无效报警::开始(){
int currentHour,currentMinute;
而(!Alarm::isStopped()){
time\u t now=时间(空);
struct tm*current=localtime(&now);
当前小时=当前->tm\u小时;
currentMinute=当前->tm_min;
如果(currentHour==m_hour&¤tMinute==m_minute){

CUT

你需要多线程来完成这个操作,但是对于你的情况,我建议检查一个接一个的弹出。

< P>你可以在自己的线程中运行每个告警,但是对于刚开始C++的人来说,这可能是有点先进的,而且它是过多的。 处理多个警报的标准方法是搜索所有警报,找到将首先“响”的警报,并为该警报休眠足够长的时间。当警报触发时,将其从列表中删除,然后搜索将首先响的下一个警报。(如果将报警保存在分类容器中,例如
std::map
,甚至仅
std::map
,则查找第一个报警会更容易。)

记住,当需要同时触发多个警报时,要处理这些情况


如果您希望用户能够在系统运行时与系统交互(例如,添加新警报、删除警报等),您仍然需要多线程-但您应该只为下一个警报休眠一个线程,而不是每个警报休眠一个线程。

听起来您需要学习多线程。而不是随机互联网上的“教程”,从书中学习语言。这是一些好方法的列表。@RawN谢谢,我会检查这些!当我试图启动线程时,我收到一个“无对象参数调用非静态成员函数”的代码…@sohomangue抱歉,我总是混淆顺序(这就是为什么我发出警告的原因)我已经安装了一个可编译的版本,看一下。TLE是因为参数化。实际上我用Python编码了这个系统,每个警报都运行了一个线程,我也想到了在C++中做同样的事情……但是我不知道怎么做。即使它是过度的,我如何通过一个对象的方法来运行一个线程?如果我不能做别的,不过我会试试那种方法