c++ g++3.4.5 g++4.8.2 由编译器引起的编译异常
#include <memory> #include <string> #include <iostream> class Student { public: Student(std::string& name) { _name = name; } ~Student(){ } void printName(){ std::cout << _name << std::endl; } private: std::string _name; }; int main() { std::string name = "xiaowang"; std::unique_ptr<Student> p(new Student(name)); //Student p = Student(name); //p.printName(); p->printName(); return 0; } 编译:默认g++3.4.5 报如下错误,unique_ptr是c++11的, g++3.4.5似乎也不支持这个。 main.cpp: In function 'int main()': main.cpp:41:5: error: 'unique_ptr' is not a member of 'std' std::unique_ptr<Student> p(new Student(name)); ^ main.cpp:41:28: error: expected primary-expression before '>' token std::unique_ptr<Student> p(new Student(name)); ^ main.cpp:41:49: error: 'p' was not declared in this scope std::unique_ptr<Student> p(new Student(name));
后经测试MM提示,改为用
/opt/compiler/gcc-4.8.2/bin/g++ -std=c++11 main.cpp (原来系统还藏了个高版本的,郁闷,干嘛不直接用高版本的啊)
编译无误。
^