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

3
  • Why the comment about implicit conversion? You're not requesting an implicit conversion. Commented Mar 30, 2011 at 9:15
  • @Kiril-Kirov also, when I am changing my code by introducing Mkt* for auto_ptr as: 'std::auto_ptr<Mkt*> 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 Commented Mar 30, 2011 at 9: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 (-: Commented Mar 30, 2011 at 9:18

4 Answers 4

6

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

obj->func1();
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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();

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.