Click here to Skip to main content
15,888,113 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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..??
Posted

Quote:
std::shared_ptr p(new Person("Scott", 25));

It should be
C++
std::shared_ptr<Person> p(new Person("Scott", 25));

shouldn't it?


The following code compiles (and runs) fine (Visual C++ Express 2010):
C++
#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();
}
 
Share this answer
 
v2
Comments
AsthaS 11-Apr-13 0:50am    
Yess..my mistake. it should be the way u wrote it...but still the problem of undefined reference persists.. :(
Stefan_Lang 11-Apr-13 5:12am    
There are only two identifiers on that line that could be undefined: std::shared_ptr and Person. Make sure you included the appropriate header files.
AsthaS 11-Apr-13 5:19am    
have included #include< memory >. Is there some other header which i need to include for shared_ptr.??
AsthaS 11-Apr-13 5:32am    
I am using netbeans 7.3 with cygwin to run this code. Hope this is not the root of error.
CPallini 11-Apr-13 5:40am    
Could you please try the little code I posted?
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.
 
Share this answer
 
Comments
AsthaS 11-Apr-13 6:06am    
Ok thanxx...i guess u have identified the issue well. Will try it with boost library now.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900