のテンプレート関数を呼び出す:エラー私はそうのように、非テンプレートクラス内のテンプレート機能を持っている非テンプレートクラスC++
class Foo
{
public:
template <class T>
void func(T& val)
{
//do work here
}
}
その後、main.cppにに私がやっている:
Foo a;
std::string val;
a.func<std::string>(val); //this line gives the error
を
"> '"の前に ""の一次式が予想されるというエラーが発生します。だから私は、迅速なGoogle検索を行うと、誰もが簡単な解決策を提案することを見つける:
a.template func<std::string>(val);
唯一の問題は、私はまだ正確に同じエラーを取得しています、です。
EDIT:
私は完全な例を与えていなかった理由は、それが外部のライブラリや質問を不明瞭長いコードが含まれているためですが、上記の単純化されたコードので、それをカットしていません。ここで私が書いた完全なクラスです:
class ConfigFileReader
{
public:
ConfigFileReader() { }
ConfigFileReader(const std::string& config_file_path)
{
setConfigFilePath(config_file_path);
}
~ConfigFileReader() { }
void setConfigFilePath(const std::string& config_file_path)
{
try
{
root_node_ = YAML::LoadFile(config_file_path);
}
catch(const YAML::BadFile& file_load_exception)
{
printf("Error opening YAML file. Maybe the file path is incorrect\n%s", file_load_exception.msg.c_str());
}
}
template<class T>
bool getParam(const std::string& param_key, T& param_value)
{
if (root_node_.IsNull() || !root_node_.IsDefined())
{
printf("Root node is undefined or not set");
return false;
}
YAML::Node node = YAML::Clone(root_node_);
std::vector<std::string> split_name;
boost::split(split_name, param_key, boost::is_any_of("/"));
for(const std::string& str: split_name)
{
if (!node.IsMap())
{
std::cout << "Parameter was not found (Node is null)." << str << std::endl; //replace with printf
return false;
}
node = node[str];
}
if (node.IsNull() || !node.IsDefined())
{
std::cout << "Parameter was not found (Node is null/undefined)." << std::endl;
return false;
}
try
{
param_value = node.as<T>();
return true;
}
catch (const YAML::TypedBadConversion<T>& type_conversion_exception)
{
std::cout << "Error converting param value into specified data type" << std::endl;
std::cout << type_conversion_exception.msg << std::endl;
}
return false;
}
private:
YAML::Node root_node_;
};
その後、別のcppのファイルには、メイン機能さ
int main(int argc, char** argv)
{
if (argc != 2)
{
printf("Incorrect number of arguments given");
return EXIT_FAILURE;
}
printf("Config file path: %s", argv[1]);
ConfigFileReader config_file_reader(std::string(argv[1]));
std::string param_out;
bool success = config_file_reader.template getParam<std::string>("controller/filepath", param_out); //<-- ERROR HERE
return EXIT_SUCCESS;
}
コンパイラ:のgcc 4.8.4、およびC++ 11フラグセットコンパイルする。
EDIT 2: コードに文字列引数コンストラクタが追加されました。
あいまいであるように、それを呼び出し、'を含むことを忘れましたか? –
Quentin
string
を受け入れConfigFileReader
のコンストラクタを定義します](http://coliru.stacked-crooked.com/a/ece8129982db7ee2)。あなたは '番号ライブラリが含まれています。 –
Ali250
[最小限の、完全で検証可能な例](http://stackoverflow.com/help/mcve)を作成し、私たちに提示する必要があります。 –