Hi, I'm not that new to programming, but I'm not a veteran either.
First the specs.
OS: Windows 7 Home Premium 64-bit
Compiler: Dev C++ ver 4.9.9.2
Problem: In brief, I'm making a Maze-type game on the console. It's a project that contains 5 files: 3 sources, 2 headers.
Here's the main.cpp of my project:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
#include <iostream>
#include <fstream>
#include <vector>
#include <conio.h>
#include <time.h>
#include "_functions.h"
#include "_classes.h"
using namespace std;
main()
{
room Room0("resources/maps/map0/room 0.txt");
getch();
}
|
When I try to run the program, it immediately crashes. So I poked around with the debugger and found the following:
Here's the "room" class first of all (header file):
1 2 3 4 5 6 7 8 9
|
class room
{
public:
room(string file);
private:
string name, directions, event[];
char directionbuf[];
};
|
Error occurs in the constructor of the "room" class (source file):
1 2 3 4
|
room::room(string file)
{
readFile(file, DISPLAY);
}
|
("DISPLAY" is an int defined in the preprocessor directives earlier in the same file)
The "readFile" function (source file):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
|
string readFile(string filename, int control)
{
ifstream infile(filename.c_str());
string line, result;
if (infile.bad()) control=0x0;
switch (control)
{
case 0xA00:
while (infile.good())
{
getline(infile, line);
result+=(line+"#");
}
break;
case 0xA01:
while (infile.good())
{
getline(infile, line);
cout << line << endl;
}
break;
default:
cout << "Could not open file: \"" << filename << "\"";
}
infile.close();
}
|
The function runs through fine, displaying the contents of the .txt file, but once the function is finished executing, the debugger spews an error saying:
Warning: An Access Violation (Segmentation Fault) raised in your program. |
I've done a little research as to what the error was, but still don't have a good understanding as to what the error is. The code compiles fine, so I assume it's my methods of approach that are causing the error.
This is my first post on the cplusplus forums. I'm in need of constructive criticism to help me out on my post/future posts. I appreciate any help, thanks :)