#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    California
    Posts
    27
    Rep Power
    0

    Error in usage of private class variables


    I'm getting the following error when I try to compile my code:

    Crypto.h:20: invalid use of member `Crypto::letters'
    Crypto.h:23: invalid use of member `Crypto::total_letters'


    Here's the Crypto.h file that it's refering to:

    class Crypto {
    public:
    Crypto();
    void create_grab_bag();
    void create_alphabet(ostream &);
    void create_key_file(ostream &);
    void create_list(node *&);
    void test_list(node *&, ostream &);
    int find_element(int, node *&);
    void encrypt();
    void get_input(istream &);
    void print_output(ostream &);
    private:
    const int letters;
    const int alphs;
    const int total_letters;
    char grab_bag[letters];
    char input[100];
    char output[100];
    char test_array[total_letters];
    };


    Line 20 is: char grab_bag[letters];
    Line 23 is: char test_array[total_letters];


    And here's the Crypto constructor

    Crypto::Crypto() {
    letters = 191;
    alphs = 5;
    total_letters=(letters*alphs);
    }



    I have a feeling that it's giving me an error because I'm declaring arrays the size of a variable that hasn't been declared yet but gets declared in the constructor. Any advice as to how I could do this? Thanks in advance!
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,638
    Rep Power
    4247
    Either declare it as
    char grab_bag[191];
    or do this:
    Code:
    In class declaration:
    char *grab_bag;
    char * test_array;
    In constructor:
    Crypto::Crypto() { 
        letters = 191; 
        alphs = 5; 
        total_letters=(letters*alphs); 
        grab_bag = new char[letters];
        test_array = new char[total_letters];
    } 
    
    and in the destructor:
    Crypto::~Crypto() {
        delete [] grab_bag;
        delete [] test_array;
    }
    Hope this helps :)
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    California
    Posts
    27
    Rep Power
    0
    Thanks for the speedy reply! That did work, but now it's giving me errors when I try and initialize those const variables in the constructor...is there a special way to initialize const variables in a constructor? Thanks.
  6. #4
  7. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,638
    Rep Power
    4247
    Take out the const specifier and declare them as plain old int :)
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo

IMN logo majestic logo threadwatch logo seochat tools logo