为什么这会在Visual Studio 2012中编译,而不是UNIX
Why Will This Compile in Visual Studio 2012 But Not UNIX?
#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: 在函数 'int main(int, char**)' 中:asst4.cpp:30:76:错误:调用"ptr_fun()"没有匹配函数ASST4.cpp:30:76:注意:候选人是:/usr/include/c++/4.6/bits/stl_function.h:443:5: 注意:模板 std::p ointer_to_unary_function std::p tr_fun(_Result (*)(_Arg))/usr/include/c++/4.6/bits/stl_function.h:469:5:注意:模板 std::p ointer_to_binary_function std::p tr_fun(_Result (*)(_Arg1, _Arg2))asst4.cpp:37:29:错误:"strcpy"未在此范围内声明
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)))
对于strcpy
问题,请使用例如 std::copy
,或包含包含strcpy
原型的<cstring>
。
并不是说你真的需要那个临时的buff
变量,因为你可以使用例如 buffer[count++]
也是。
strcpy
错误很明显——只是#include <cstring>
对于ptr_fun()
错误,我的猜测是您的using namespace std
导致它尝试使用<locale>
标头中std::isalnum
的模板化版本之一。只需将调用更改为
not1(ptr_fun(::isalnum))
使它在我的系统上与 G++ 和 clang 一起愉快地编译。
相关文章:
- 二叉排序树无法编译
- 编译时未启用intel oneApi CUDA支持
- 在VS代码中交叉编译Windows与Linux上的MinGW的SDL程序
- Unix 宏在使用 -std=c++0x 标志编译时变得未定义
- 双击编译C++ Unix 可执行文件不会打开现有文件以从中读取信息
- 在 Unix 中编译 c++ 时嵌套模板参数出错
- 如何在Windows中为Unix/Linux编译C++应用程序
- Unix可执行文件上的Mac分段错误,文件编译正常
- Unix 命令行在编译后无法运行程序,没有错误消息
- 为什么这会在Visual Studio 2012中编译,而不是UNIX
- 为什么这不能在我的学校 unix 系统上编译?
- 如何让我的C++程序在 Unix 上编译
- 在unix中编译dhrystone时出错
- 在Unix/Linux(Ubuntu)中编译头文件和两个.cpp文件
- 在Unix上编译所有的boost库
- 我得到编译器错误,而在unix中编译我的代码
- 如何查找UNIX的编译时间
- 从Visual Studio 2012为Windows和UNIX计算机编译项目
- 代码在Visual Studio中可以很好地编译,但在使用g++的Unix终端中就不行了.我在多维向量处得到一个错误.为
- AIX unix服务器上的c++编译错误