对于下面的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;
}
Run Code Online (Sandbox Code Playgroud)
我无法理解为什么我收到错误C2039?我正在使用VS 2008编译器.
请帮忙.谢谢
你必须使用 ->
obj->func1();
Run Code Online (Sandbox Code Playgroud)
auto_ptr没有func1(),但它有operator ->()产生一个Mkt*存储在里面的指针,然后->将再次使用该指针,这将调用Mkt::func1()成员函数.