Hi all,

I'm having a little problem with array and some "unqualified-id" problem. I've created gazillions (okay, maybe 80) arrays before but I had never encountered such problem. Especially with static arrays.

#include <stdio.h>
#include <cstdlib>
using namespace std;

int main()
{
	// with addition of buffers for 3x3 square mask
	// 8x8 original image is now 10x10
	int [][] imageSquare =                                      //12
	{ 
		{4, 4, 4, 4, 4, 4, 4, 4, 4, 4},
		{4, 4, 4, 4, 4, 4, 4, 4, 4, 4},
		{4, 4, 4, 4,48, 4, 4, 4, 4, 4},
		{4, 4, 4,64,64,64,64, 4, 4, 4},
		{4, 4,17,64,64,96,64, 4, 4, 4},
		{4, 4, 4,64,85,64,64, 8, 4, 4},
		{4, 4, 4,64,64,64,64, 4, 4, 4},
		{4, 4,56, 4, 4,23, 4, 4, 4, 4},
		{4, 4, 4, 4, 4, 4, 4, 4, 4, 4},
		{4, 4, 4, 4, 4, 4, 4, 4, 4, 4}
	};

	// 3x3 square mask
	int [][] squareMask = 
	{
		{1, 1, 1},
		{1,-8, 1},
		{1, 1, 1}
	};

	int [][] resultSquare;
	resultSquare = new int [10][10];

	// just instantiating result arrays
	for(int i = 0; i < 10; i++){
		for(int j = 0; j< 10; j++){
			resultSquare [i][j] = 0;
		}
	}

	//now calculating for 3x3 squre mask
	int i,j,m,n,a,b;
	for(i = 1; i < 9; i++){        //starting from the original image array
		for(j = 1; j < 9; j++)
		{
			a = i-1;
			b = j-1;
			
			for(m = 0; m < 3; m++, a++){
				for(n = 0; n < 3; n++, b++)
				{
					resultSquare [a][b] = resultSquare [a][b] + imageSquare [a][b] - squareMask[m][n];
				}
			}

                  }
	}

	// print out the result 
	
}

the error that I'm getting is

comp.cpp:12: error: expected unqualified-id before '[' token

I googled but I've had not much luck with it. Would someone shed some light as to what I did wrong?

Dani AI

Generated

The compiler error comes from declaring an array without an identifier before the brackets. In C++, a declaration must be: type, then name, then the extents. So write the identifier first and, for multidimensional arrays, all but the leftmost dimension must be specified. For example, this is valid because the leftmost extent can be deduced from the initializer: int matrix[][3] = { {0,1,2}, {3,4,5} };. Placing the brackets before the name (e.g., int[][10] ...) produces the "expected unqualified-id before '[' token" error because the compiler is literally expecting an identifier in that position. See cppreference: Arrays.

If you want a dynamically allocated 2D array with new, declare a pointer to an array type, not a pointer to pointer. For a 10x10 grid: int (*result)[10] = new int[10][10]; /* use result[i][j] */ delete[] result;. This ensures correct contiguous layout and indexing; details in cppreference: new expression. For safer, idiomatic C++, prefer std::array<std::array<int,10>,10> for fixed size or std::vector<std::vector<int>> for dynamic size (std::array, std::vector).

One more tip for your 3x3 window: when you advance rows of the mask, reinitialize the column index each time, or compute indices as offsets from the center to avoid cumulative drift.

Recommended Answers

All 10 Replies

2 things:
first, arrays are declared like this:

int array[5];
int array2[] = {1, 2, 3, 4, 5};

not like this:

int[5] array;
int[] array2 = {1, 2, 3, 4, 5};

secondly, when dealing with multidimensional arrays, unfortunately you can't do

int array[][] = {{1, 2}, {3, 4}, {5, 6}};

you have to do

int array[][2] = {{1, 2}, {3, 4}, {5, 6}};

