为什么在Visual Studio 2012中编译但不是UNIX?

时间:2013-10-28 08:25:53

标签: c++ c++11

#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;

}

错误:

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

3 个答案:

答案 0 :(得分: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)))

答案 1 :(得分:0)

对于strcpy问题,请使用例如而是std::copy,或包含<cstring>,其中包含strcpy的原型。

并非您真正需要该临时buff变量,因为您可以使用,例如buffer[count++]也是如此。

答案 2 :(得分:0)

strcpy错误显而易见 - 仅#include <cstring>

对于ptr_fun()错误,我的猜测是using namespace std导致它尝试使用std::isalnum标题中<locale>的一个模板化版本。只需将呼叫更改为

即可
not1(ptr_fun(::isalnum))

使我的系统上的g ++和clang都很愉快地编译。

相关问题