0

クラスBの友達として関数を宣言しているのに、完全に特殊化された関数でクラスBのオブジェクトをインスタンス化できない理由がわかりません。助けてください。それがばかげているかどうかはわかりません。しかし、私は初めてC++テンプレートを学習しています。次のエラーが発生します。

1>c:\users\google drive\learnopencv\learningc\templateexample.cpp(12): error C2065: 'B' : undeclared identifier
1>c:\users\google drive\learnopencv\learningc\templateexample.cpp(12): error C2062: type 'int' unexpected
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:00.37
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========




#include "stdafx.h"
    using namespace std;
    template<typename V>
    void DoSomething(V v) //Generic Function
    {
        B<char> s;
        s.data=1;
    };
    template<>
    void DoSomething<int>(int cv) //Specialized Function
    {
        B<int> b1l; // not able to instantiate an object of class B
    };
    template<typename T>                  //Template class B
    class B
    {
        int data;
        template<class X1>
        friend void DoSomething<X1>(X1);
    };

    int main(int argc,char *argv[])
    {
        int x=12;
        DoSomething(x);

        return 0;
    }
4

1 に答える 1

2

定義するとき

template<typename V>
void DoSomething(V v) //Generic Function
{
    B<char> s;
    s.data=1;
};

Bはまだ定義されていないため、エラーが発生します。一部の並べ替えで修正できないものはありません。

using namespace std;
template<typename T>                  //Template class B
class B
{
    int data;
    template<class X1>
    friend void DoSomething(X1);
};
template<typename V>
void DoSomething(V v) //Generic Function
{
    B<char> s;
    s.data=1;
};
template<>
void DoSomething<int>(int cv) //Specialized Function
{
    B<int> b1l; // not able to instantiate an object of class B
};
于 2012-09-10T11:14:31.763 に答える