Yes, I've searched high and low for the answer but none seemed to be for this particular problem. The compiler says that ReadFile does not name a type while it is obviously defined in the header file. Why is it doing this? I am practicing inheritance with the file stream classes, making the class able to store integers or characters in files. Integers/character arrays are separated by a %% in the file. display.h contains the class declarations and prototypes and display.cpp contains the definition and main.
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
|
//display.h
#ifndef DISPLAY_H
#define DISPLAY_H
#include <iosfwd>
#include <fstream>
class ReadFile: public std::ifstream{
private:
ReadFile(const ReadFile& r){}
public:
ReadFile(const char* fileName);
ReadFile& operator>>(int& param);
ReadFile& operator>>(char *c);
};
class OutFile: public std::ofstream{
private:
OutFile(const OutFile& o){}
public:
OutFile(const char* fileName);
OutFile& operator<<(const int& i);
OutFile& operator<<(const char* c);
};
class Sprite{
private:
char* m_sprite;
int m_width;
int m_height;
public:
Sprite(const char* sprite, int width, int height);
};
#endif
|
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 108 109 110 111 112 113 114
|
//display.cpp
#include "display.h"
#include <iostream>
#include <fstream>
using std::ifstream;
ReadFile::ReadFile(const char* fileName){
ifstream::open(fileName);
if(ifstream::fail()){
throw "Issue reading file";
}
}
ReadFile& ReadFile::operator>>(int& param){
/*r is simply for holding the extracted characters
*/
char r[16];
r[0] = ifstream::get();
for(int i = 1; r[i] != '%' && r[i-1] != '%'; ++i){
if(i == 16){
throw "%% trigger not found";
}
r[i] = ifstream::get();
}
int temp = 0;
int streamsize = 1;
for(int count = 1; r[count] != '%'; ++count){
streamsize = streamsize * 10;
if(r[count] == '%')
r[count] = '\0';
}
for(int count = 0; r[count]; ++count){
if(int(r[count]) < 48 || int(r[count]) > 57)
throw "Invalid conversion to integer";
temp += ( static_cast<int>(r[count]) - 48 ) * streamsize;
streamsize /= 10;
}
param = temp;
return *this;
}
ReadFile& ReadFile::operator>>(char* c){
c[0] = ifstream::get();
for(int i = 1; i < 32767; ++i){
c[i] = ifstream::get();
if(c[i] == '%' && c[i-1] == '%'){
c[i-1] = '\0';
c[i] = '\0';
break;
}
}
return *this;
}
using std::ofstream;
OutFile::OutFile(const char* fileName){
ofstream::open(fileName);
if(ofstream::fail())
throw "Cannot write to file";
}
OutFile& OutFile::operator<<(const int& i){
for(int digit = 100000000; digit > 0; digit /= 10)
ofstream::operator<<( static_cast<char>(48 + i / digit) );
ofstream::operator<<( "%%");
return *this;
}
OutFile& OutFile::operator<<(const char* c){
ofstream::operator<<(c);
ofstream::operator<<("%%");
return *this;
}
Sprite::Sprite(const char* sprite, int width, int height){
m_width = width;
m_height = height;
m_sprite = new char[width * height + 1];
for(int i = 0; sprite[i]; ++i)
m_sprite[i] = sprite[i];
}
int main(){
OutFile editSprite("sprite.dat");
editSprite << " + +++ + ";
editSprite.close();
OutFile numbers("numbers.txt");
numbers << 5423 << 343;
numbers.close();
ReadFile read("numbers.txt");
int data;
read >> data;
std::cout << data << std::endl;
read >> data;
std::cout << data << std::endl;
std::cin.get();
return 0;
}
|
whenever I try to compile it, I get the following errors:
'ReadFile' does not name a type
ReadFile& ReadFile::operator>>(int& param){
^
'ReadFile' does not name a type
ReadFile& ReadFile::operator>>(char* c){
^
In function 'int main()':
expected ';' before 'read'
ReadFile read("numbers.txt");
^
'read' was not declared in this scope
read >> data;
^