簡體   English   中英

為什么會在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: 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::strcpycstring標頭中,應該包含在內。

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進行編譯。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM