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;
}

I apologize for the unreadability of the post, I wrapped the compiler comments in text tags and the code in icode - which I assumed stood for "intelligent code display"..:-) What a day...

Well, my first remark is that you don't have to call a destructor explicitly, it will be automatically called when the object goes out-of-scope (this is for example when you shut down your program), so don't write: ~Question(); anywhere in your code.
Edit:: ^^
Actually this is just giving a compiler error because you don't call a destructor like that, you have to specify the object as well, for example: a.~Question(); , but remember that in general, calling a destructor manually is not recommended, and practically never needed.

Also, don't use system("pause"); , it's quite heavy on system resources, it's better to use cin.get() instead of it.
(For a complete overview about the bad things of system("pause"); , I suggest you to read the following information: http://www.gidnetwork.com/b-61.html).

These instructions aren't valid in the code of your destructor, because the arrays aren't dynamically allocated in memory:

delete[]correct;                 
delete[]answers;

Just remove these statements from your destructor.
The arrays will be cleaned up automatically (because they aren't dynamically allocated).

Unfortunately my instructor wants the destructor included in the program, so I have to figure out why it's not working.

Unfortunately my instructor wants the destructor included in the program, so I have to figure out why it's not working.

Sorry, I had edited my post, but probably you haven't seen my edit, so I'm including it here:

Edit:: ^^
Actually this is just giving a compiler error because you don't call a destructor like that, you have to specify the object as well, for example: a.~Question(); , but remember that in general, calling a destructor manually is not recommended, and practically never needed.

I get it. Thank you for your help. I have no choice but to create the arrays in my class dynamically since he wants a destructor included. All of these stupid mistakes and not even half way through the program....:-)

Did you notice your copy assignments are backwards?

void Question::setnumq(int number)  {
//    number = numq;
      numq = number;
}
void Question::setnum(int number)   {
//    number = num;
       num = number;
}
void Question::setn(int number)     {
//    number = n;
      n = number;
}

Also your correct array access is short by one.
answers[4]
correct[5]

however you are only indexing correct 0...3
for(int i =0; i<4;i++)
correct = q.correct;

>>I have no choice but to create the arrays in my class dynamically since he wants a destructor included.

That's really two different conditions. This:

Question::~Question() {}

is a valid destructor implementation assuming no dynamic memory is allocated for object variables or someplace within an object method (that isn't deleted before the destructor for that object is called). If there is any dynamic memory that hasn't be deleted by the time the destructor is called, then you should be writing code to deallocate the memory within the destructor, and any other code you want in it, too.

This article has been dead for over six months. Start a new discussion instead.