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

Recommended Answers

All 2 Replies

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.

First of all, your class needs at least a default constructor. Something to tell the computer how to make a question and what default values to use. It could be something like

Question()

where you tell it the default values, or something like

Question(string quest, string answers[], int correct[], int numquest, int num, int n)

where the programmer provides all the default values, or something in between.

Second of all, I think you're getting your Copy Constructor and Constructor mixed up. The Copy Constructor should only be used when you are copying one Question to another, not when you create a new instance. It looks like you've got a mash up of your Constructor and Copy Constructor.

Third, quest and n are not defined in your program. They are private variables in the class, and cannot be accessed except by the code for the class. If you want access to them, you have to write functions in your class to do so, such as

int getn() {
  return n;
}

which would then be accessed through your Questions variable.

Which brings me to my next point: enterquestion is a function in the Question class. It cannot be accessed as you're doing it. It should be accessed like so:

b.enterquestion(...)

All member functions need to be accessed through a variable of the class using the . operator.

Finally, you never initialize your fstream in main. What file is it reading? I see you initialize it in the readfile function in the class, but then there's no point to passing an fstream as an argument if you don't use it.

If you fix these 5 things, it should run. I think you should reread your notes on classes.

commented: Good, well-written, concise advice. +18

Thank you for pointing me in the right direction. I was ready to go postal....thanks again!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.