2013-10-28 90 views
0
#include <iostream> 
#include <cstdlib> 
#include <string> 
#include <ctype.h> 
#include <cmath> 
#include <functional> 
#include <numeric> 
#include <algorithm> 

using namespace std; 

int main(int argc, char *argv[]) 
{ 

int length = 0; 

cout << "Enter a string: "; 

string buffer; 
char buff[1024]; 

while (getline(cin, buffer)) 
{ 
    buffer.erase(remove_if(buffer.begin(), buffer.end(), not1(ptr_fun(isalnum))), buffer.end()); 
    break; 
} 

length = buffer.length(); 
int squareNum = ceil(sqrt(length)); 

strcpy(buff, buffer.c_str()); 

char** block = new char*[squareNum]; 
for(int i = 0; i < squareNum; ++i) 
block[i] = new char[squareNum]; 

int count = 0 ; 

for (int i = 0 ; i < squareNum ; i++) 
{ 
    for (int j = 0 ; j < squareNum ; j++) 
    { 
     block[i][j] = buff[count++]; 
    } 
} 

for (int i = 0 ; i < squareNum ; i++) 
{ 
    for (int j = 0 ; j < squareNum ; j++) 
    { 
     cout.put(block[j][i]) ; 
    } 
} 

return 0; 

} 

錯誤:爲什麼會在Visual Studio 2012中編譯但不是UNIX?

 
asst4.cpp: In function ‘int main(int, char**)’: 
asst4.cpp:30:76: error: no matching function for call to ‘ptr_fun()’ 
asst4.cpp:30:76: note: candidates are: 
/usr/include/c++/4.6/bits/stl_function.h:443:5: note: template std::pointer_to_unary_function std::ptr_fun(_Result (*)(_Arg)) 
/usr/include/c++/4.6/bits/stl_function.h:469:5: note: template std::pointer_to_binary_function std::ptr_fun(_Result (*)(_Arg1, _Arg2)) 
asst4.cpp:37:29: error: ‘strcpy’ was not declared in this scope 
+0

大概你傳遞了這個'std = C++ 11'標誌或者其他東西。並使用最新的編譯器。 –

回答

4
std::strcpy

cstring頭,即應被包括在內。

std::isalnum也在locale標題和std::ptr_fun不能選擇一個,你需要。你應當手動指定它像

std::not1(std::ptr_fun<int, int>(std::isalnum)) 

或投std::isalnum所需簽名

std::not1(std::ptr_fun(static_cast<int(*)(int)>(std::isalnum))) 
0

對於strcpy問題,例如使用代替std::copy,或者包括<cstring>,其中包含strcpy的原型。

不是說你真的需要臨時buff變量,因爲你可以使用例如, buffer[count++]

0

strcpy錯誤是顯而易見的 - 只是#include <cstring>

對於ptr_fun()錯誤,我的猜測是,你using namespace std導致它嘗試使用std::isalnum的模板版本之一從<locale>頭。簡單地將呼叫改爲

not1(ptr_fun(::isalnum)) 

使得它可以在我的系統上與g ++和clang一起愉快地編譯。

相關問題