C++ 快速g++;noob的错误诊断

C++ 快速g++;noob的错误诊断,c++,compilation,g++,C++,Compilation,G++,有人能告诉我如何修复g++给我的这个短程序的编译错误吗?或者是它里面的任何糟糕的东西。我是个笨蛋。谢谢我知道大多数是因为我不知道自己在做什么 #include <iostream> #include <fstream> #include <string> #include <vector> using namespace std; int main (int argc, char* argv[]) { struct RGB {

有人能告诉我如何修复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;
}
#包括
#包括
#包括
#包括
使用名称空间std;
int main(int argc,char*argv[])
{   
结构RGB{
浮子r;
浮球g;
浮球b;
};
int w=atoi(argv[1]);
inth=atoi(argv[2]);
矢量图像;
图像。调整大小(w);
对于(int q=0;q文件看起来您正试图声明
矢量的
矢量的
矢量的
s的
RGB的
s。您缺少一对括号:

vector<vector<RGB> > image;
矢量图像;
另外,您应该在
main
函数之外声明
struct RGB

  • struct RGB
    移出main。不能在本地声明的类型上创建模板

  • 向量的向量应声明为:

    vector< vector<RGB> >
    

  • 这段代码有很多错误。我将带您完成前几个步骤,希望这能给您一个想法

    我们得到的第一个编译器错误是:

    15: error: ‘atoi’ was not declared in this scope
    
    atoi是一个库函数,因此我们需要包含正确的库。快速搜索“atoi头”告诉我们这是cstdlib。在顶部,我们添加:

    #include<cstdlib>
    
    第18行是:

    vector<vector RGB > image;
    
    矢量图像;
    
    声明集合的语法为:

    collection_type<type_the_collection_is_of> varname;
    
    collection\u type varname;
    
    根据需要的是像素矢量还是像素矢量矢量,这是:

    vector<RGB> image;
    vector< vector<RGB> > image;
    
    矢量图像;
    向量<向量>图像;
    
    作为一般规则,如果编译器对变量声明感到困惑,那么它将对以后使用该变量时发生的事情感到困惑

    哎呀,它仍然对声明感到不满: 21:错误:“模板类std::分配器”的模板参数使用本地类型“main(int,char**)::RGB”

    “本地类型”。这是一个奇怪的短语。哦。结构是在函数内部定义的。我根本不知道这是允许的。但对我们来说似乎不太好,所以让我们把它移到外部并再次编译


    这不是全部,但我希望我已经给了你一个如何调试这类东西的感觉。

    编译器错误是什么?
    向量图像;
    ?你是说
    向量图像;
    ?可能是的,哈哈。大多数时候我只是在尝试一些东西,直到它工作。它应该是一个二维向量。我做错了什么?从外观上看,朱不完全是我上面告诉你的。撇开这一点,看起来你做对了。大部分问题都是因为结构放错了位置。我仍然收到了第39行的错误。我想这就是所有问题。谢谢。
    vector<vector RGB > image;
    
    collection_type<type_the_collection_is_of> varname;
    
    vector<RGB> image;
    vector< vector<RGB> > image;