Need some asistance with debugg

error: invalid types ‘int[int]’ for array subscript

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

30 Replies - 3216 Views - Last Post: 28 July 2008 - 09:16 AM Rate Topic: -----

#1 Delta_Echo  Icon User is offline

  • D.I.C Addict

Reputation: 5
  • View blog
  • Posts: 722
  • Joined: 24-October 07

Need some asistance with debugg

Posted 16 July 2008 - 06:51 AM

I intend to use this code in an encryption program. But, because this is the first time I have used classes,
I am getting some new errors that I have never seen. I'm hopping some of you elite programming ninjas can help ;-)

My intent:

1. Make the code work (duh)
2. Make it very efficient.
3. Extreme importance on mobility and re usability.
4. Full functionality, that is each class can perform
many tasks that would be needed for that class.
5. Easy to understand and modify.


Any tips you guys (and girls) can give would be appreciated.
MAIN.CPP:
#include <cstdlib>
#include <stdio.h>
#include <time.h>
#include <iostream>
#include <fstream>

#include "cipher.h"

using namespace std;

cipher Ciph1;

int main(){
     Ciph1.seed_gen(Ciph1.MySeed);
     Ciph1.ciph_gen(Ciph1.MyCipher,Ciph1.MySeed);
     if (ciph_any(Ciph1.MyCipher) != true){
         cout << "Fatal Error: In: main(): In: ciph_any(): Ciph1: MyCipher: Values out of range! Terminating...\n";
         return 0;
     }
     Ciph1.ciph_pri(Ciph1.MyCipher);
     cin.get();
}




Cipher.h:
:
/**
 Fucntions:
 **************************************************************************************************
 *seed_gen(<Seed_Array>);                | Generates the seeds to be used by the cipher generater.*
 *ciph_gen(<Cipher_Array>,<Seed_Array>); | Generates the cipher using the seed array.             *
 *ciph_any(<Cipher_Array>);              | Checks the array for values exceding 9 or preceding 0  *
 *                                       | if an error is found the main() decides how to handle  *
 *                                       | it, based on user arguments to main().                 *
 *ciph_pri(<Cipher_Array>);              | Prints the array.                                      *
 **************************************************************************************************
 **/
/** _--+++Written by Delta+++--_ **/

#include <stdio.h>
#include <cstdlib>
#include <time.h>
#include <iostream>

using namespace std;


#define RAND_MAX = 9
#define RAND_MIN = 0

class cipher{
    public:

    // Public Variables
    int MyCipher[512][10];
    int MySeed[512];

    // Public Functions

     // ***Seed Generator***
     void seed_gen(int* MyPoint){
         for (int a = 0;a != 512;a++){
             srand (time(NULL));
             MyPoint[a] = rand();
         }
     }

     // ***Cipher Generator***
     // ciph_gen(<Cipher_Array>,<Seed_Array>);
     void ciph_gen(int* MyPoint,int* MySec){
         // Stage 1: Set each first element of
         // each line to the seed value of that
         // line
         for (int a = 1;a != 512;a++){
             MyPoint[a][1] = MySec[a];
         }
         int con;
         //Stage 2
         for (int b = 1;b != 512;b++){
             con = MyPoint[b][1];
             for (int c = 1;c != 10;c++){
                 if (con == 9){
                     c++;
                     con = 0;
                     MyPoint[b][c] = con;
                 }
                 else {
                     MyPoint[b][c] = con++;
                 }
             }
         }
     }

     // ***Cipher Anylizer***
     // ciph_any(<cipher_array>);
     bool cipher_any(int* MyPoint){
         for (int a = 1;a != 512;a++){
             for(int b = 1;b != 10;a++){
                 if (MyPoint[a][b] > 9 || MyPoint[a][b] < 0){
                     return false; // Integrity = VOID
                 }
             }
         }
     }

     // ***Print Cipher***
     // ciph_pri(<Cipher_Array>)
     void ciph_pri(int* MyPoint){
         for (int a = 1;a != 512;a++){
             for (int b = 1;b != 10;b++){
                 cout << MyPoint[a][b];
             }
             cout << "\n";
         }
     cin.get();
     }

}



