首页 \ 问答 \ C ++编译错误 - “命名空间std中没有命名为'function'的类型”(C++ Compile Error - “no type named 'function' in namespace std”)

C ++编译错误 - “命名空间std中没有命名为'function'的类型”(C++ Compile Error - “no type named 'function' in namespace std”)

我正在为我的C ++编程类工作,它涉及实现一个哈希映射。 我的讲师给了我们一个头文件,我们需要使用我们的哈希映射类。 提供的头文件包含以下行:

typedef std::function<unsigned int(const std::string&)> HashFunction;

根据我对C ++的有限理解,这会将HashFunction类型定义为std :: function。 但是,当我编译代码时,我得到错误:

./HashMap.h:46:15: error: no type named 'function' in namespace 'std'
        typedef std::function<unsigned int(const std::string&)> HashFunction;
                ~~~~~^
./HashMap.h:46:23: error: expected member name or ';' after declaration specifiers
        typedef std::function<unsigned int(const std::string&)> HashFunction;
        ~~~~~~~~~~~~~~~~~~~~~^

HashMap.h文件有

#include <functional>

在顶部,如果重要的话。

有谁知道我为什么会收到这些错误?


I'm working on an assignment for my C++ programming class which involves implementing a hash map. My instructor has given us a header file that we are required to use with our hash map class. The provided header file contains the line:

typedef std::function<unsigned int(const std::string&)> HashFunction;

From my (limited) understanding of C++, this would define the type HashFunction as a std::function. However, when I compile the code, I get the errors:

./HashMap.h:46:15: error: no type named 'function' in namespace 'std'
        typedef std::function<unsigned int(const std::string&)> HashFunction;
                ~~~~~^
./HashMap.h:46:23: error: expected member name or ';' after declaration specifiers
        typedef std::function<unsigned int(const std::string&)> HashFunction;
        ~~~~~~~~~~~~~~~~~~~~~^

The HashMap.h file has

#include <functional>

at the top, if it matters.

Does anyone know why I'm getting these errors?


原文:https://stackoverflow.com/questions/13213286
更新时间:2023-12-25 19:12

最满意答案

此代码用于检查警报是否已安排:

boolean weeklyAlarmUp = (PendingIntent.getBroadcast(context, 0, new Intent(context,
           ScheduledWeeklyService.class), PendingIntent.FLAG_NO_CREATE) != null);

不起作用,因为你依赖PendingIntent的现有。 取消警报时,即使警报已取消,您也不会取消PendingIntent因此它仍然存在。

要解决此问题,请确保在取消警报时也取消PendingIntent ,如下所示:

// cancel alarm
alarmMgrWeekly.cancel(piWeekly);
// Cancel PendingIntent
piWeekly.cancel();

This code to check if the alarm has been scheduled:

boolean weeklyAlarmUp = (PendingIntent.getBroadcast(context, 0, new Intent(context,
           ScheduledWeeklyService.class), PendingIntent.FLAG_NO_CREATE) != null);

doesn't work because you are relying on the existing of the PendingIntent. When you cancel the alarm you aren't canceling the PendingIntent so it still exists, even though the alarm has been canceled.

To fix that, make sure you also cancel the PendingIntent when you cancel the alarm, like this:

// cancel alarm
alarmMgrWeekly.cancel(piWeekly);
// Cancel PendingIntent
piWeekly.cancel();

相关问答

更多
  • ToggleButton扩展了View,所以你可以简单地使用View.setOnClickListener() ,就像这样: // get your ToggleButton ToggleButton b = (ToggleButton) findViewById(R.id.myButton); // attach an OnClickListener b.setOnClickListener(new OnClickListener() { @Override public void on ...
  • 请参阅问题附带的评论。 未分配变量“context”,导致Null Reference Exception。 设置Air Plane模式设置背后的逻辑没有任何问题。 See comments attached to question. The variable 'context' was not assigned, resulting in a Null Reference Exception. There isn't anything wrong with the logic behind setting ...
  • 我假设你的意思是“只有一个”而不是“只有当”。 在这种情况下,您可以使用RadioButton(源自ToggleButton),并在ItemTemplate中的_MiniDrawerButton上设置GroupName。 看起来您可能已经在使用自定义ControlTemplate,因此只需更改Style和ControlTemplate TargetTypes就可以对RadioButton使用相同的控件。 I assume you mean "only one" instead of "only when". ...
  • 我想你正在寻找类似的东西。 您也可以以相同的方式更改BorderBrush。