【问题标题】:Problems compiling on an AXP server在 AXP 服务器上编译的问题
【发布时间】:2015-09-27 17:04:54
【问题描述】:

所以我试图在 AXP 服务器上编译这个 C++ 代码,但它不想编译。我要解决的问题是背包问题,我想使用文件来读取数据。 这是我目前拥有的代码:

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

    // A utility function that returns maximum of two integers
    int max(int a, int b)
    {
        return (a > b) ? a : b;
    }

    // Returns the maximum value that can be put in a knapsack of capacity W
    int knapSack(int W, int wt[], int val[], int n)
    {
        int i, w;
        int K[n + 1][W + 1];

        // Build table K[][] in bottom up manner
        for (i = 0; i <= n; i++)
        {
            for (w = 0; w <= W; w++)
            {
                if (i == 0 || w == 0)
                    K[i][w] = 0;
                else if (wt[i - 1] <= w)
                    K[i][w]= max(val[i - 1] + K[i - 1][w - wt[i - 1]], K[i - 1][w]);
                else
                    K[i][w] = K[i - 1][w];
            }
        }

        return K[n][W];
    }

    int main()
    {

        ifstream myFile;
        myFile.open("p1.txt");

        //cout << "Enter the number of items in a Knapsack:";
        int n, W;
        myFile >> n;
        int val[n], wt[n];
        //cout << "Enter the capacity of knapsack";
        myFile >> W;

        for (int i = 0; i < n; i++)
        {
            //cout << "Enter weight and value for item " << i << ":";
            myFile >> wt[i];
            myFile >> val[i];
        }

        //    int val[] = { 60, 100, 120 };
        //    int wt[] = { 10, 20, 30 };
        //    int W = 50;
        cout << "The highest valid value is: " << knapSack(W, wt, val, n) << endl;

        return 0;
    }

这是编译后给我的错误列表:

    using namespace std;
    ................^
    %CXX-E-MISNAMNAM, name must be a namespace name
    at line number 3

        int K[n + 1][W + 1];
    ..........^
    %CXX-E-EXPRNOTCONST, expression must have a constant value
    at line number 16

        int K[n + 1][W + 1];
    .................^
    %CXX-E-EXPRNOTCONST, expression must have a constant value
    at line number 16

        int val[n], wt[n];
    ............^
    %CXX-E-EXPRNOTCONST, expression must have a constant value
    at line number 45

        int val[n], wt[n];
    ...................^
    %CXX-E-EXPRNOTCONST, expression must have a constant value
    at line number 45

【问题讨论】:

  • 可变长度数组不是标准的 C++,尽管一些编译器支持它作为扩展。服务器托管的编译器不支持它们,因此您需要更改代码。不知道为什么它会在 std 命名空间上抛出错误。

标签: c++ knapsack-problem


【解决方案1】:

%CXX-E-MISNAMNAM

由于历史原因,HP C++ 默认使用 ANSI 之前的 iostreams 库 几个平台。这个 pre-ANSI iostreams 库不涉及 std 命名空间,因此当您编写 using namespace std; 时,编译器会抱怨,因为它在您包含的标头中没有提及 namespace std

更多详情,请参阅http://h71000.www7.hp.com/commercial/cplus/docs/ugv_stl.html 中的第 7.1.2 节。

如果定义了 __USE_STD_IOSTREAM 宏,HP C++ 将使用 ANSI iostream 库,它确实存在于命名空间std 中。你可以定义这个 使用/DEFINE=(__USE_STD_IOSTREAM) 调用编译器时的宏。

%CXX-E-EXPRNOTCONST

可变长度数组是 C99 标准的一部分,但不是 C++ 的一部分。一世 我不知道 HP C++ 已经实现了这个标准的扩展。你 可能需要重写您的程序以避免使用它们(也许使用 std::vector) 来实现用 HP C++ 编译。

http://h71000.www7.hp.com/commercial/cplus/docs/ugv_contents.html 包含很多 有关 HP C++ 的有用信息。

【讨论】:

    猜你喜欢
    • 2016-02-27
    • 2011-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-17
    • 1970-01-01
    相关资源
    最近更新 更多