Need help to solve program errors

This is a discussion on Need help to solve program errors within the C++ Programming forums, part of the General Programming Boards category; I am trying to create a median filter program under C++ with the use of a image procesing class library, ...

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    14

    Need help to solve program errors

    I am trying to create a median filter program under C++ with the use of a image procesing class library, calls picture.h, so I've written my code, but the compiler keep giving me a error message that I don't quite understand. Can somebody kindly explain to me what's going wrong with my program and suggest some useful resource that I possible can look up when I get stuck in this kind of situation. Thanks in advance.

    Code:
    #include "usage.h"
    #include "picture.h"
    int median(int& pel, const int **buf) 
    	{	char array[9];
    		array[0]=buf[-1][-1];
    		array[1]=buf[-1][0];
    		array[2]=buf[-1][1];
    		array[3]=buf[0][-1];
    		array[4]=buf[0][0];
    		array[5]=buf[0][1];
    		array[6]=buf[1][-1];
    		array[7]=buf[1][0];
    		array[8]=buf[1][1];
    		
    	for (int i=0; i<=9; i++)
    	{
    		for(int y=0; y<=8; y++)
    		{
    			if(array[y]>array[y+1])
    			{
    			int temp = array[y+1];
    			array[y+1] = array[y];
    			array[y] = temp;
    			}
    		}
    	pel = array[4];
    	return 0;
    	}
    int main(int argc, char *argv[]) 
    	{
    	usage("median inpic outpic");
    	picture_of_int in(argv[1]);
    	picture_of_int out(in,median);
    	out.write(argv[2]);
    	return 1;
    	}
    Error message under the compiler:

    median2.cpp: In function 'int median(int&, const int**)':
    median2.cpp:30: error: a function-definition is not allowed here before '{' token
    median2.cpp:36: error: expected `}' at end of input

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    You are missing a closing bracket before 'pel = array[4];'. Besides, accessing negative indices is not a good idea, I'm pretty sure it will crash your program.

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    14
    Quote Originally Posted by Desolation
    You are missing a closing bracket before 'pel = array[4];'. Besides, accessing negative indices is not a good idea, I'm pretty sure it will crash your program.
    Thanks so much for pointing that out, I've been spending hours on that, but couldn't spot that out.

  4. #4
    Registered User
    Join Date
    Apr 2004
    Posts
    14
    Code:
    char array[9];
    		array[0]=buf[-1][-1];
    		array[1]=buf[-1][0];
    		array[2]=buf[-1][1];
    		array[3]=buf[0][-1];
    		array[4]=buf[0][0];
    		array[5]=buf[0][1];
    		array[6]=buf[1][-1];
    		array[7]=buf[1][0];
    		array[8]=buf[1][1];
    Is there a way of using "for" loop or some other loop to respresent this array, so that I can change the parameter of the median filter. At the moment, this is a 3x3 median filter. I would like program something can change its parameter, i.e. 3x3, 5x5, 7x7... 15x15 without reprogramming the source code.

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    You'd probably need nested for loops of depth two.

    Code:
    for(i) // each row
    {
        for(j) // each element in the current row
        {
            array[i * width + j] //this is the corresponding element
        }
    }
    And yes, negative indices aren't a really good idea.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  6. #6
    Registered User
    Join Date
    Apr 2004
    Posts
    14
    Quote Originally Posted by jafet


    And yes, negative indices aren't a really good idea.
    Sorry, I forgot to mention in the first post that the class library "picture.h" allows "pel"'s to use negative indices for neighbourhood operators, i.e. buf[-1][-1] is the top left corner of the centre pixel. But this only apply to the functions which use pel only.

  7. #7
    Registered User
    Join Date
    Apr 2004
    Posts
    14
    You'd probably need nested for loops of depth two.
    This method is been great help, thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Errors in Dev-C++ program
    By sinking2008 in forum C Programming
    Replies: 5
    Last Post: 05-15-2008, 09:00 AM
  2. List of Errors in C# program.
    By arcaine01 in forum C# Programming
    Replies: 1
    Last Post: 05-09-2008, 02:37 PM
  3. Replies: 2
    Last Post: 12-19-2005, 05:57 PM
  4. Errors in my program
    By gjack72 in forum C++ Programming
    Replies: 3
    Last Post: 11-17-2005, 01:45 PM
  5. Counter Errors for otherwise working program???
    By jereland in forum C Programming
    Replies: 7
    Last Post: 03-22-2004, 07:37 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21