我正在为我的C++编程类工作,它涉及实现哈希映射.我的讲师给了我们一个头文件,我们需要使用我们的哈希映射类.提供的头文件包含以下行:
typedef std::function<unsigned int(const std::string&)> HashFunction;
Run Code Online (Sandbox Code Playgroud)
根据我对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;
~~~~~~~~~~~~~~~~~~~~~^
Run Code Online (Sandbox Code Playgroud)
HashMap.h文件有
#include <functional>
Run Code Online (Sandbox Code Playgroud)
在顶部,如果重要的话.
有谁知道我为什么会收到这些错误?
我有两个模型,User和QuestionEvent.
class User < ActiveRecord::Base
...
has_many :questions
has_many :asked_questions, :class_name => 'QuestionEvent', :foreign_key => 'questioner_id'
has_many :received_questions, :class_name => 'QuestionEvent', :foreign_key => 'respondent_id'
end
class QuestionEvent < ActiveRecord::Base
...
belongs_to :question
belongs_to :questioner, :class_name => 'User'
belongs_to :respondent, :class_name => 'User'
end
Run Code Online (Sandbox Code Playgroud)
当我调用QuestionEvent.first.questioner或QuestionEvent.first.respondent时,我得到了预期的用户.但是,调用User.first.asked_questions给了我:
NoMethodError: undefined method `asked_questions' for #<User:0x007fc687d53ae0>
Run Code Online (Sandbox Code Playgroud)
谁能看出我在这里犯的是什么错误?
QuestionEvent的数据库模式是:
t.integer :question_id
t.integer :questioner_id
t.integer :respondent_id
Run Code Online (Sandbox Code Playgroud)