2014-05-11 2 views
1

사람들이 내 문제에 대해 Google 검색을 수행하기 시작하기 전에 나는 꽤 오랫동안 노력했다고 말해 보겠습니다.C++에서 참조로 객체 전달하기

C++에서 참조로 객체를 전달하는 방법을 알 수 없지만 컴파일러 오류가 계속 발생합니다. 그들이 도움이된다면 그들을 포함시킬 수 있습니다. 특히, 스레드에서 함수 parseFeed에 대한 참조로 RSSFeed 개체 피드를 전달하려고합니다.

void parseFeed(RSSFeed& feed) { 
    //dostuff 
} 

RSSFeed feed(); 
thread(parseFeed, feed); // line 119 

오류 :

g++ -g -Wall -pedantic -O0 -std=c++0x -D_GLIBCXX_USE_NANOSLEEP -D_GLIBCXX_USE_SCHED_YIELD -I/usr/class/cs110/include/libxml2 -I/usr/class/cs110/local/include -c -o news-aggregator.o news-aggregator.cc 
In file included from /usr/include/c++/4.6/functional:56:0, 
       from /usr/include/c++/4.6/bits/stl_algo.h:68, 
       from /usr/include/c++/4.6/algorithm:63, 
       from news-aggregator.cc:14: 
rss-feed.h: In constructor âconstexpr std::_Head_base<_Idx, _Head, false>::_Head_base(const _Head&) [with long unsigned int _Idx = 0ul, _Head = RSSFeed]â: 
/usr/include/c++/4.6/tuple:162:44: instantiated from âconstexpr std::_Tuple_impl<_Idx, _Head, _Tail ...>::_Tuple_impl(const _Head&, const _Tail& ...) [with long unsigned int _Idx = 0ul, _Head = RSSFeed, _Tail = {}]â 
/usr/include/c++/4.6/tuple:423:24: instantiated from âconstexpr std::tuple<_T1>::tuple(const _T1&) [with _T1 = RSSFeed]â 
/usr/include/c++/4.6/functional:1362:70: instantiated from âstd::_Bind_result<_Result, _Functor(_Bound_args ...)>::_Bind_result(_Functor&&, _Args&& ...) [with _Args = {RSSFeed&}, _Result = void, _Functor = void (*)(RSSFeed&), _Bound_args = {RSSFeed}]â 
/usr/include/c++/4.6/functional:1477:41: instantiated from âtypename std::_Bindres_helper<_Result, _Functor, _ArgTypes>::type std::bind(_Functor&&, _ArgTypes&& ...) [with _Result = void, _Functor = void (&)(RSSFeed&), _ArgTypes = {RSSFeed&}, typename std::_Bindres_helper<_Result, _Functor, _ArgTypes>::type = std::_Bind_result<void, void (*(RSSFeed))(RSSFeed&)>]â 
/usr/include/c++/4.6/thread:135:9: instantiated from âstd::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (&)(RSSFeed&), _Args = {RSSFeed&}]â 
news-aggregator.cc:119:25: instantiated from here 
rss-feed.h:54:3: error: âRSSFeed::RSSFeed(const RSSFeed&)â is private 
/usr/include/c++/4.6/tuple:97:25: error: within this context 
/usr/include/c++/4.6/tuple:97:25: error: use of deleted function âRSSFeed::RSSFeed(const RSSFeed&)â 
rss-feed.h:54:3: error: declared here 
rss-feed.h: In constructor âstd::_Head_base<_Idx, _Head, false>::_Head_base(_UHead&&) [with _UHead = RSSFeed, long unsigned int _Idx = 0ul, _Head = RSSFeed]â: 
/usr/include/c++/4.6/tuple:174:43: instantiated from âstd::_Tuple_impl<_Idx, _Head, _Tail ...>::_Tuple_impl(std::_Tuple_impl<_Idx, _Head, _Tail ...>&&) [with long unsigned int _Idx = 0ul, _Head = RSSFeed, _Tail = {}, std::_Tuple_impl<_Idx, _Head, _Tail ...> = std::_Tuple_impl<0ul, RSSFeed>]â 
/usr/include/c++/4.6/tuple:434:51: instantiated from âstd::tuple<_T1>::tuple(std::tuple<_T1>&&) [with _T1 = RSSFeed, std::tuple<_T1> = std::tuple<RSSFeed>]â 
/usr/include/c++/4.6/functional:1368:78: instantiated from âstd::_Bind_result<_Result, _Functor(_Bound_args ...)>::_Bind_result(std::_Bind_result<_Result, _Functor(_Bound_args ...)>&&) [with _Result = void, _Functor = void (*)(RSSFeed&), _Bound_args = {RSSFeed}, std::_Bind_result<_Result, _Functor(_Bound_args ...)> = std::_Bind_result<void, void (*(RSSFeed))(RSSFeed&)>]â 
/usr/include/c++/4.6/functional:1477:41: instantiated from âtypename std::_Bindres_helper<_Result, _Functor, _ArgTypes>::type std::bind(_Functor&&, _ArgTypes&& ...) [with _Result = void, _Functor = void (&)(RSSFeed&), _ArgTypes = {RSSFeed&}, typename std::_Bindres_helper<_Result, _Functor, _ArgTypes>::type = std::_Bind_result<void, void (*(RSSFeed))(RSSFeed&)>]â 
/usr/include/c++/4.6/thread:135:9: instantiated from âstd::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (&)(RSSFeed&), _Args = {RSSFeed&}]â 
news-aggregator.cc:119:25: instantiated from here 
rss-feed.h:54:3: error: âRSSFeed::RSSFeed(const RSSFeed&)â is private 
/usr/include/c++/4.6/tuple:101:42: error: within this context 
/usr/include/c++/4.6/tuple:101:42: error: use of deleted function âRSSFeed::RSSFeed(const RSSFeed&)â 
rss-feed.h:54:3: error: declared here 

해결 : 스레드 (parseFeed, REF ( 피드 )); // 라인 119

+0

@alecb 어떻게 그렇게 ?? – 0x499602D2

답변

6

RSSFeed feed(); 

타입 RSSFeed의 객체를 반환하고 매개 변수가없는 함수 선언입니다. 그것은 객체 정의가 아닙니다.

쓰기 대신

RSSFeed feed; 

또한 클래스 RSSFeed 삭제로 정의된다 더 복사 생성자가 없습니다 오류 메시지 목록에서 보인다.

+1

이 문제는 [여기] (http://en.wikipedia.org/wiki/Most_vexing_parse) –

+0

/** * 생성자 : RSSFeed * 사용 : RSS 피드 ("http : /feeds.washingtonpost.com/news/world.rss "); * ------------------------------------------------ ---------------------- * 제공된 URL을 중심으로 RSSFeed 객체를 구성합니다. */ RSSFeed (const std :: string & url) : url (url) {} 저는 이것이 문제라고 생각하지 않지만 생성자의 인수를 생략했지만 피드를 변수로 사용하면 코드가 올바르게 작동합니다. – fool

+2

@MaxBozzi 아니, 그것은 단지 짜증나는 분석이다. * 대부분의 애매한 구문 분석은'X x (X)'가됩니다. – 0x499602D2