Thread: Debug Assertion Error

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    49

    Debug Assertion Error

    Hello, I would very much appreciate any help. I'm a new programmar. I'm tring to make a text game and I Keep getting a Debug Assertion Error at runtime. Can Someone Please Help me know why and how to fix it. Thank you.
    Code:
    #include <string>
    using namespace std;
    class Space
    {
    public:
    
    private:
    string Description;
    };
    
    class Player
    {
    public:
    Space **Location;
    private:
    
    };
    
    
    int main(){
    Player Anthony;
    Space A[10][10];
    Anthony.Location=(Space**)A ; // I think this part is causing the 
    for ( int i = 0 ; i < 10 ; i++ ){    // error
    Anthony.Location[i] = A[i] ;
    }
    
    
    
    
    return 0;
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Anthony.Location=(Space**)A ;
    If you have to use a cast, you're probably doing something suspicious. By the way, Space** isn't the same as a two dimensional array of Space. If you really wanted to do it this way, you could do this:
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    class Space {
    public:
      int Description;
    };
    
    class Player {
    public:
      Space (*Location)[10];
    };
    
    int main()
    {
      Space A[10][10];
      Player Anthony;
    
      Anthony.Location = A;
      for ( int i = 0; i < 10; i++ ) {
        for ( int j = 0; j < 10; j++ )
          Anthony.Location[i][j].Description = i + j;
      }
    
      for ( int i = 0; i < 10; i++ ) {
        for ( int j = 0; j < 10; j++ )
          cout<< Anthony.Location[i][j].Description <<' ';
        cout<<endl;
      }
    }
    But I wouldn't recommend it for anything but a toy that prints a nifty figure.
    My best code is written with the delete key.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well the cast rather gives it away. A pointer to a pointer (**) is not the same thing as a 2D array. Sure you can use [row][column] to access either, but that is where the similarity ends.

    Try this
    Code:
    #include <string>
    using namespace std;
    class Space
    {
    public:
    
    private:
    string Description;
    };
    
    class Player
    {
    public:
    Space (*Location)[10];  // a pointer to an array
    private:
    
    };
    
    
    int main(){
        Player Anthony;
        Space A[10][10];
        Anthony.Location=A;
        return 0;
    }

  4. #4
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    I like the approach you are taking to this project.. but at the same time I see some basic areas of improvement.. for starters:

    Code:
    class Space
    {
    public:
    
    private:
    string Description;
    };
    There is nothing in the public area of this class.. which isn't wrong.. but a class isn't a class without some sort of function.

    Code:
    class Player
    {
    public:
    Space **Location;
    private:
    
    };
    Similar problem with the Player class.. location (although i'm not sure if it's declared correctly) I think should appear in the private area of the class.


    General Class Format:

    Public: will contain class member functions

    Private: will contain class variables.. which can only be accessed by class member functions or friend functions


    I like seeing the use of the double pointer to reference the 2 dimensional array.. but that's a technique I haven't specifically used myself.. so I cannot make a judgement right off hand as to its correctness.

    I would like to see more code if you don't mind.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Bah, beaten by microseconds...

  6. #6
    Registered User
    Join Date
    Nov 2004
    Posts
    49
    Hello, Thank you for the fast replies. Let me try to explain what I was tring to do.
    I put the:
    Code:
    Space **Location
    Thinking I can use it to Point to an Multi-dimensional array. Then I can access the Array though the Pointer to pointer Location.
    Code:
     Space (*Location)[10];
    That looks good, but I didn't want to specify how much the array will be. Since the Rooms will change for each Level.
    I wanted to assign more then 10 to some rooms I make.
    The Space class I wanted to hold information liike:
    Description of each spot the player moves to within the room Muti-dimensional array. And Location will keep track of his concordinates. and I can manipulate Location for Methods such as Move and stuff Ill add later.
    Why did I get the Debug Assert Error? and Is there a way I can Make it where I can Use A Location Pointer Without specifying the size of the array it takes? Like I was tring to do. Make it where location can point to a room made of spaces of any size. And Manipulate Location like a multi-dimensional array? Im a beginner, so I have hard time understanding code. I actually been tring to teach myself programming off and on for 4 years but Im not very good at it. Thanks for your patience and Responce
    The Brain, Thanks for your feedback and help on how to lay out my class. I actually left the methods out on purpose, until I fixed my error. I just started to make the project so I dont have more then this. Sorry.
    Thanks

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >but I didn't want to specify how much the array will be
    You have no choice if you want to point to a multidimensional array. The alternative is to dynamically allocate your own simulated array, or use a standard container class.
    My best code is written with the delete key.

  8. #8
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    but I didn't want to specify how much the array will be.
    Not a problem.. if you don't want to declare a static array with a fixed size.. just use a dynamic two dimensional array.

    Just keep in mind: int array[variable]; //is illegal in c++


    Legal: int *array = new array[variable];
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  9. #9
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    I have never seen posts come up this fast.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  10. #10
    Registered User
    Join Date
    Nov 2004
    Posts
    49
    hehe, Yea the posting is coming up pretty fast. I'm lets say not the most advance programmer in the world. Pointers and dynamic memory and stuff confuses me. And Sometimes I ask a lot of questions, and start to annoy people, but I hope that dont happen here. So You guys are saying there is no way for me to make a pointer point to an multi-dimensional array, without defining a size? And I have to use A Dynamic Array to acommplish this? I don't know if Im advance enough to do such a thing. I can do basic dynamic stuff. I understand what you posted brain. But why cant I point to a multi-dimensional array with a pointer without definfing a size? You can do that with Pointers which arent pointed to multi-dimensional arrays? Right? I ask a lot of questions cause its hard for me to learn without knowing everything. If I were to make it a dynamic array. like
    Code:
    Anthony.Location= new Space[10]; 
    for( int i=0;i<10;i++){
    Anthony.Location[i]=new Space[5];
    }
    ?
    ok I have no clue what Im doing LOL. Sorry

  11. #11
    Registered User
    Join Date
    Nov 2004
    Posts
    49
    sorry I meant
    Code:
    Anthony.Location= new Space[10]; 
    for( int i=0;i<5;i++){ // 5 not 10
    Anthony.Location[i]=new Space[5];
    }

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Close!
    Code:
    Anthony.Location = new Space*[10]; 
    for( int i=0;i<10;i++){
        Anthony.Location[i]=new Space[5];
    }

  13. #13
    Registered User
    Join Date
    Nov 2004
    Posts
    49
    DAHHHH... Lol Salem, I found that out actually before I viewed the email saying I got a reply from the Site. You beat me hehe. I was tinkering with the Code and I actually Got the same solution as you. Thanks for the solution
    Here is my new Workable Game thus far...
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    class Space
    {
    public:
    void SetDescription(string);
    string GetDescription();
    private:
    string Description;
    };
    string Space::GetDescription(){
    return Description;
    }
    void Space::SetDescription(string Description){
    this->Description=Description;
    }
    class Player
    {
    public:
    Space **Location;
    private:
    
    };
    
    
    int main(){
    Player Anthony;
    Anthony.Location= new Space*[10]; 
    for( int i=0;i<5;i++){ // 5 not 10
    Anthony.Location[i]=new Space[5];
    }
    Anthony.Location[1][3].SetDescription(" You Found A CREEPY STAIRWAY");
    cout<<Anthony.Location[1][3].GetDescription();
    
    
    system("PAUSE");
    return 0;
    }
    Thanks Everyone. I hate to leave you all. You been great. Im going to be posting a lot on here now that Im here, Cause Im sure Ill make millions more mistakes. This forume is very Helpful. THank you Kinda sad My problem was solved now Sniffle. LOL

  14. #14
    Registered User
    Join Date
    Nov 2004
    Posts
    49
    Wait I keep forgetting things. I need to review more before I post LOl.
    Code:
    Anthony.Location[1][3].SetDescription(" You Found A CREEPY STAIRWAY");
    cout<<Anthony.Location[1][3].GetDescription();
    
    delete[] Anthony.location;
    
    Anthony.location=NULL;
    is that right?

  15. #15
    Registered User
    Join Date
    Nov 2004
    Posts
    49
    I mean
    Code:
    delete[] Anthony.Location; 
    
    Anthony.Location=NULL; // forgot to cap the L on location.
    Ok Im sorry. Im posting too Much. Im going to stop LOL byebye all

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. DX - CreateDevice - D3DERR_INVALIDCALL
    By Tonto in forum Game Programming
    Replies: 3
    Last Post: 12-01-2006, 07:17 PM
  4. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 07:33 AM
  5. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM