Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Please help me CPP

Please help me CPP

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestionc++data-structures
3 Posts 2 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    bluehai
    wrote on last edited by
    #1

    Hello ! I have created some classes for Huffman Tree but when I call CreateNode function it appears an error about CharArray string. What is this error ? Who can help me about this problem ? Thank you. class Node { friend class HuffmanTree; private: char *CharArray; unsigned long Frequency; Node *pLeft; Node *pRight; …. } class HuffmanTree { private: Node *pRoot; public: HuffmanTree(); ~HuffmanTree(); Node *CreateNode(char *CharArray, unsigned long Frequency); …. } Node *HuffmanTree::CreateNode(char *CharArray, unsigned long Frequency) { Node *NewNode = new Node(CharArray, Frequency); NewNode->pLeft = NULL; NewNode->pRight = NULL; return NewNode; } Node::Node(char *CharArray, unsigned long Frequency) { int CharArrayLen = strlen(CharArray)+1; this->CharArray = NULL; // delete this->CharArray; this->CharArray = new char(sizeof(char)*CharArrayLen); strcpy(this->CharArray,CharArray); this->Frequency = Frequency; pLeft = NULL; pRight = NULL; } :)

    A 1 Reply Last reply
    0
    • B bluehai

      Hello ! I have created some classes for Huffman Tree but when I call CreateNode function it appears an error about CharArray string. What is this error ? Who can help me about this problem ? Thank you. class Node { friend class HuffmanTree; private: char *CharArray; unsigned long Frequency; Node *pLeft; Node *pRight; …. } class HuffmanTree { private: Node *pRoot; public: HuffmanTree(); ~HuffmanTree(); Node *CreateNode(char *CharArray, unsigned long Frequency); …. } Node *HuffmanTree::CreateNode(char *CharArray, unsigned long Frequency) { Node *NewNode = new Node(CharArray, Frequency); NewNode->pLeft = NULL; NewNode->pRight = NULL; return NewNode; } Node::Node(char *CharArray, unsigned long Frequency) { int CharArrayLen = strlen(CharArray)+1; this->CharArray = NULL; // delete this->CharArray; this->CharArray = new char(sizeof(char)*CharArrayLen); strcpy(this->CharArray,CharArray); this->Frequency = Frequency; pLeft = NULL; pRight = NULL; } :)

      A Offline
      A Offline
      Anonymous
      wrote on last edited by
      #2

      Hello, Your code seems OK except that (IMHO): - you removed trailling ';' at the end of class declarations - you do not check for CharArray consistency (e.g. != NULL) - you should use a more structured syntax (as m_ prefix for data members) - I would recommand the use of new char[] instead of new char(). It would imply the use of delete [] when destroying the memory block. You should be more specific about your error. Do you receive an exception ?

      B 1 Reply Last reply
      0
      • A Anonymous

        Hello, Your code seems OK except that (IMHO): - you removed trailling ';' at the end of class declarations - you do not check for CharArray consistency (e.g. != NULL) - you should use a more structured syntax (as m_ prefix for data members) - I would recommand the use of new char[] instead of new char(). It would imply the use of delete [] when destroying the memory block. You should be more specific about your error. Do you receive an exception ?

        B Offline
        B Offline
        bluehai
        wrote on last edited by
        #3

        Hello, Thanks for your response. I have updated my code and they run well. I will ask you when i have new errors. I don't have any exception currently. Special thanks to you. Best regards, Hai

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups