Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ Cygwin netbeans
I am trying my hands on the smart pointers in c++. I have included header file in my code but my code is giving me error saying "unable to resolve identifier" on the line
 
std::shared_ptr 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 3:22am
AsthaS453
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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();
}
  Permalink  
v2
Comments
AsthaS at 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 at 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 at 11-Apr-13 5:19am
   
have included #include< memory >. Is there some other header which i need to include for shared_ptr.??
AsthaS at 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 at 11-Apr-13 5:40am
   
Could you please try the little code I posted?
AsthaS at 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 at 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
good
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:

#include <tr1/memory>
...
std::tr1::shared_ptr<Person&gr; p(new Person("Scott", 25));

Otherwise, you can get it from the Boost libraries, which is where it was developed and tested before it's inclusion in C++11.
  Permalink  
Comments
AsthaS at 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
0 OriginalGriff 5,170
1 DamithSL 4,357
2 Maciej Los 3,750
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,851


Advertise | Privacy | Mobile
Web01 | 2.8.141216.1 | Last Updated 11 Apr 2013
Copyright © CodeProject, 1999-2014
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