快速g ++错误诊断为菜鸟

时间:2013-01-17 01:23:19

标签: 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

3 个答案:

答案 0 :(得分:2)

您似乎正在尝试声明vector vectorRGB s vector<vector<RGB> > image; 。你错过了一对括号:

struct RGB

此外,您应该在main函数之外声明{{1}}。

答案 1 :(得分:0)

  1. struct RGB移出主广场。您无法对本地声明的类型进行模板化。

  2. 矢量矢量应声明为:

    vector< vector<RGB> >
    
  3. ofstream构造函数不将string对象作为参数。请改用:

    ofstream file(filename.c_str());
    

答案 2 :(得分:0)

这段代码有很多问题。我将带您完成前几个步骤,希望这会给您一个想法。

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

15: error: ‘atoi’ was not declared in this scope

atoi是一个库函数,所以我们需要包含正确的库。一个快速的谷歌“atoi标题”告诉我们这是cstdlib。在顶部,我们添加:

#include<cstdlib>

我们看到的下一个错误是:

18: error: template argument 1 is invalid

第18行是:

vector<vector RGB > image;

声明集合的语法是:

collection_type<type_the_collection_is_of> varname;

根据您是否需要像素矢量或像素矢量矢量,这是以下之一:

vector<RGB> image;
vector< vector<RGB> > image;

作为一般规则,如果编译器对变量声明感到困惑,那么在使用该变量时会发生的事情会让人感到困惑。所以让我们重新编译。

哎呀,它对这个宣言仍然不满意:     21:错误:'template class std :: allocator'的模板参数使用本地类型'main(int,char **):: RGB'

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

这不是一切,但我希望我已经给你一个如何调试这种事情的感觉。

相关问题