you can only leave the first dimension of an array up to the compiler (I'm going off of g++, it COULD be different for other compilers).

since you were googling for an answer, maybe check this out as well:
http://www.cplusplus.com/doc/tutorial/arrays/

Hope that's helpful

~J

In addition to the declaration issues jesseb07 pointed out, I think you've still got indexing issues with the m and n loops that also increment a and b .

[edit]Maybe reset b ?

a = i-1;
            for ( m = 0; m < 3; m++, a++ )
            {
                b = j-1;
                for ( n = 0; n < 3; n++, b++ )

So I changed my code and tried your way to no avail... :/

#include <stdio.h>
#include <cstdlib>
using namespace std;

int main(void)
{
   // with addition of buffers for 3x3 square mask
   // 8x8 original image is now 10x10
   int [][10] imageSquare =                            
   { 
   	{4, 4, 4, 4, 4, 4, 4, 4, 4, 4},
   	{4, 4, 4, 4, 4, 4, 4, 4, 4, 4},
   	{4, 4, 4, 4,48, 4, 4, 4, 4, 4},
   	{4, 4, 4,64,64,64,64, 4, 4, 4},
	{4, 4,17,64,64,96,64, 4, 4, 4},
	{4, 4, 4,64,85,64,64, 8, 4, 4},
	{4, 4, 4,64,64,64,64, 4, 4, 4},
	{4, 4,56, 4, 4,23, 4, 4, 4, 4},
	{4, 4, 4, 4, 4, 4, 4, 4, 4, 4},
	{4, 4, 4, 4, 4, 4, 4, 4, 4, 4}
   };
}

This still gives the same error of comp.cpp:12: error: expected unqualified-id before '[' token.

I mean, it says it's expecting some kind of id before [ which I obviously don't have. But again, I have absolutely no idea what I am missing.

In addition to the declaration issues jesseb07 pointed out, I think you've still got indexing issues with the m and n loops that also increment a and b .

I'll check that out. But that really isn't the main issue as I can't even seem to instantiate simple 2d static arrays..


Edited to add:

I've been googling how to declare 2d arrays. And from what I've gathered, everybody seems to be doing what I did in the first place.

http://www.cafeaulait.org/course/week2/52.html

They are within first 10 results from googling declare 2d array c++. I know I shouldn't believe everything they say on interwebs but this is what I've been seeing mostly...

Note the difference between these two:

int [][10] imageSquare =
int imageSquare[][10]  =

Also, I edited my earlier reply before I saw this post.

So I changed my code and tried your way to no avail... :/

#include <stdio.h>
#include <cstdlib>
using namespace std;

int main(void)
{
   // with addition of buffers for 3x3 square mask
   // 8x8 original image is now 10x10
   int [][10] imageSquare =                            
   { 
   	{4, 4, 4, 4, 4, 4, 4, 4, 4, 4},
   	{4, 4, 4, 4, 4, 4, 4, 4, 4, 4},
   	{4, 4, 4, 4,48, 4, 4, 4, 4, 4},
   	{4, 4, 4,64,64,64,64, 4, 4, 4},
	{4, 4,17,64,64,96,64, 4, 4, 4},
	{4, 4, 4,64,85,64,64, 8, 4, 4},
	{4, 4, 4,64,64,64,64, 4, 4, 4},
	{4, 4,56, 4, 4,23, 4, 4, 4, 4},
	{4, 4, 4, 4, 4, 4, 4, 4, 4, 4},
	{4, 4, 4, 4, 4, 4, 4, 4, 4, 4}
   };
}

This still gives the same error of comp.cpp:12: error: expected unqualified-id before '[' token.

I mean, it says it's expecting some kind of id before [ which I obviously don't have. But again, I have absolutely no idea what I am missing.

ironically enough, you missed the entire point of my post, change "int[][10] imageSquare" to "int imageSquare[][10]" and your compiler error will go away

ironically enough, you missed the entire point of my post, change "int[][10] imageSquare" to "int imageSquare[][10]" and your compiler error will go away

huh, yeah somehow I completely missed that. Well, your way did eliminate the problem! Before I give you an e-hug, what difference does the placement of [][] make? Thanks a bunch.


Edited to add:

wow, do i feel retarded or what...

Can some one help me with my code because i get the same error in line 18. right after int getanInt();

#include <iostream>
#include <string>
#include <cctype>
using namespace std;

int main()
{
    int getanInt();
    int value;

    cout << "Enter an integer value: ";
    value = getanInt();
    cout << "The integer entered is: " << value << endl;

    return 0;
}
    int getanInt();
{
    bool isvalidInt(string);
    bool notanint = true;
    string svalue;

    while (notanint)
    {
          try
       {
             cin >> svalue;
        if(!isvalidInt(svalue)) throw svalue;
        }
        catch (string e)
        {
              cout << "Invalid integer - Please re-enter: ";
        continue;
        }
        notanint = false;
        }
        return atoi(svalue.c_str());
        }

        bool isvalidInt(string str)
        {
             int start = 0;
             int i;
             bool valid = true;
             bool sign = false;

        if (str.length() == 0) valid = false;
        if (str.at(0) == '-' || str.at(0) == '+')
        {
           sign = true;
           start = 1;
           }

        if (sign && str.length() == 1) valid = false;

        i = start;
        while (valid && i < str.length())
        {
              if (!isdigit(str.at(i))) valid = false;
        i++;
        }
        return valid;
        }

Right off the bat from your code you need to put int GetanInt(); before your main function so that the compiler knows there is a function called GetanInt() somewhere. Secondly you have a semi colon after your function name on line 17. There could be more but thats right from the start.

I'm having the same problem but with SFML.

#include <SFML/Window.hpp>
using namespace sf;

// main() is where program execution begins

{
    sf::Window window(sf::VideoMode(800, 600), "My window");

    // run the program as long as the window is open
    while (window.isOpen())
    {
        // check all the window's events that were triggered since the last iteration of the loop
        sf::Event event;
        while (window.pollEvent(event))
        {
            // "close requested" event: we close the window
            if (event.type == sf::Event::Closed)
                window.close();
        }
    }

    return 0;
}

on the first curly brace.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.