1

我正在编写一个小程序来解决 Project Euler Problem 21,当我遇到无法解释的内部编译器错误时,我正在测试我的代码的早期部分。我将不胜感激有关如何重新编写程序以避免此类错误的任何提示。

这是我的构建命令和编译器选项:

g++ -std=c++11 -O2 -Wall -o "pe_021" "pe_021.cc"

这是我得到的错误:

Internal compiler error: Error reporting routines re-entered.  
Compilation failed.

这是我的代码:

#include <array>
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

const unsigned int N = 10000; //look for amicable numbers smaller than N
vector<unsigned int> list_of_primes;

vector<unsigned int[2]> prime_factorize(unsigned int);

int main(int argc, char **argv)
{
    //import a list of primes
    ifstream ifs ("primes_10000.txt");
    for (unsigned int index = 0; index < N; index++) {
        string prime_number;
        getline(ifs, prime_number);
        list_of_primes.push_back(stoi(prime_number));
    }
    ifs.close();

    //test prime factorization function by prime factorizing 12
    vector<unsigned int[2]> prime_factorization = prime_factorize(12);
    for(unsigned int (&prime_and_exponent)[2] : prime_factorization) {
        cout << prime_and_exponent[0] << ", " << prime_and_exponent[1] << endl;
    }

    return 0;
}

vector<unsigned int[2]> prime_factorize(unsigned int number)
{
    vector<unsigned int[2]> prime_factorization;
    for(unsigned int index = 1; index < list_of_primes.size(); index++) {
        if(number % list_of_primes[index] == 0) {
            unsigned int prime_and_exponent[2] = {list_of_primes[index], 1};
            prime_factorization.push_back(prime_and_exponent);
            number /= list_of_primes[index];

            while(number % list_of_primes[index] == 0) {
                prime_factorization.back()[1]++;
                number /= list_of_primes[index];
            }
        }
        if(number == 1) {
            break;
        }
    }
    return prime_factorization;
}
4

2 回答 2

3

错误是由线路引起的

  prime_factorization.push_back(prime_and_exponent);

至少在 GCC 4.7.3 中,将数组放入向量似乎会导致崩溃。首先,将错误报告发送给 GCC 开发人员。其次,将您的unsigned int[2]课程包装起来,我认为这应该可行。

UPD:正如评论中指出的那样,将数组放入向量中是非法的。

于 2013-09-01T20:32:28.140 回答
0

用替换了 的unsigned int[2]出现pair<unsigned int,int unsigned>。你会快乐很多。

于 2013-09-01T22:33:10.800 回答