2012-11-12 53 views
0

編譯時,它會出現一個錯誤:編譯libfacebookcpp,「StringBuilder的」未聲明

 
PagingInfo.hpp:35: error: ‘StringBuilder’ was not declared in this scope. 

我已經inlude正確的頭文件,但爲什麼編譯器無法找到StringBuilder的difinition?

Utils.hpp:

#ifndef LIBFACEBOOKCPP_UTILS_H_ 
#define LIBFACEBOOKCPP_UTILS_H_ 

template<class TData, class TStr> 
inline TData fromString(const TStr &str) 
{ 
std::stringstream oss; 
oss << str; 
TData t; 
oss >> t; 
return t; 
} 

class StringBuilder 
{ 
public: 
inline operator const std::string() const 
{ 
return oss.str(); 
} 

private: 
std::ostringstream oss; 
}; 

#endif // LIBFACEBOOKCPP_UTILS_H_ 

PagingInfo.hpp

#ifndef LIBFACEBOOKCPP_PAGING_INFO_H_ 
#define LIBFACEBOOKCPP_PAGING_INFO_H_ 
#include "Utils.hpp" 
namespace LibFacebookCpp 
{ 

struct PagingInfo 
{ 
PagingInfo(unsigned int offset_, unsigned int limit_) : offset(offset_), limit(limit_) { } 

bool IsValid() const { return 0 != limit; } 
void GetUri(Uri *uri) const 
{ 
LIBFACEBOOKCPP_ASSERT(uri); 
uri->query_params["limit"] = StringBuilder() << offset; 
uri->query_params["offset"] = StringBuilder() << limit; 
} 
... 
}; 

} // namespace LibFacebookCpp 

#endif // LIBFACEBOOKCPP_PAGING_INFO_H_ 
+0

沒有壓痕?請告訴我你的代碼實際上並不像這樣。另外,爲什麼你需要一個stringstream包裝? – Wug

回答

1

當我補充足夠的骨架代碼得到這個下降到只有你在ideone的問題,我得到一個不同的錯誤:

prog.cpp: error: no match for 'operator<<' in 'StringBuilder() << ((const LibFacebookCpp::PagingInfo*)this)->LibFacebookCpp::PagingInfo::offset'

您的StringBuilder類沒有< <運算符定義。爲了使用:

StringBuilder() << offset; 

您將需要定義一個。

在你和我之間,對於stringstream(每個基本類型都有一個),該操作符有15個重載。重新實現它們將是一個巨大的浪費時間。只需使用stringstream即可。

+0

哦,對不起,我忽略了代碼的功能太長了。謝謝。我在Mac x86_64上編譯libfacebookcpp。所以很多錯誤。 – kuangi

+0

請包括它。並認真地重新考慮使用stringbuilder包裝。 – Wug

相關問題