简体   繁体   中英

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;

}

Errors:

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 is in the cstring header, that should be included.

std::isalnum is also in the locale header and std::ptr_fun cannot choose one, that you need. You should specify it manually like

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

or cast std::isalnum to needed signature

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

For the strcpy problem, use eg std::copy instead, or include <cstring> which contains the prototype for strcpy .

Not that you really need that temporary buff variable as you can use eg buffer[count++] as well.

The strcpy error is obvious -- just #include <cstring>

For the ptr_fun() error, my guess is that your using namespace std is causing it to try to use one of the templated versions of std::isalnum from the <locale> header. Simply changing the call to

not1(ptr_fun(::isalnum))

makes it compile happily with both g++ and clang on my system.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM