Help, am a c++ student who is completely stuck. I know that there are some stupid errors in this, I've been working all day on this and I still can't find them. I'm supposed to create a class, copy constructor, and than create an instance in main and then create an array of objects from this class. I can't even get the constructor to instantiate, and the function called from main doesn't recognize any of the variables declared in the class. What am I doing wrong? Please help.
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
class Question {
private:
string quest;
string answers[4];
int correct[5];
int numquest;
int num;
int n;
public:
void enterquestion(string&,fstream&,int&);
void readanswers(string&);
Question(const Question& q); // copy constructor declaration
~Question(); // destructor
Question& operator=(const Question& q); // assignment operator
void readfile(fstream&);
};
// Copy Constructor
Question::Question (const Question& q) {
cout<<"Constructor is being created"<<endl;
correct[0] = 2;
correct[1] = 4;
correct[2] = 3;
correct[3] = 4;
correct[4] = 1;
for(int i =0; i<4;i++)
correct[i] = q.correct[i];
numquest = q.numquest; quest = q.quest;
for (int i=0; i<4; i++)
answers [i]= q.answers[i]; // copy array of strings
num = 0;
n = 0;
}
// Assignment Operator
Question& Question::operator=(const Question& q) {
cout<<"Operator equals...."<<endl;
if ( this != &q) { // check for self assignment
delete [] this->answers;
delete [] this->correct;
}
for(int i =0; i<4;i++)
correct[i] = q.correct[i];
numquest = q.numquest; quest = q.quest;
for (int i=0; i<4; i++)
answers [i]= q.answers[i]; // copy array of strings
num = q.num;
n = 0;
return *this;
}
// Destructor
Question::~Question() {
delete [] answers;
delete [] correct;
}
void Question::readfile(fstream& in) {
in.open("questions.txt");
if (!in)
cout<<"Error - cannot open file\n"<<endl;
}
//
void Question::enterquestion (string& quest,fstream& in,int& n) {
in>>n;
cout<<n;
}
ifstream in;
int main ()
{
// How do you properly create the object below
Question b (Question);
enterquestion(quest,in,n);// The Compiler gives me an error
// quest and n undeclared -- even //though they are declared in the class
in.close();
system("pause");
return 0;
}