Compiler:
cipher.h:22:1: warning: "RAND_MAX" redefined
In file included from /usr/lib/gcc/i386-redhat-linux/4.3.0/../../../../include/c++/4.3.0/cstdlib:73,
				 from main.cpp:1:
/usr/include/stdlib.h:129:1: warning: this is the location of the previous definition
In file included from main.cpp:7:
cipher.h: In member function ‘void cipher::ciph_gen(int*, int*)’:
cipher.h:49: error: invalid types ‘int[int]’ for array subscript
cipher.h:54: error: invalid types ‘int[int]’ for array subscript
cipher.h:59: error: invalid types ‘int[int]’ for array subscript
cipher.h:62: error: invalid types ‘int[int]’ for array subscript
cipher.h: In member function ‘bool cipher::cipher_any(int*)’:
cipher.h:73: error: invalid types ‘int[int]’ for array subscript
cipher.h:73: error: invalid types ‘int[int]’ for array subscript
cipher.h: In member function ‘void cipher::ciph_pri(int*)’:
cipher.h:85: error: invalid types ‘int[int]’ for array subscript
main.cpp: At global scope:
main.cpp:9: error: expected unqualified-id before ‘using’
main.cpp: In function ‘int main()’:
main.cpp:15: error: no matching function for call to ‘cipher::ciph_gen(int [512][10], int [512])’
cipher.h:44: note: candidates are: void cipher::ciph_gen(int*, int*)
main.cpp:16: error: ‘ciph_any’ was not declared in this scope
main.cpp:20: error: no matching function for call to ‘cipher::ciph_pri(int [512][10])’
cipher.h:82: note: candidates are: void cipher::ciph_pri(int*)



Any help would be appreciated.
Thanks everyone!

This post has been edited by Delta_Echo: 16 July 2008 - 06:53 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Need some asistance with debugg

#2 Cerolobo  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 53
  • View blog
  • Posts: 450
  • Joined: 05-April 08

Re: Need some asistance with debugg

Posted 16 July 2008 - 07:11 AM

cipher.h:22:1: warning: "RAND_MAX" redefined
RAND_MAX is actually a standard C define. It's defined in stdlib.h. You should use a different name, so that you don't conflict with standard C names.

The rest of the errors are relating to this
MyPoint[a][1]

MyPoint is defined as int* MyPoint, so you can't do two subscript operators on it.

I assume you meant to declare the code as a int **MyPoint; however, I'm not 100% sure about this.


At the end of Cipher.h, you are missing a semicolon after the close curly bracket. IE, };


That should hopefully clear up all your errors.

Edit: Since you have int MyCipher[512][10];, MyPoint should be defined as int (*MyPoint)[10]

This post has been edited by Cerolobo: 16 July 2008 - 07:16 AM

Was This Post Helpful? 1
  • +
  • -

#3 AmitTheInfinity  Icon User is offline

  • C Surfing ∞
  • member icon

Reputation: 117
  • View blog
  • Posts: 1,559
  • Joined: 25-January 07

Re: Need some asistance with debugg

Posted 16 July 2008 - 07:15 AM

By observing your code first thing that strike my mind was, why are you sending members variables or object to the member methods of same object? They are anyways accessible to those methods.

So I think none of the methods from your Cipher class needs parameters to be passed. You can directly access those arrays of your class in those methods. Use of those pointers are creating complications and errors there. This will resolve most of your problems.

The redefinition error may be because of the macro is already defined. You can change the name of macro in that case.
Was This Post Helpful? 0
  • +
  • -

#4 Cerolobo  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 53
  • View blog
  • Posts: 450
  • Joined: 05-April 08

Re: Need some asistance with debugg

Posted 16 July 2008 - 07:24 AM

As another note, you really should move all your function bodies to it's own .cpp file. With the way it's currently set up, you can only include Cipher.h once. If you included it more then once, you will get linker errors, due to having multiple definitions for each function.

Plus, you should look into header guards. The typical way of doing this is as follows

// Top of the header and don't add this comment :/
#ifndef CIPHER_H // or what ever you want to call it
#define CIPHER_H

// Put all your header stuff in here

#endif


