簡體   English   中英

錯誤:未在此范圍內聲明to_string

[英]error : to_string was not declared in this scope

我正在編譯solaris 5.11上的代碼。 G ++版本是4.8.2。

相同的代碼可在Ubuntu上使用,但會出現錯誤:在solaris上未在此范圍內聲明“ to_string()”。

我瀏覽了許多鏈接並嘗試了以下操作:

  1. 在to_string()之前添加“ std ::”。 這給出了錯誤-'to_string不是std的成員'
  2. 編譯時添加了“ std = c ++ 11”或“ std = c ++ 0x”。

以上兩種方法均無效。

是否與Solaris有關?

實際的代碼非常龐大。 因此,在下面的示例代碼中模擬錯誤。

temp.cpp

#include<iostream>
#include<string>

using namespace std;

int main()
{
    string str;
    int i = 10;
    str = "john age is " + to_string(i);
    cout << str;
    return 0;
}

命令:g ++ temp.cpp -std = c ++ 0x -o temp

對於GCC 4.8.2, to_string函數根據以下條件有條件地定義:

#if ((__cplusplus >= 201103L) && defined(_GLIBCXX_USE_C99) \
     && !defined(_GLIBCXX_HAVE_BROKEN_VSWPRINTF))

GLIBCXX_USE_C99宏取決於操作系統支持的大量C99函數,因此,大概是在Solaris上構建GCC時未找到必需的C99庫函數。 因此,沒有to_string定義。

在當前版本的GCC中,條件更為細化,並檢查C99函數是否在C ++ 98模式和C ++ 11中定義,以便缺少任何C99函數不會禁用所有功能:

#if __cplusplus >= 201103L
//...
#if _GLIBCXX_USE_C99_STDIO

無法將這些改進移植到GCC 4.8,因此您可能需要至少更新到GCC 6。

使用std = c ++ 11進行編譯,如下所示

g ++ -std = c ++ 11 filename.cc

注意:您的編譯器必須支持c ++ 11

暫無
暫無

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

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