2016-11-03 15 views
0

나는 간단한 클래스를 가지고있다.컴파일러가 make -shared()가 l- 값을 예상 함

simple_class.h 맥북에서 실행

class SimpleClass { 
    private: 
     int d_ii; 
     int d_jj; 
    public: 
     SimpleClass() : d_ii(40), d_jj(10) {} 
     SimpleClass(const int ii, const int jj) : d_ii(ii), d_jj(jj) {} 
}; 

test.t.cpp

//----------------------------------------------------------------// 
// 
// Test Program 
#include <memory> 
#include <simple_class.h> 

int main (int argc, char * argv[]) 
{ 
    SimpleClass sc1; 
    SimpleClass sc2(10, 20); 

    std::shared_ptr<SimpleClass> spSc1(new SimpleClass(10,12)); 

    int ii = 10; 
    int jj = 16; 
    std::shared_ptr<SimpleClass> spSc2 = std::make_shared<SimpleClass> (ii, jj); 

    std::shared_ptr<SimpleClass> spSc3 = std::make_shared<SimpleClass> (10, 16); 

    return 0; 
} 

: 그 생성자 중 하나는 인수로 두 int 값을 사용합니다.

gcc -o test -I. test.t.cpp -lstdc++

그러나이 오류가 발생합니다 : 여기 내 컴파일 문은

test.t.cpp:18:41: error: no matching function for call to 'make_shared' 
std::shared_ptr<SimpleClass> spSc3 = std::make_shared<SimpleClass> (10, 16); 
             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:4708:1: note: 
     candidate function [with _Tp = SimpleClass, _A0 = int, _A1 = int] not viable: expects an l-value for 1st 
     argument 
make_shared(_A0& __a0, _A1& __a1) 
^ 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:4692:1: note: 
     candidate function template not viable: requires 0 arguments, but 2 were provided 
make_shared() 
^ 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:4700:1: note: 
     candidate function template not viable: requires single argument '__a0', but 2 arguments were provided 
make_shared(_A0& __a0) 
^ 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:4716:1: note: 
     candidate function template not viable: requires 3 arguments, but 2 were provided 
make_shared(_A0& __a0, _A1& __a1, _A2& __a2) 
^ 
1 error generated. 

그래서 제 질문은, 왜하지 않습니다 make_shared() 작업이 사용? 이 이유

int ii = 10; 
    int jj = 16; 
    std::shared_ptr<SimpleClass> spSc2 = std::make_shared<SimpleClass> (ii, jj); 

사람이 설명 할 수 : L 자 값 버전이 컴파일 않습니다 통과 것을

std::shared_ptr<SimpleClass> spSc2 = std::make_shared<SimpleClass>(10, 16); 

주의 사항?

내 생성자가 인수를 const으로 선언합니다 (필수는 아니지만). examples given heremake_shared()으로 상수를 전달합니다.

+0

[재생할 수 없음] (http://coliru.stacked-crooked.com/a/d582117f25211a0d) – NathanOliver

+0

사용중인 컴파일러 버전은 무엇입니까? – NathanOliver

+0

Dino @ MacDino : ~/code/test $ gcc -v --prefix =/응용 프로그램/Xcode.app/내용/개발자/usr --with-gxx-include-dir =/usr/include/C++/4.2.1 Apple LLVM 버전 7.0.2 (clang-700.1.81) 대상 : x86_64-apple-darwin14.5.0 스레드 모델 : posix –

답변

3

C 컴파일러로 C++ 코드를 컴파일하는 경우 대신 g++을 사용하십시오. 또한 std::shared_ptr 및 친척은 G ++ 컴파일러에서 활성화해야하는 C++ 11 기능입니다. 또한 , G ++ 4.2 C++ 11 기능을 지원하지 않습니다 그래서 당신의 명령 줄 (g ++ 자동으로 수행, ++가 된 libstdc에 연결이 필요하지 않습니다)

편집 g++ -o test -I. test.t.cpp -std=c++11이 될 것입니다. OSX에서

+0

그가 C 컴파일러로 컴파일한다면 더 많은 오류가 발생할 것입니다. 'class'와'std :: shared_ptr'도 에러가됩니다. – NathanOliver

+0

그래, 문제가되는 C++ 11 지원이 부족합니다. –

+0

감사. > gcc -o test -I에 주목합니다. test.t.cpp -lstdC++ -std = C++ 11 도 작동합니다. –