I've created a class and create two new objects from that class. 3 variables are stored as private. I have mutator and access functions for these variables. When I try to write a value through these functions, they don't seem to change the variable in the class. What am I doing wrong? Also, the Destructor when it's not commented out gives me a compiler error - [TEX]142 C:\Documents and Settings\Ivan\My Documents\pro4.cpp no match for 'operator~' in '~Question()' [/TEX][TEX] note C:\Dev-Cpp\include\c++\3.4.2\bits\ios_base.h:83 candidates are: std::_Ios_Fmtflags std::operator~(std::_Ios_Fmtflags) note C:\Dev-Cpp\include\c++\3.4.2\bits\ios_base.h:83 std::_Ios_Openmode std::operator~(std::_Ios_Openmode) [/TEX]. I call delete to delete the two arrays that were created, but this error pops up. I've posted the entire code - Any help or nudge in the right direction is much appreciated.
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
using namespace std;
class Question {
private:
int numq;
int num;
int n;
public:
string quest;
string answers[4];
int correct[5];
Question (); // default constructor
Question(const Question& q); // copy constructor declaration
~Question(); // destructor
Question& operator=(const Question& q); // assignment operator
string getquest();
int getnumq();
int getnum();
int getn();
void setnumq(int);
void setnum(int);
void setn(int);
void enterquestion(string&,fstream&,int&);
void readanswers(string&);
void displaymessage();
};
// accessor functions for class variables
int Question::getnumq () {
return numq; }
int Question::getnum() {
return num; }
int Question::getn() {
return n; }
// mutator functions for class variables
void Question::setnumq(int number) {
number = numq;
}
void Question::setnum(int number) {
number = num;
}
void Question::setn(int number) {
number = n;
}
// Default Constructor
Question::Question() {
// cout<<"Constructor is being created"<<endl;
quest = "a";
answers[0] = "a";
correct[0] = 2;
correct[1] = 4;
correct[2] = 3;
correct[3] = 4;
correct[4] = 1;
numq = 0;
num = 0;
n = 0;
}
// Copy Constructor
Question::Question (const Question& q) {
// cout<<"\nCopying Constructor is working...."<<endl;
quest = q.quest;
for (int i=0; i<4; i++)
answers [i]= q.answers[i]; // copy array of strings
for(int i =0; i<4;i++)
correct[i] = q.correct[i];
numq = q.numq;
num = q.num;
n = q.n;
}
// Assignment Operator
Question& Question::operator=(const Question& q) {
if (this!= &q)
{
cout<<"\nOperator equals working...."<<endl;
quest = q.quest;
for (int i=0; i<4; i++)
answers [i]= q.answers[i]; // copy array of strings
for(int i =0; i<4;i++)
correct[i] = q.correct[i];
// numq = q.numq;
num = q.num;
n = q.n;
}
return *this;
}
// Destructor
Question::~Question() {
// delete[]correct;
// delete[]answers;
}
void Question::enterquestion (string& quest,fstream& in,int& n) {
in>>n;
cout<<n;
}
void Question::displaymessage() {
cout<<"\nEnter in the Answer that you think is correct for the question."<<endl<<endl;
}
int main ()
{
int n2,num2,numq2;
ifstream in;
in.open("questions.txt");
if (!in)
cout<<"Error - cannot open file\n"<<endl;
Question a, b; // Create two instances of class Question
vector<Question>arr(5); // Create an array of objects
arr[0] = a; // Set fields in array[o] = fields in class (later will iterate through object)
b.displaymessage(); // Can display a void member function
cout<<"Enter a number ";
cin>>n2;
b.setn(n2); // Call mutator to set private n var in class
num2 = b.getn(); // get set value in private n and copy into local for printing
// print out the new variable to if it is set -- var does not set what i do wrong?
cout<<endl<<num2;
// ~Question(); //"No match for operator~ in ~Question
system("pause");
in.close();
return 0;
}