3

以下の C++ コードでは、コンパイラ エラーが発生します。

class Mkt
{
    int k;
public:
    Mkt(int n): k(n)
    {
        throw;
    }
    ~Mkt()
    {
        cout<<"\n\nINSIDE Mkt DTOR function:\t"<<endl;
    }
    void func1()
    {
        cout<<"\n\nINSIDE FUNC1 function....value of k is:\t"<<k<<endl;
    }
};

int main(int argc, char* argv[] )
{
    try
    {
        std::auto_ptr<Mkt> obj(new Mkt(10)); //no implicit conversion
            obj.func1(); //error C2039: 'func1' : is not a member of 'std::auto_ptr<_Ty>'
    }
    catch(...)
    {
        cout<<"\n\nINSIDE EXCEPTION HANDLER..........."<<endl;
    }
return 0;
}

エラー C2039 が表示される理由がわかりません。VS 2008 コンパイラを使用しています。

助けてください。ありがとう

4

4 に答える 4

6

つまりauto_ptr、ポインタです:)。使用する必要がありますoperator->:

obj->func1();
于 2011-03-30T09:01:36.817 に答える
5

使用する必要があります->

obj->func1();

auto_ptrはありませんがfunc1()、内部に格納されoperator ->()たポインターを生成し、そのポインターで再度使用され、メンバー関数が呼び出されます。Mkt*->Mkt::func1()

于 2011-03-30T09:01:54.403 に答える
2

Be aware that after you fix the compilation problem (change dot-operator into -> operator) you will encounter a huge run-time problem.

Mkt(int n): k(n)
{
    throw;
}

throw without an argument is meant to be used inside catch-blocks and causes re-throwing handled exception. Called outside catch-blocks will result in a call to abort function and your program termination. You probably meant something like

throw std::exception();

or, better,

throw AnExceptionDefinedByYou();
于 2011-03-30T09:25:59.797 に答える
1

これは C++ の非常に基本的なことです。auto_ptr - 「ptr」は「ポインター」を表します。

于 2011-04-19T19:19:01.883 に答える