2014-05-11 118 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怎麼這樣? (http://en.wikipedia.org/wiki/Most_vexing_parse) – 0x499602D2

回答

6

RSSFeed feed(); 

是返回類型的RSSFeed的目的並且沒有參數的函數聲明。它不是一個對象定義。

寫,而不是

RSSFeed feed; 

而且似乎從錯誤信息的列表類的RSSFeed沒有拷貝構造函數是刪除了它的定義。

+1

這個問題被稱爲'vexing parse',參見[這裏](http://en.wikipedia.org/wiki/Most_vexing_parse) –

+0

/** *構造函數:RSSFeed *用法:RSSFeed提要(「http:/ /feeds.washingtonpost.com/news/world.rss「); * ------------------------------------------------ ---------------------- *圍繞提供的URL構造一個RSSFeed對象。我不認爲這是問題,我省略了構造函數的參數,但是當我使用feed作爲變量時,代碼工作正常。 – fool

+2

@MaxBozzi不,這只是一個令人煩惱的解析。一個*最*煩人的解析將是'X x(X())'。 – 0x499602D2