Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9135209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:47:39+00:00 2026-06-17T08:47:39+00:00

The code compiles, and runs as expected using Visual Studio. I understand that never

  • 0

The code compiles, and runs as expected using Visual Studio. I understand that never guarantees it will compile/run elsewhere, but I don’t understand why in this instance. Perhaps someone can help clarify? The g++ compiler gives me errors at line 45 where the vector of vectors of type RGB are declared:

#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;
}

Here’s the full list of errors that pop up in 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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T08:47:40+00:00Added an answer on June 17, 2026 at 8:47 am

    As I remember C++03 forbids using types with internal linkage in templates.

    You should pull the RGB class from the main() function

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some code that compiles and runs on MSVC++ but will not compile
I have code, that compiles and runs as expected in gcc and doesn't compile
This piece of code compiles and runs as expected on GCC 3.x and 4.x:
I am using std::unique_ptr in this piece of code which compiles and runs as
This code compiles and runs though gives a Microsoft compiler error that I cant
I've got a NI CVI/Labview project that compiles by using the compile.exe command on
I was somehow surprised that the following code compiles and runs (vc2012 & gcc4.7.2)
first the code compiles and runs with VS2010 but when I compile with cl.exe
My cocos2d iOS game code compiles and runs without error in Xcode 4.2.1 but
This code compiles, and runs successfully locally, but not on another server. Both machines

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.