2011-02-10 20 views
0

私は共有ライブラリを作成しています。考え方は次のとおりです。 ライブラリ内の共有関数は、intまたはdoubleパラメータで呼び出されます。両方を受け入れる必要があります。関数のある時点では、それがintかdoubleかに応じてパラメータを使って何かを行う「private」関数が呼び出されます。 私はライブラリ関数のテンプレートを使うことに決めました。私が正しく理解していれば、コンパイラはパラメータの型を知っている必要があります。そうでなければ、ライブラリをコンパイルできませんでした。したがって、intとdoubleの2つのテンプレートをインスタンス化します。 問題は、コンパイラは、プライベート関数のどのバージョンを呼び出すべきか分からないようですが、そのパラメータの型を知っています。C++テンプレート+共有ライブラリ=>あいまいな呼び出し

それは、私はそれと間違って何ができるか分からない、

ペトル

library.hpp


#include < iostream > 

namespace { 

void printNumber(int const n); 
void printNumber(double const n); 

} 

namespace library { 

template < typename T > 
void doSomething(T const number); 

} 

ライブラリ:-)私を助けてください夜遅くです.cpp


#include "library.hpp" 

using namespace std; 

void printNumber(int const n) { 
    cout << "This was an int." << endl; 
} 

void printNumber(double const n) { 
    cout << "This was a double." << endl; 
} 

template < typename T > 
void library::doSomething(T const number) { 
    // ... 
    // Do something that does not depend on T at all... 
    // ... 
    printNumber(number); 
} 

template void library::doSomething(int const number); 
template void library::doSomething(double const number); 

MAIN.CPP


#include "library.hpp" 

int main(int const argc, char const * (argv) []) { 
    library::doSomething(10); 
    library::doSomething(10.0); 
    return 0; 
} 

コンパイラ


../src/library.cpp: In function ‘void library::doSomething(T) [with T = int]’: 
../src/library.cpp:21:52: instantiated from here 
../src/library.cpp:18:2: error: call of overloaded ‘printNumber(const int&)’ is ambiguous 
../src/library.cpp:5:6: note: candidates are: void printNumber(int) 
../src/library.cpp:9:6: note:     void printNumber(double) 
/home/petmal/Eclipse_Projects/library/include/library.hpp:6:6: note:     void::printNumber(double) 
/home/petmal/Eclipse_Projects/library/include/library.hpp:5:6: note:     void::printNumber(int) 
../src/library.cpp: In function ‘void library::doSomething(T) [with T = double]’: 
../src/library.cpp:22:55: instantiated from here 
../src/library.cpp:18:2: error: call of overloaded ‘printNumber(const double&)’ is ambiguous 
../src/library.cpp:5:6: note: candidates are: void printNumber(int) 
../src/library.cpp:9:6: note:     void printNumber(double) 
/home/petmal/Eclipse_Projects/library/include/library.hpp:6:6: note:     void::printNumber(double) 
/home/petmal/Eclipse_Projects/library/include/library.hpp:5:6: note:     void::printNumber(int) 
+0

'printNumber(const int&n)'も宣言してください。これはエラーメッセージに表示されます。 –

+0

@ J-16 SDiZ違いはありません... – Petr

答えて

2

あなたの匿名名前空間の使用が間違っています。匿名の名前空間でprintNumber()を宣言していますが、それをグローバルスコープで定義しています。これは、2つのprintNumber(int)の2つを持つため、あいまいになります。printNumber(double)の機能です。

これを試してみてください:

library.hpp:

#ifndef LIBRARY 
#define LIBRARY 

namespace library { 
    template < typename T > 
    void doSomething(T const number); 
} 

#endif 

library.cpp:

#include <iostream> 

#include "library.hpp" 

using namespace std; 

namespace { 
    void printNumber(int const n) { 
     cout << "This was an int." << endl; 
    } 

    void printNumber(double const n) { 
     cout << "This was a double." << endl; 
    } 
} 

template < typename T > 
void library::doSomething(T const number) { 
    // ... 
    // Do something that does not depend on T at all... 
    // ... 
    printNumber(number); 
} 

template void library::doSomething<int>(int const number); 
template void library::doSomething<double>(double const number); 

main.cppに:あなたの例のように。

+0

ありがとう、これは動作します。定義をヘッダーファイルに移動するもう1つの方法も、その作業を行います。どちらが良いかはっきりしない...これは妥当と思われる... – Petr

+0

はい。私は他の答えがなぜ削除されたのかわかりません - この種のテンプレートの具体的なインスタンス化は、状況によっては完全な解決策ではないかもしれません。それでも、ターゲットプラットフォームで動作し、特定のインスタンシエーションセットしか必要としない場合は、このようにコードを分割してコンパイル時間を短縮することができます。これは常に優れています。 –

0

私はあいまいさが "10" はintまたは二重のいずれかになりますという事実から来ていると信じています。 1つまたは他のものとしてキャストし、それが修正されているかどうかを確認してください。

+0

コンパイルしないのはライブラリそのものです。 Main.cppは、デモンストレーション目的でのみあります... – Petr

1

名前空間を使用して削除std; コアコードでは、使用したいのstdのどの機能を宣言し、例:

int main() { 
using std::cout; 
using std::cin; 
using std::endl; 

//................. your code 
} 

とプログラムが正常に実行されます。

関連する問題