Thread: What was wrong with my program?

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    22

    What was wrong with my program?

    So, I just spent the last 3 or 4 hours fixing a bug in a program of mine. The program would compile, but then immediately crash as soon as it started running, with no explanation. The compiler gave no description of the error, since it compiled. But although I finally fixed the problem, I still don't really understand what was wrong in the first place.

    So, the program was a Real Time Strategy game Ive been working on. It has a class called unit, which represents individual units on the battlefield, and another class unitType, which stores unchanging stats and data on different unit types. The nightmare started when I decided to write a new constructor for unitType that would read the data in from a txt file.

    Class declaration in header:
    Code:
    class unitType
    {
       private:
          // lots of data members...
       public:
          unitType(int id = 0, std::string path =NULL);
          // some other functions...
    }
    The full function in source file:
    Code:
    unitType::unitType(int id, std::string path)
    {
       ifstream afile(path);
       // read in some data...
       afile.close()
    }
    In the Main function I did this:
    Code:
    int main()
    {
       // .....
       unitType theTypes[20];
       theTypes[0] = unitType(0, "unitData.txt");
       // .....
    }
    And, in the declaration of the unit class:
    Code:
    class unit
    {
       private:
          unitType type;
          // ....
       public:
          unit(int x, int y, unitType theType);
          // ...
    }
    The reason for the default arguments to the unitType constructor is that the line where I declare an array of 20 unitTypes would not compile unless there was a constructor that would take zero arguments.

    The code would compile, but instantly crash. I finally fixed it by completely eliminating the "std::string path" argument to the unitType constructor, and just putting "unitData.txt" directly into the ifstream command in the constructor.

    My theory is that when I declared the array, the constructor was called with the default argument of NULL, and then ifstream was called with a NULL argument. But the thing is, when I changed ifstream(path) to ifstream("unitData.txt"), it still wouldn't work. Only once I completely removed the std::string path argument from the constructor would it not crash.

    I have it fixed now of course, and I really don't need to take in path as an argument, I just did that out of habit I guess. But I was curious exactly what the problem was after the hours I spent fixing it.

    Thanks.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Instead of this:
    Code:
    unitType(int id = 0, std::string path =NULL);
    you probably intended to write this:
    Code:
    unitType(int id = 0, const std::string& path = "");
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Maze Program - What am I doing wrong?
    By Gipionocheiyort in forum C++ Programming
    Replies: 20
    Last Post: 08-02-2007, 01:31 PM
  2. Replies: 5
    Last Post: 01-13-2007, 02:14 AM
  3. What is wrong with my code? My first program......
    By coreyt1111 in forum C++ Programming
    Replies: 11
    Last Post: 11-14-2006, 02:03 PM
  4. Whats wrong with this program - Output of a series
    By duffmckagan in forum C Programming
    Replies: 2
    Last Post: 07-26-2006, 09:57 AM
  5. Whats wrong with my program?
    By Ruflano in forum C++ Programming
    Replies: 5
    Last Post: 02-21-2002, 05:09 PM