std :: shared_ptr..unable解析标识符 [英] std::shared_ptr..unable to resolve identifier
问题描述
我正在尝试使用c ++中的智能指针。我已经包含< memory>我的代码中的头文件,但我的代码在行上给我错误说无法解析标识符
std :: shared_ptr< person> p(新人(Scott,25));
其中Person是我在我的代码中创建的类。
请有人帮忙.. ??
Quote:std :: shared_ptr p(新人(Scott,25));
应该是
< pre lang =C ++> std :: shared_ptr< Person> p( new Person( Scott , 25 ));
不应该吗?
以下代码编译(并运行)正常(Visual C ++ Express 2010
):
#include < 内存 >
#include < iostream >
class 人
{
int age;
const char * name;
public :
Person( const char * name, int age):name(name),age(age){}
void show(){std :: cout<<名称<< <<年龄<< std :: endl;}
};
int main()
{
std :: shared_ptr< Person> p( new Person( Scott , 25 ));
p-> show();
}
std :: shared_ptr是C ++ 11的补充。 (这是在2007年发布的技术报告1中.VC ++ 2010将它包含在< memory>中,因为众所周知它会成为标准的一部分,但严格来说它应该留在tr1名称空间中。)
如果您的编译器不支持C ++ 11,您可以尝试使用:
< br />
#include< tr1 / memory>< br />
...< br />
std :: tr1 :: shared_ptr< Person& gr; p(新人(Scott,25));< br />
否则,您可以从添加库,它是在C ++ 11中包含它之前开发和测试的。
I am trying my hands on the smart pointers in c++. I have included <memory> header file in my code but my code is giving me error saying "unable to resolve identifier" on the line
std::shared_ptr<person> p(new Person("Scott", 25));
Where Person is a class that i have created in my code.
Please can anyone help..??
Quote:std::shared_ptr p(new Person("Scott", 25));
It should be
std::shared_ptr<Person> p(new Person("Scott", 25));
shouldn''t it?
The following code compiles (and runs) fine (Visual C++ Express 2010
):
#include <memory> #include <iostream> class Person { int age; const char * name; public: Person(const char * name, int age):name(name), age(age){} void show(){std::cout << name << " " << age << std::endl;} }; int main() { std::shared_ptr<Person> p(new Person("Scott", 25)); p->show(); }
std::shared_ptr is a C++11 addition. (It was in Technical Report 1, published in 2007. VC++ 2010 included it in <memory> since it was well known it would become part of the standard, but strictly speaking it should have been left in the tr1 namespace.)
If your compiler doesn''t support C++11, you could try to use:
<br /> #include <tr1/memory><br /> ...<br /> std::tr1::shared_ptr<Person&gr; p(new Person("Scott", 25));<br />
Otherwise, you can get it from the Boost libraries, which is where it was developed and tested before it''s inclusion in C++11.
这篇关于std :: shared_ptr..unable解析标识符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!