A alternative is to use #pragma once; however, it is NOT standard.
Was This Post Helpful? 1
  • +
  • -

#5 Delta_Echo  Icon User is offline

  • D.I.C Addict

Reputation: 5
  • View blog
  • Posts: 722
  • Joined: 24-October 07

Re: Need some asistance with debugg

Posted 16 July 2008 - 08:07 AM

Thanks, but i still have some errors:
In file included from main.cpp:8:
cipher.h:22:1: warning: "RAND_MAX" redefined
In file included from /usr/lib/gcc/i386-redhat-linux/4.3.0/../../../../include/c++/4.3.0/cstdlib:73,
				 from main.cpp:1:
/usr/include/stdlib.h:129:1: warning: this is the location of the previous definition
main.cpp: In function ‘int main()’:
main.cpp:16: error: no matching function for call to ‘cipher::ciph_gen(int (*)[512][10], int (*)[512])’
cipher.h:44: note: candidates are: void cipher::ciph_gen(int**, int*)
main.cpp:17: error: ‘ciph_any’ was not declared in this scope
main.cpp:21: error: no matching function for call to ‘cipher::ciph_pri(int [512][10])’
cipher.h:82: note: candidates are: void cipher::ciph_pri(int**)



Main.cpp:
#include <cstdlib>
#include <stdio.h>
#include <time.h>
#include <iostream>
#include <fstream>
#include <stdlib.h>

#include "cipher.h"

using namespace std;

cipher Ciph1;

int main(){
     Ciph1.seed_gen(Ciph1.MySeed);
     Ciph1.ciph_gen(&Ciph1.MyCipher,&Ciph1.MySeed);
     if (ciph_any(Ciph1.MyCipher) != true){
         cout << "Fatal Error: In: main(): In: ciph_any(): Ciph1: MyCipher: Values out of range! Terminating...\n";
         return 0;
     }
     Ciph1.ciph_pri(Ciph1.MyCipher);
     cin.get();
}



cipher.h:
/**
 Fucntions:
 **************************************************************************************************
 *seed_gen(<Seed_Array>);                | Generates the seeds to be used by the cipher generater.*
 *ciph_gen(<Cipher_Array>,<Seed_Array>); | Generates the cipher using the seed array.             *
 *ciph_any(<Cipher_Array>);              | Checks the array for values exceding 9 or preceding 0  *
 *                                       | if an error is found the main() decides how to handle  *
 *                                       | it, based on user arguments to main().                 *
 *ciph_pri(<Cipher_Array>);              | Prints the array.                                      *
 **************************************************************************************************
 **/
/** _--+++Written by Delta+++--_ **/

#include <stdio.h>
#include <cstdlib>
#include <time.h>
#include <iostream>

using namespace std;


#define RAND_MAX = 9
#define RAND_MIN = 0

class cipher{
      public:

    // Public Variables
    int MyCipher[512][10];
    int MySeed[512];

    // Public Functions

     // ***Seed Generator***
     void seed_gen(int* MyPoint){
         for (int a = 0;a != 512;a++){
             srand (time(NULL));
             MyPoint[a] = rand();
         }
     }

     // ***Cipher Generator***
     // ciph_gen(<Cipher_Array>,<Seed_Array>);
     void ciph_gen(int** MyPoint,int* MySec){
         // Stage 1: Set each first element of
         // each line to the seed value of that
         // line
         for (int a = 1;a != 512;a++){
             MyPoint[a][1] = MySec[a];
         }
         int con;
         //Stage 2
         for (int b = 1;b != 512;b++){
             con = MyPoint[b][1];
             for (int c = 1;c != 10;c++){
                 if (con == 9){
                     c++;
                     con = 0;
                     MyPoint[b][c] = con;
                 }
                 else {
                     MyPoint[b][c] = con++;
                 }
             }
         }
     }

     // ***Cipher Anylizer***
     // ciph_any(<cipher_array>);
     bool cipher_any(int** MyPoint){
         for (int a = 1;a != 512;a++){
             for(int b = 1;b != 10;a++){
                 if (MyPoint[a][b] > 9 || MyPoint[a][b] < 0){
                     return false; // Integrity = VOID
                 }
             }
         }
     }

