I'm fairly new to c++ and currently trying to learn templates and how they work. My assignment asks me to create a parent class, base, that takes 4 arguments into a template. As well it must have a subclass that can print the values of the base class.
The parent class gets the values by reading a txt file.
Right now I'm trying to instantiate a subclass so that I can print the values line by line in the parent class.
My code is messy as I am VERY new to c++. My background consists of C#, Java and Javascript. Any help would be fantastic! Thanks in advance.
Error Message:
Error 1 error C2248: 'std::basic_ifstream<_Elem,_Traits>::basic_ifstream' : cannot access private member declared in class 'std::basic_ifstream<_Elem,_Traits>'
Code:
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <cctype>
using namespace std;
//template that accept 4 different data types
template<class Type1, class Type2, class Type3, class Type4>
class Base {
//protected declarations
protected:
Type1 str;
Type2 i;
Type3 d;
Type4 b;
//public declarations
public:
int lineRead;
int data;
string line, delimiter;
size_t pos;
ifstream inputfile;
string token;
//public functions
public:
void readFile(int lineNum) {
//temp information holders
lineRead = 0;
pos = 0;
delimiter = " ";
//open .txt file
inputfile.open("file.txt");
//while end of file is not reached
while (!inputfile.eof()) {
//increment line counter
lineRead++;
//read line
getline(inputfile,line);
//if this is the line requested fill the template members with the
//correct data. <string, int, double, bool>
if(lineNum == lineRead){
//getting the string
pos = line.find(delimiter);
token = line.substr(0, pos);
str = token;
line.erase(0, pos + delimiter.length());
//getting the integer
pos = line.find(delimiter);
token = line.substr(0, pos);
i = stoi(token);
line.erase(0, pos + delimiter.length());
//getting the double
pos = line.find(delimiter);
token = line.substr(0, pos);
d = stod(token);
line.erase(0, pos + delimiter.length());
//getting the boolean
pos = line.find(delimiter);
token = line.substr(0, pos);
b = to_bool(token);
line.erase(0, pos + delimiter.length());
}
}
//close the file
inputfile.close();
system("pause");
};
//Changes a string to lower case and then reads the string
//to find if the value is true or false. Returns a boolean.
bool to_bool(string str) {
transform(str.begin(), str.end(), str.begin(), ::tolower);
istringstream is(str);
bool tempB;
is >> boolalpha >> tempB;
return tempB;
}
};
class Subclass : public Base<string, int, double, bool> {
private:
string name;
public:
Subclass::Subclass(string name) {
cout << "test";
}
void printValues() {
cout << str;
}
};
//main function
int main() {
//call Base class and give the template data types
Subclass b = Subclass("test");
//read the file
b.readFile(2);
return 0;
}
Subclass b = Subclass("test");
This creates an object, and then tries to copy it. Your class is not copyable, hence the error (also, copying derived classes is problematic in its own way). To create an object without copying, use the following syntax:
Subclass b("test");