代码在Visual Studio中编译良好,但在使用g ++的Unix终端中编译。我在多维向量上得到一个错误。为什么?

时间:2013-01-16 16:40:09

标签: c++ visual-studio g++

代码编译,并使用Visual Studio按预期运行。我明白从不保证它会在其他地方编译/运行,但我不明白为什么在这种情况下。也许有人可以帮忙澄清一下? g ++编译器在第45行给出了错误,其中声明了RGB类型向量的向量:

#include <iostream>
#include <string>
#include <vector>
#include <fstream>

using namespace std;

/*  The following color method uses the provided formula to determine
the float value of each (i,j) coordinate passed as parameters.  */

float color (int i, int j)
{
    float col = float (((i & 0x08) == 0) ^ ((j & 0x08) == 0));

    return col;
}

int main()
{
    // The provided RGB object that stores each rgb value:
    struct RGB
    {
        float r;
        float g;
        float b;
    };

    int w;
    int h;
    string filename;
    float c; // to store the result from the color method.

    cin >> w >> h >> filename;

    // A vector of vectors to represent the 2D array:
    vector< vector<RGB> > rgb(h, vector<RGB>(w));

    for (int i = 0; i < h; i++)
        for (int j = 0; j < w; j++)
        {
            c = color(i, j);
            rgb[i][j].r = c;
            rgb[i][j].g = c;
            rgb[i][j].b = c;
        }

        ofstream ppmfile;
        ppmfile.open (filename);
        ppmfile << "P3\n" << w << " " << h << endl;
        ppmfile << "255\n";

        for (int i = 0; i < h; i++)
        {
            // The following loop uses integer multiplication to output to the ppm
            // file the rgb values converted to integers on the 0-255 scale.
            for (int j = 0; j < w; j++)
            {
                ppmfile << rgb[i][j].r * 255 << " ";
                ppmfile << rgb[i][j].g * 255 << " ";
                ppmfile << rgb[i][j].b * 255;
                if (j != (w-1))
                    ppmfile << " ";
            }
            ppmfile << endl;
        }


        return 0;
}

以下是g ++中弹出的错误的完整列表:

hw1.cxx: In function 'int main()':
hw1.cxx:45: error: template argument for 'template<class _Alloc> class std::allocator' uses local type 'main()::RGB'
hw1.cxx:45: error:   trying to instantiate 'template<class _Alloc> class std::allocator'
hw1.cxx:45: error: template argument 2 is invalid
hw1.cxx:45: error: template argument 1 is invalid
hw1.cxx:45: error: template argument 2 is invalid
hw1.cxx:45: error: invalid type in declaration before '(' token
hw1.cxx:45: error: template argument for 'template<class _Alloc> class std::allocator' uses local type 'main()::RGB'
hw1.cxx:45: error:   trying to instantiate 'template<class _Alloc> class std::allocator'
hw1.cxx:45: error: template argument 2 is invalid
hw1.cxx:45: error: initializer expression list treated as compound expression
hw1.cxx:51: error: invalid types 'int[int]' for array subscript
hw1.cxx:52: error: invalid types 'int[int]' for array subscript
hw1.cxx:53: error: invalid types 'int[int]' for array subscript
hw1.cxx:57: error: no matching function for call to 'std::basic_ofstream<char, std::char_traits<char> >::open(std::string&)'
/usr/local/gcc443/lib/gcc/i386-pc-solaris2.10/4.4.3/../../../../include/c++/4.4.3/fstream:696: note: candidates are: void std::basic_ofstream<_CharT, _Traits>::open(const char*, std::_Ios_Openmode) [with _CharT = char, _Traits = std::char_traits<char>]
hw1.cxx:67: error: invalid types 'int[int]' for array subscript
hw1.cxx:68: error: invalid types 'int[int]' for array subscript
hw1.cxx:69: error: invalid types 'int[int]' for array subscript

3 个答案:

答案 0 :(得分:4)

我记得C ++ 03禁止在模板中使用具有内部链接的类型。

您应该从main()函数中拉出RGB类

答案 1 :(得分:2)

在C ++ 03中(几乎可以肯定你在使用g ++ 4.4.3时),问题是带有内部链接的本地类型和类型不能用作模板参数。在这种情况下,您的RGB课程在main内定义,不能用作vector的参数。

在C ++ 11中放宽了限制,但我不记得是否仍然禁止(函数)本地类型。 Visual Studio可能正在使用C ++ 11规则,而g ++则不然。

答案 2 :(得分:2)

我看到两个错误:

vector< vector<RGB> > rgb(h, vector<RGB>(w));

C ++ 03不允许将此类型用于函数的“本地”类型,因此将定义移出main()。

同样,open函数不会将std::string作为参数,而是使用const char *。所以改变

ppmfile.open (filename);

ppmfile.open( filename.c_str() );