c++ - 菜鸟的快速 g++ 错误诊断

标签 c++ compilation g++

<分区>

有人可以告诉我如何修复 g++ 给我的这个短程序或其中任何其他糟糕的编译错误。我是菜鸟。谢谢。我知道其中大部分是因为我不知道自己在做什么。

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

int main (int argc, char* argv[])
{   
    struct RGB {
        float r;
        float g;
        float b;
    };

    int w = atoi(argv[1]);
    int h = atoi(argv[2]);

    vector<vector RGB > image;
    image.resize(w);
    for (int q = 0; q < w; ++q)
    image[q].resize(h);

    for (int i = 0; i < w; ++i){
        for (int j = 0; j < h; ++j){
            float col = float (((i & 0x08) == 0) ^ ((j & 0x08) == 0));
            image[i][j].r = col;
            image[i][j].g = col;
            image[i][j].b = col;
        }
    }

    string filename = string(argv[3]) + ".ppm";
    ofstream file(filename);
    file << "P3" << endl;
    file << w << " " << h << endl;
    file << "255" << endl;
    for (int i = 0; i < h; ++i){
        for (int j = 0; j < w; ++j){
            float col = float (((i & 0x08) == 0) ^ ((j & 0x08) == 0));
            file << image[i][j].r*255 << " ";
            file << image[i][j].g*255 << " ";
            file << image[i][j].b*255 << " ";
        }
        file << endl;
    }
    file.close();

    return 0;
}

错误:

hw1.cpp: In function ‘int main(int, char**)’:
hw1.cpp:24: error: template argument for ‘template<class _Alloc> class std::allocator’ uses local type ‘main(int, char**)::RGB’
hw1.cpp:24: error:   trying to instantiate ‘template<class _Alloc> class std::allocator’
hw1.cpp:24: error: template argument 2 is invalid
hw1.cpp:24: error: template argument 1 is invalid
hw1.cpp:24: error: template argument 2 is invalid
hw1.cpp:24: error: invalid type in declaration before ‘;’ token
hw1.cpp:25: error: request for member ‘resize’ in ‘image’, which is of non-class type ‘int’
hw1.cpp:27: error: invalid types ‘int[int]’ for array subscript
hw1.cpp:32: error: invalid types ‘int[int]’ for array subscript
hw1.cpp:33: error: invalid types ‘int[int]’ for array subscript
hw1.cpp:34: error: invalid types ‘int[int]’ for array subscript
hw1.cpp:39: error: no matching function for call to ‘std::basic_ofstream<char, std::char_traits<char> >::basic_ofstream(std::string&)’
/usr/include/c++/4.2.1/fstream:596: note: candidates are: std::basic_ofstream<_CharT, _Traits>::basic_ofstream(const char*, std::_Ios_Openmode) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.2.1/fstream:580: note:                 std::basic_ofstream<_CharT, _Traits>::basic_ofstream() [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.2.1/iosfwd:92: note:                 std::basic_ofstream<char, std::char_traits<char> >::basic_ofstream(const std::basic_ofstream<char, std::char_traits<char> >&)
hw1.cpp:46: error: invalid types ‘int[int]’ for array subscript
hw1.cpp:47: error: invalid types ‘int[int]’ for array subscript
hw1.cpp:48: error: invalid types ‘int[int]’ for array subscript

最佳答案

看起来您正在尝试声明 vectorvector s of RGBs。您缺少一对括号:

vector<vector<RGB> > image;

此外,您应该在 main 函数之外声明 struct RGB

关于c++ - 菜鸟的快速 g++ 错误诊断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14370785/

相关文章:

c++ - 如何在 C++ 中生成无向图?

c++ - 在平方二进制矩阵中找到二进制矩形的位置

c++ - 项目编译速度太慢(约2000行)

c++ - 链接动态库

c++ - 在 C++ 中使用 `link` 作为类名时出错

c++ - C++中的类接口(interface)继承

c++ - std::ios_base::ate 和 std::ios_base::trunc

c++ - 避免指定包含模板函数指针的冗余模板参数

c++ - gcc/g++ 是否生成 if(false) 语句的主体?

c - c中相同代码的不同答案