Trouble using std::unique_ptr

Feb 21, 2014 at 7:53am
So being the humble human I am during my never ending endeavor to learn I found this example code in my book, promptly attempting to compile and run it I found that std::unique_ptr is in fact (according to my compiler)it is not a member of std in direct opposition to what my book said, If anyone could help me out here I would be very thankful, on a side note I'm running codeblocks on linux but the compiler is up to date. I know it should be member of memory but nothing appears to occur between the two.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
  #include <iostream>
#include <memory> // include this to use std::unique_ptr
using namespace std;

class Date
{
private:
    int Day;
    int Month;
    int Year;

    std::string DateInString;

public:
    //Constructor that initializs the objext to a day, month and year
    Date(int InputDay, int InputMonth, int InputYear)
    : Day(InputDay), Month(InputMonth), Year(InputYear) {};

    void DisplayDate()
    {
        std::cout << Day << " / " << Month << " / " << Year << std::endl;
    }
};

int main()
{
    std::unique_ptr<int> pDynamicAllocInteger(new int);
    *pDynamicAllocInteger = 42;

    //use smart pointer type like an int*
    std::cout << "Intiger value is: " << *pDynamicAllocInteger << std::endl;

    std::unique_ptr<Date> pHoliday(new Date(25, 11, 2011));
    std::cout << "The new instance of date contains: ";

    //use pHoliday just as you would a Date*
    pHoliday->DisplayDate();

    //no need to do the following when using unique_ptr:
    //delete pDynamicAlocInteger;
    //delete pHoliday;

    return 0;
}
Feb 21, 2014 at 8:46am
std::unique_ptr is part of the C++11 standard. It looks like your compiler does not support this feature of C++11. Perhaps consider using a different compiler?
Feb 21, 2014 at 8:50am
unique_ptr is part of the stdandard from the language version C++11 on.
You need to update your compiler in order to use it (your book should have mention that)
Feb 21, 2014 at 9:03am
On Linux, so you're probably using GCC.

GCC has many "up to date" versions (4.3 through to 4.8 are actively maintained), to get the C++11 capabilities you need GCC 4.7.x or GCC 4.8.x, many Linux distributions don't have either of these (Fedora 20 has GCC 4.4 for instance). The one popular distro I know that tends to track the latest greatest GCC is Ubuntu (Ubuntu 13.10 has GCC 4.8).

So, check your GCC version (gcc -v), if it's not high enough then you'll need to do one of the following:

1) find a more up to date GCC package.
2) install a different distribution.
3) compile the newer GCC yourself.

Post back if you want more advice on any of these options.
Last edited on Feb 21, 2014 at 9:04am
Feb 21, 2014 at 9:07am
You need to switch on C++ 11 features when using GCC with the flag -std=c++11.
Feb 21, 2014 at 9:22am
You need to switch on C++ 11 features when using GCC with the flag -std=c++11.


@kbw do you know how he'd do that with his CodeBlocks IDE? (I've never used it before, why is it so popular all of a sudden, has everyone abandoned Eclipse?)
Feb 21, 2014 at 9:27am
Forgot about the flag... On CodeBlocks you just go to settings -> Compiler and debugger -> Check the flag for have g++ follow the C++00 standard.

@ValliusDax - People definitely still use Eclipse for making Android apps. I'm not sure of it's popularity for other things though.

Feb 21, 2014 at 5:01pm
@Mats, Hey it worked just dandy, I knew it was up to date but for some reason C++11 wasn't selected instead 98 something was. Thanks guys for all your help ,and sorry for the slow response time.
Topic archived. No new replies allowed.