私は比較的小さなC++プロジェクトを持っており、小さなヘルパー関数などを含むUtilsヘッダーファイルを作ることにしました。テンプレートを使用している関数を宣言していたとき、私はテンプレートを必要としない関数を作成しようとしましたが、うまくいけません。テンプレートなしでC++関数を宣言することはできません
結果は、リンカエラーです。既に定義されています。(ファイル).obj
私は単純なvoid関数を宣言することすらできません。テンプレートを持たないものはリンカーエラーです。
私には、これを引き起こす可能性のあるものは何もありません。ここでは、ヘッダファイルのコードです...事前に感謝します。
#pragma once
namespace Utils
{
std::string GetActiveWindowTitle()
{
// This doesnt work either, also gives linker error.
return active_window;
}
template<typename T>
void Print(char * value, T printValue)
{
std::cout << value << ": " << printValue << std::endl;
}
template<typename T>
void Print(T printValue)
{
std::cout << "DEBUG: " << printValue << std::endl;
}
void PrintStr(std::string str)
{
// This doesn't work because it doesnt have the template, it gives a linker error
std::cout << "DEBUG: " << str.c_str() << std::endl;
}
}
は 'あなたのヘッダー内の関数定義にinline'を追加、または他の別々の翻訳単位(.cppファイル)に関数定義を移動するを参照してください。 。 * function-templates *の 'inline'指定子は不要です。 – WhiZTiM
ヒント:テンプレートは暗黙のうちに' inline'です。 – Rakete1111