Virtual function problem
Dec 1, 2012 at 10:10pm Dec 1, 2012 at 10:10pm UTC
Hi!
I'm about to submit my program for my course, but there must be at least 2 virtual functions in it, and whenever there's a virtual function, I get a runtime error.
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
//Jatek.h
#ifndef JATEK_H
#define JATEK_H
#include <iostream>
using namespace std;
class Jatek {
protected :
char * leiras;
public :
Jatek();
//rest of the code
};
//Jatek.cpp
#include "Jatek.h"
Jatek::Jatek() {
leiras = "nincs leiras" ;
}
//rest of the code
//Tablajatek.h
#ifndef TABLAJATEK_H
#define TABLAJATEK_H
#include "Jatek.h"
#include <iostream>
using namespace std;
class Tablajatek: public Jatek {
protected :
int n;
int ** tabla;
public :
Tablajatek();
virtual void a();
//rest of the code
};
//Tablajatek.cpp
#include "Tablajatek.h"
Tablajatek::Tablajatek(): Jatek() {
n = 8;
tabla = new int *[n];
for (int i = 0; i<n; i++) {
tabla[i] = new int [n];
}
for (int i = 0; i<n; i++) {
for (int j = 0; j<n; j++) {
tabla[i][j] = 0;
}
}
}
void Tablajatek::a() {
cout<<"hi" ;
}
//rest of the code
//Reversi.h
#ifndef OSZTALY_H
#define OSZTALY_H
#include <iostream>
#include "Tablajatek.h"
using namespace std;
class Reversi: public Tablajatek {
private :
int szabad; //szabad mezők száma
int kovetkezo;
public :
Reversi();
//rest of the code
};
//Reversi.cpp
#include "Reversi.h"
Reversi::Reversi() : Tablajatek() {
kovetkezo = 1;
szabad = 62; //8*8-4 = 62
tabla[3][3] = 1;
tabla[4][4] = 1;
tabla[3][4] = 2;
tabla[4][3] = 2;
//rest of the code
}
//main.cpp
#include <fstream>
#include "Jatek.h"
#include "Tablajatek.h"
#include "Reversi.h"
int main() {
Reversi Reverzi;
//rest of the code...
}
I tried putting in any kind of virtual function in any of the classes and it crashed at the "Reversi Reverzi;" part. Without a virtual function, it works fine.
PS: I tried putting it in without separating: like
virtual void a() {cout<<"hi" ;}
and it works, but we have to separate the code...
Any help is appreciated
Topic archived. No new replies allowed.