Re: [STL ] 請問List
※ 引述《tyc5116 (累人啊....)》之銘言:
: : 推 Ebergies:就是你看到的意思 07/13 15:11
: : → tyc5116:不懂... 07/13 15:22
: : 推 Ebergies:把 n 換成 75, 就這樣... 07/13 15:28
: : → iamivers0n:Non-type template parameter 07/13 15:30
: : → tyc5116:不是很懂....我看我先研究個幾天好了....@@ 07/13 16:04
: : → iamivers0n:就如同你所看到的 樣板參數是一個整數 07/13 18:24
: : → Cloud:你可以先不管...就先照你認為的寫看看..之後就懂了..QQ 07/13 18:54
: 不好意思,複習了一下,我還是把我的一些問題提出來問問好了....@@
: 先列出這個網頁所提供的範例
: template<int n>
: class CComp{
: public:
: bool operator()(const C& lhs)
: {
: return (lhs.v2==n);
: }
: };
: 1.以他最後所寫出的find_if(cv.begin(),cv.end(),CComp<75>());來看
: 我有用到這一種operator嗎...??
: 以及像Cloud大所說的,配合我的結構,我把他改過了,先貼出我原本的架構
: class TP_Gra{//TP.h
: public:
: TP_Gra(int vID,Point vOrigin_Position);
: TP_Gra();
: ~TP_Gra();
: void Set_ID(int vID){ID=vID;}
: int Get_ID(){return ID;}
: void Set_Coodinate(Point Position);
: Point Get_Coodinate(){return Origin_Position;}
: private:
: int ID;
: Point Origin_Position;
: };
: class Truckload_TP_Gra{//Truckload.h
: typedef list<TP_Gra> Contents;
: public:
: Truckload_TP_Gra();
: Truckload_TP_Gra(TP_Gra one_TP);
: void add_TP(TP_Gra new_TP);
: int Get_First_Ob_ID();
: int Get_Last_Ob_ID();
: Point Get_First_Ob_Coodinate();
: Point Get_Last_Ob_Coodinate();
: private:
: Contents Load;
: };
//這裡就用不到template ...template<int n> class Find_Ob_by_ID{//fun.h
class Find_Ob_by_ID
{
public:
Find_Ob_by_ID (int val):_value(val) // Ctor
{}
bool operator()(TP_Gra& lhs)
{
return (lhs.ID == _value)
// 應該沒有v2的 member吧...return (lhs.v2==n);
}
private:
int _value;
};
接下來...
list<TP_Gra>::iterator cviter=
find_if( Load.begin(),
Load.end(),
Find_Ob_by_ID(100)); // 可以當參數傳入
至於你已經把 Load 設為 private,把 Load 的 iterator暴露出來不是很好
所以以上的 fine_if包在一個member function,比較好..
只要回傳結果就行了~~
: template<int n> class Find_Ob_by_ID{//fun.h
: public:
: bool operator()(TP_Gra& lhs)
: {
: return (lhs.v2==n);
: }
: };
: 然後我在主程式那寫了這樣的程式碼
: list<TP_Gra>::iterator cviter=
: find_if(Token_Place.Get_First_Ob_ID(),
: Token_Place.Get_Last_Ob_ID(),
: Find_Ob_by_ID<pb->Name>());
: (上面全部打成同一行)
: 然後出現了這樣的錯誤訊息
: error C2975: 'n' : 對 'Find_Ob_by_ID' 無效的樣板引數,必須是編譯時期常數運算式
: error C2440: '正在初始化' : 無法由 'int' 轉換為
: 'std::list<_Ty>::_Iterator<_Secure_validation>'
: 請問....我該怎麼解決呢?謝謝
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 218.174.32.89
※ 編輯: Cloud 來自: 218.174.32.89 (07/15 18:56)
※ 編輯: Cloud 來自: 218.174.32.89 (07/15 18:57)
推
07/15 23:12, , 1F
07/15 23:12, 1F
討論串 (同標題文章)
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章