私はC++ 11の新機能ですので、今日はstd::function
で実験しています。クラスポインタに文字列のハッシュテーブルを作成するために使用しようとしています。小さなアセンブラのために私が作成しています:C++ - クラスメソッド関数ポインタのunordered_mapの初期設定リスト
assemble.cpp
#include <iostream>
#include <unordered_map>
#include <functional>
#include <vector>
#include <string>
class Assembler {
public:
// Assembles the source file and writes it
bool assemble(std::string output_file);
// Creates the symbol table from a (.s) file
bool create_symbol_table(std::string file_name);
private:
// Hash table for opcode to functions
std::unordered_map<std::string,std::function<bool(std::vector<std::string>)>>
opcode_map =
{
{"add",assemble_data_processing}
};
bool assemble_data_processing(std::vector<std::string> instruction);
};
bool Assembler::assemble_data_processing(std::vector<std::string> instruction) {
std::cout << "Data Processing" << std::endl;
return false;
}
は、これは私のG ++でコンパイル時エラーを与える:
g++ -Wall -g -Werror -std=c++11 -o assemble.o assemble.cpp
assemble.cpp:28:5: error: could not convert ‘{{"add", ((Assembler*)this)->Assembler::assemble_data_processing}}’ from ‘<brace-enclosed initializer list>’ to ‘std::unordered_map<std::__cxx11::basic_string<char>, std::function<bool(std::vector<std::__cxx11::basic_string<char> >)> >’ }; ^
しかし、機能に非クラスの関数を作る場合、それは問題なくコンパイルされます。
bool assemble_data_processing(std::vector<std::string> instruction) {
std::cout << "Data Processing" << std::endl;
return false;
}
私はクラスメソッドのためのブレース初期化子リストにunordered_map
を初期化するにはどうすればよいですか?
'assemble_data_processing'を静的メンバー関数にしようとしましたか? – Frank