Click here to Skip to main content
13,091,145 members (62,506 online)
Rate this:
Please Sign up or sign in to vote.
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 10-Apr-13 2:22am
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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;
  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));
AsthaS 11-Apr-13 0:50am 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?
AsthaS 11-Apr-13 5:45am
I have tried the code. But the same error--Unable to resolve identifier shared_ptr-- But it works fine if i change shared_ptr with auto_ptr.. :(
CPallini 11-Apr-13 6:11am
Then as Orjan Westin suggests, you have an old compiler. Either try his solution or update the compiler.
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

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.
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.170813.1 | Last Updated 11 Apr 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100