     // ***Print Cipher***
     // ciph_pri(<Cipher_Array>)
     void ciph_pri(int** MyPoint){
         for (int a = 1;a != 512;a++){
             for (int b = 1;b != 10;b++){
                 cout << MyPoint[a][b];
             }
             cout << "\n";
         }
     cin.get();
     }

};



I don't get it, can't main see my cipher class and the functions inside it?
Was This Post Helpful? 0
  • +
  • -

#6 polymath  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 52
  • View blog
  • Posts: 670
  • Joined: 04-April 08

Re: Need some asistance with debugg

Posted 16 July 2008 - 08:44 AM

Like amittheinfinity said:

Cipher.h
/**
 Fucntions:
  ********************************************************************************
******************
 *seed_gen();                | Generates the seeds to be used by the cipher generater.          *
 *ciph_gen();                | Generates the cipher using the seed array.                       *
 *ciph_any();                | Checks the array for values exceding 9 or preceding 0            *
 *                                    | if an error is found the main() decides how to handle   *
 *                                    | it, based on user arguments to main().                  *
 *ciph_pri();                | Prints the array.                                                *
  ********************************************************************************
******************
 **/
/** _--+++Written by Delta+++--_ **/

#include <stdio.h>
#include <cstdlib>
#include <time.h>
#include <iostream>

using namespace std;

class cipher{
      public:

    // Public Variables
    int MyCipher[512][10];
    int MySeed[512];

    // Public Functions

     // ***Seed Generator***
     void seed_gen(){
         int random;
         srand(time(NULL)); //Only seed random once in a function
         for (int a = 0;a != 512;a++){
             random=(rand() %10); //makes it 0-9
             MySeed[a] = random;
         }
     }

     // ***Cipher Generator***
     // ciph_gen();
     void ciph_gen(){
         // Stage 1: Set each first element of
         // each line to the seed value of that
         // line
         for (int a = 1;a != 512;a++){
             MyCipher[a][1] = MySeed[a];
         }
         int con;
         //Stage 2
         for (int b = 1;b != 512;b++){
             con = MyCipher[b][1];
             for (int c = 1;c != 10;c++){
                 if (con == 9){
                     c++;
                     con = 0;
                     MyCipher[b][c] = con;
                 }
                 else {
                     MyCipher[b][c] = con++;
                 }
             }
         }
     }

     // ***Cipher Anylizer***
     // ciph_any();
     bool cipher_any(){
         for (int a = 1;a != 512;a++){
             for(int b = 1;b != 10;a++){
                 if (MyCipher[a][b] > 9 || MyCipher[a][b] < 0){
                     return false; // Integrity = VOID
                 }
             }
         }
     }

     // ***Print Cipher***
     // ciph_pri();
     void ciph_pri(){
         for (int a = 1;a != 512;a++){
             for (int b = 1;b != 10;b++){
                 cout << MyCipher[a][b];
             }
             cout << "\n";
         }
     cin.get();
     }

};



Main.cpp
#include <cstdlib>
#include <cstdio>
//#include <stdio.h> is the same as cstdio
#include <ctime>
//#include <time.h> is the same as ctime
#include <iostream>
#include <fstream>
//#include <stdlib.h> is the same as cstdlib

#include "cipher.h"

using namespace std;

cipher Ciph1;

int main(){
     Ciph1.seed_gen();
     Ciph1.ciph_gen();
     if (Ciph1.ciph_any() != true){
         cout << "Fatal Error: In: main(): In: ciph_any(): Ciph1: MyCipher: Values out of range! Terminating...\n";
         return 0;
     }
     Ciph1.ciph_pri();
     cin.get();
}



