read access violation error
        Posted  
        
            by user293569
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by user293569
        
        
        
        Published on 2010-03-14T21:03:17Z
        Indexed on 
            2010/03/14
            21:15 UTC
        
        
        Read the original article
        Hit count: 486
        
class Node{
      private:
              string name;
              Node** adjacent;
              int adjNum;
      public:
             Node();
             Node(string, int adj_num);
             Node(const Node &);
             bool addAdjacent(const Node &);
             Node** getAdjacents();
             string getName();
             ~Node();
      };
bool Node::addAdjacent(const Node &anode){
     Node** temp;   
     temp= new Node*[adjNum+1];
     for(int i=0;i<adjNum+1;i++)
         temp[i]=adjacent[i];
     temp[adjNum]=const_cast<Node *>(&anode);
     delete[] adjacent;
     adjacent=new Node*[adjNum+1];
     adjacent=temp;
     delete[] temp;
     adjNum++;
     return true;
}
int main()
{
    Node node1("A",0);
    Node node2("B",0);
    node1.getName();
    node1.addAdjacent(node2);
    system("PAUSE");
    return 0;
}
when the program comes to this part:
for(int i=0;i<adjNum+1;i++)
     temp[i]=adjacent[i];
it says Access violation reading location 0xcccccccc. The class must allocate the memory fore adjacent, but I think it didn't how can I solve this problem?
© Stack Overflow or respective owner