Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

C++ Class problem...

Options
  • 05-04-2002 7:19pm
    #1
    Posts: 0


    This problem is driving me up the wall and I was hoping there would be someone out there who are fairly proficient at C++.
    Thanks by the way, for those who responded to my other Java question, very helpful.

    Righty, the problem I'm having is this dictionary program that we have to do. The program just stores words and all that...but now we have to update it with classes.

    So I have an idea for a dictionary class which creates a wordlist class, which contains the char array. Fair enough.

    But using header files, I cannot make a 2d char array from within the header file, which contains the actual class and the function prototypes.

    I want the constructor to set the lenght of the 2d array so in the header file, I have this...

    ...
    private:

    char words [length1][length2]

    ...


    and when the constructor is called, it should give those variables values.

    But it keeps telling me that they're undefined variables and all that...but they're defined in the constructor.

    Could anyone tell me how to make arrays from within classes?
    This is a little problem i'm sure, but it's drving me up the wall.
    Any help would be appreciated...thanks

    Thanks in advance!:)


Comments

  • Closed Accounts Posts: 7,346 ✭✭✭Rev Hellfire


    allocate it on the heap in the classes constructor

    ie

    m_pData = new char[ iMooWidth][iCowsLikeGrass];

    delete it in the destructor

    delete[] m_pData;


  • Posts: 0 [Deleted User]


    Great i'll try that then thanks
    :)


  • Registered Users Posts: 255 ✭✭hertz


    You wrote this:

    private:

    char words [length1][length2]

    but when you declare a 2d array dont you leave the second [] blank,
    ie char words [length1][];

    thats how you do it outside a class, you probably know that though.

    hope was some help.

    hertz


  • Registered Users Posts: 2,281 ✭✭✭DeadBankClerk


    what you really should have is a class word, sometihng like this
    class Word
    {
      private:
      char* word;
      char* meaning;
      Word(const char *w = 0, const char* m = 0);
      ~Word();
    
    
      public:
      bool setWord(const char* w);
      bool setMeaning(const char* m);
      char* getWord() const;
      char* getMeaning() const;
      
    }
    
    Word::Word(const char *w = 0, const char* m = 0)
    {
    	this->setWord(w); this->setMeaning(w);
    }
    
    Word::~Word()
    {
    	if (this->word)
    		delete [strlen(word)] this->word;
    	if (this->meaning)
    		delete [strlen(meaning)] this->meaning;
    }
    
    bool Word::setWord(const char* w)
    {
    	return bool(strcpy(this->word, w));
    }
    
    bool Word::setMeaning(const char* m)
    {
    	return bool(strcpy(this->meaning, m));
    }
    
    char* Word::getWord() const
    {
    	return this->word;
    }
    
    char* Word::getMeaning() const
    {
    	return this->meaning;
    }
    

    And then you should make a class dictionary that has a pointer of type Word, to allow you make an array of words.

    I won't write it because I'm too lazy to do the rest of your homework now :D


  • Registered Users Posts: 2,281 ✭✭✭DeadBankClerk


    bump


  • Advertisement
  • Posts: 0 [Deleted User]


    Wow!
    That's a good class, never though about it like that.

    Thanks for the help, DeadBankClerk, that was more than i was expecting , thanks for that, i wouldn't expect you to provide more code, that was more than helpful.


    thanks again
    :)


Advertisement