6

コードを C++ 11 にコンパイルして srand48 関数を使用できないのはなぜですか?

私はいくつかの行列をいじるプログラムを持っています。-std=c++0x問題は、フラグを使用してコードをコンパイルするときです。私はいくつかのc ++ 11のみの関数を使用したいのですが、これが私のアプローチです。C++ のバージョンを指定しなくても問題なくコンパイルできます。このような:

g++ -O2 -Wall test.cpp -o test -g

上記のフラグの機能を誤解している場合は、修正してください。

Windows 7 64 ビット マシンでコードを実行し、cygwin でコンパイルします。g++ バージョン 4.5.3 (GCC) を使用しています。さらに情報が必要な場合はコメントしてください。

なんらかの理由で (自分にとっても)、すべてのコードが 1 つのコンパイル ユニットで記述されます。エラーの原因が構造上のエラーである場合は、遠慮なく指摘してください。:)

次のエラーが表示されます。

g++ -std=c++0x -O2 -Wall test.cpp -o test -g

test.cpp: In function ‘void gen_mat(T*, size_t)’:
test.cpp:28:16: error: there are no arguments to ‘srand48’ that depend on a template parameter, so a declaration of ‘srand48’ must be available
test.cpp:28:16: note: (if you use ‘-fpermissive’, G++ will accept your code, but allowing the use of an undeclared name is deprecated)
test.cpp:33:28: error: there are no arguments to ‘drand48’ that depend on a template parameter, so a declaration of ‘drand48’ must be available

これが私のコードのサブです。上記のエラーが生成されます。

#include <iostream>
#include <cstdlib>
#include <cassert>
#include <cstring>
#include <limits.h>
#include <math.h>

#define RANGE(S) (S)

// Precision for checking identity.
#define PRECISION 1e-10

using namespace std;

template <typename T> 
void gen_mat(T *a, size_t dim)
{
    srand48(dim);
    for(size_t i = 0; i < dim; ++i)
    {
        for(size_t j = 0; j < dim; ++j)
        {
            T z = (drand48() - 0.5)*RANGE(dim);
            a[i*dim+j] = (z < 10*PRECISION && z > -10*PRECISION) ? 0.0 : z;
        }
    }
}

int main(int argc, char *argv[])
{

}

よろしくキム。

これは私にとって問題を解決した解決です:

最初の nm は、 でコンパイルする場合、srand() は使用できないと説明しました-std=c++0x。ただし、使用する正しいフラグ-std=gnu++11は g++ バージョン 4.7+ が必要です。したがって、私にとっての解決策は、-std=gnu++0x コンパイル コマンド =を使用してコードをコンパイルすることでした。g++ -O2 -Wall test.cpp -o test -g -std=gnu++0x

4

1 に答える 1

6

明示的に設定-stc=c++03すると、同じエラーが発生します。これはdrand48、フレンドが実際には C++ 標準の一部ではないためです。gccこれらの機能を拡張機能として組み込み、標準の動作が必要な場合は無効にします。

のデフォルトの標準モードは、g++実際には-std=gnu++03です。-std=gnu++11の代わりに使用するか、コンパイラに-std=c++0x渡し ます。-U__STRICT_ANSI__

于 2013-05-16T09:04:04.670 に答える