c++ - 为什么这会在 Visual Studio 2012 而不是 UNIX 中编译?

标签 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

最佳答案

std::strcpy 位于 cstring header 中,应该包含在内。

std::isalnum 也在 locale header 中并且 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)))

关于c++ - 为什么这会在 Visual Studio 2012 而不是 UNIX 中编译?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19630033/

相关文章:

c++ - 在 C++ 中将 unique_ptr 和 shared_ptr 与函数参数一起使用

c++ - 与 openmp 的 10 维蒙特卡洛集成

c++ - WinHttp C++ 中的 POST 请求

c++ - 传递临时 std::string 时的 string_view 行为

c++ - 在 C++ 中, 'Node' 是一个类, 'node' 是 'Node' 的实例。为什么 "*node = nullptr "是错误的,而 "*node = NULL"是正确的?

c++ - 我可以在 C++ 中使用 getopt 来读取带参数和不带参数的字符吗?

独立 std::threads 的 C++ std::vector

iphone - 包含 <string> 时 <cstdio> 出错

c++ - 无法使用在模板化基类 [C++] 中声明的枚举来编译代码

python - 使用 Swig 通过 Python 包装 Eigen/C++ 时出错