自定義C trim函式出現的小問題

2021-07-31 06:32:12 字數 2483 閱讀 8865

在位址

中,關於trim函式的方法3:gcc編譯器不能通過問題。

這是該**:

/* 

filename : stringtrim1.cpp

compiler : visual c++ 8.0

description : demo how to trim string by other method.

release : 11/18/2006

*/#include #include #include template std::basic_string& trim(std::basic_string&);

int main( )

template std::basic_string& trim(std::basic_string& s)

std::basic_string::iterator c;

// erase whitespace before the string

for (c = s.begin(); c != s.end() && iswspace(*c++);); s.erase(s.begin(), --c);

// erase whitespace after the string

for (c = s.end(); c != s.begin() && iswspace(*--c);); s.erase(++c, s.end());

return s;

}

以下是我在gcc編譯的結果:   ps:我用的是trim.cpp

[root@localhost testconfig]# g++ trim.cpp

trim.cpp: in function 『std::basic_string<_chart, std::char_traits<_chart>, std::allocator<_chart> >& trim(std::basic_string<_chart, std::char_traits<_chart>, std::allocator<_chart> >&)』:

trim.cpp:21: error: expected 『;』 before 『c』

trim.cpp:23: error: 『c』 was not declared in this scope

trim.cpp: in function 『std::basic_string<_chart, std::char_traits<_chart>, std::allocator<_chart> >& trim(std::basic_string<_chart, std::char_traits<_chart>, std::allocator<_chart> >&) [with t = char]』:

trim.cpp:11:   instantiated from here

trim.cpp:17: error: could not convert 『s->std::basic_string<_chart, _traits, _alloc>::empty [with _chart = char, _traits = std::char_traits, _alloc = std::allocator]』 to 『bool』

trim.cpp:21: error: dependent-name 『std::basic_string::iterator』 is parsed as a non-type, but instantiation yields a type

trim.cpp:21: note: say 『typename std::basic_string::iterator』 if a type is meant

這裡報的是「變數c之前丟失分號」的錯誤,這其實是個誤導性的提示,重要的提示是我劃線的這兩句。一開始很迷惑,看了這個博文

後,明白是歧義造成的。gcc編譯器並不清楚std::basic_string::iterator是一種型別還是std::basic的成員,除非顯示宣告其為乙個型別,因此直接加上typename關鍵字就行了。下面是更正後的**。

trim.cpp

#include #include template std::basic_string& trim(std::basic_string& s);

int main(int argc, char* )

template std::basic_string& trim(std::basic_string& s)

typename std::basic_string::iterator c;//c;

for (c = s.begin(); c != s.end() && iswspace(*c++); );

s.erase(s.begin(), --c);

for (c = s.end(); c != s.begin() && iswspace(*--c); );

s.erase(++c, s.end());

return s;

}

自定義的列印函式

void userdefinedprintfun char format,int i,else if format 1 format d else if format 1 format f va end arg ptr return 如果採用下面三種方法呼叫,合法合理 userdefinedprin...

oracle自定義的函式

今天在資料庫裡做資料運算時 資料都為0以上 發現報無效數字,之前也是經常遇到這種錯誤,一直沒有做過整理 解決辦法 1.查出非資料的字段,但是沒查到 select from 表 where replace 表字段,0123456789.is null 2.將空格清除掉 換行,製表符,空格 update...

hive的自定義函式

程式設計步驟 1 繼承org.apache.hadoop.hive.ql.udf 2 需要實現evaluate函式 evaluate函式支援過載 3 在hive的命令列視窗建立函式 add jar linux jar path 建立function create temporary function...