Also, if I were you, I'd move my dependant includes into cipher.h, like iostream and ctime, and such. That way you don't get errors when you move cipher but not time (because then cipher will not have time(NULL). Also, since you don't need to acccess MyCipher and MySeed (the class structure does that for you), I would make them private so that you can't accidentally mess something up in them. Finally, you should move the srand(time(NULL)); into either an initialization function you call in main or into main itself because you never want to seed rand more then once in a program.

This post has been edited by polymath: 16 July 2008 - 08:53 AM

Was This Post Helpful? 0
  • +
  • -

#7 Delta_Echo  Icon User is offline

  • D.I.C Addict

Reputation: 5
  • View blog
  • Posts: 722
  • Joined: 24-October 07

Re: Need some asistance with debugg

Posted 16 July 2008 - 08:52 AM

Ah, so no arguments are needed? Thanks Amit and Poly.
Was This Post Helpful? 0
  • +
  • -

#8 polymath  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 52
  • View blog
  • Posts: 670
  • Joined: 04-April 08

Re: Need some asistance with debugg

Posted 16 July 2008 - 08:54 AM

That is the beauty of classes/struts
Was This Post Helpful? 0
  • +
  • -

#9 Delta_Echo  Icon User is offline

  • D.I.C Addict

Reputation: 5
  • View blog
  • Posts: 722
  • Joined: 24-October 07

Re: Need some asistance with debugg

Posted 16 July 2008 - 09:04 AM

Well i compiled the code, and on execution bash returns this message:
 
Segmentation Fault


? wtf ?
Was This Post Helpful? 0
  • +
  • -

#10 polymath  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 52
  • View blog
  • Posts: 670
  • Joined: 04-April 08

Re: Need some asistance with debugg

Posted 16 July 2008 - 09:11 AM

Since you are using bash, you are (i think) using linux, and the only thing I can think of that would make it not work is non-standard code, but i can't find any of that in there.

EDIT: Segmentation fault: http://en.wikipedia....mentation_fault

This post has been edited by polymath: 16 July 2008 - 09:18 AM

Was This Post Helpful? 0
  • +
  • -

#11 Delta_Echo  Icon User is offline

  • D.I.C Addict

Reputation: 5
  • View blog
  • Posts: 722
  • Joined: 24-October 07

Re: Need some asistance with debugg

Posted 16 July 2008 - 09:13 AM

Yeah, im using Linux Fedora 9.
Was This Post Helpful? 0
  • +
  • -

#12 Delta_Echo  Icon User is offline

  • D.I.C Addict

Reputation: 5
  • View blog
  • Posts: 722
  • Joined: 24-October 07

Re: Need some asistance with debugg

Posted 16 July 2008 - 11:03 AM

I don't see any problems in the code....?
Was This Post Helpful? 0
  • +
  • -

#13 polymath  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 52
  • View blog
  • Posts: 670
  • Joined: 04-April 08

Re: Need some asistance with debugg

Posted 16 July 2008 - 11:27 AM

I don't either, and i can't find a reason for the segfault.

segfaults should only happen when you try to overwrite non-allocated memory... check your pointers

Also, I see that you are initializing a and b in some of you loops to be 1, not zero, though that shouldn't be a problem...

Maybe one of your loops is overrunning the size of the array...
Was This Post Helpful? 0
  • +
  • -

#14 Delta_Echo  Icon User is offline

  • D.I.C Addict

Reputation: 5
  • View blog
  • Posts: 722
  • Joined: 24-October 07

Re: Need some asistance with debugg

Posted 16 July 2008 - 10:20 PM

MAybe the memore was being used by my os? OR maybe linux stopped my prog from accessing it....
Was This Post Helpful? 0
  • +
  • -

#15 AmitTheInfinity  Icon User is offline

  • C Surfing ∞
  • member icon

Reputation: 117
  • View blog
  • Posts: 1,559
  • Joined: 25-January 07

Re: Need some asistance with debugg

Posted 16 July 2008 - 10:46 PM

I have doubt on your this code

for (int b = 1;b != 512;b++){
             con = MyCipher[b][1];
             for (int c = 1;c != 10;c++){
                 if (con == 9){
                     c++;
                     con = 0;
                     MyCipher[b][c] = con;
                 }
                 else {
                     MyCipher[b][c] = con++;
                 }
             }
         }



let's consider your value of c has reached to 9 and value of con is also 9, in this case your c increments by 1 and it becomes 10! now if you try to access array with this value of c then it will give segmentation fault.

That's what I think, I am not sure whether this case occurs in your scenario.

I hope this will help you. :)
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3