3

For the below C++ code, I am getting an compiler error:

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;
}

I am not able to understand why I am getting the error C2039? I am using VS 2008 compiler.

Pls help. Thanks

XMarshall
  • 953
  • 3
  • 11
  • 23
  • Why the comment about implicit conversion? You're not requesting an implicit conversion. – Lightness Races in Orbit Mar 30 '11 at 09:15
  • @Kiril-Kirov also, when I am changing my code by introducing Mkt* for auto_ptr as: 'std::auto_ptr obj(new Mkt(10)); obj->func1();' I am again getting the previous error. I tried to make the function call as: 'obj.func1(); ' but still getting the same error. I am not able to understand this – XMarshall Mar 30 '11 at 09:16
  • @Tomalak-Geretkal yes I was trying to remind myself of the syntax that I am not requesting implicit conversion. That's why the comment (-: – XMarshall Mar 30 '11 at 09:18

4 Answers4

6

It is auto_ptr, this means, that it is pointer :). You must use operator->:

obj->func1();
Kiril Kirov
  • 37,467
  • 22
  • 115
  • 187
5

You have to use ->

obj->func1();

auto_ptr doesn't have func1(), but it has operator ->() that will yield a Mkt* pointer stored inside and then -> will be used again on that pointer and this will call the Mkt::func1() member function.

sharptooth
  • 167,383
  • 100
  • 513
  • 979
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();
Tadeusz Kopec for Ukraine
  • 12,283
  • 6
  • 56
  • 83
1

This is very basic thing in c++ .. auto_ptr - the "ptr" stands for "pointer",

Pooh
  • 21
  • 3