Completely Stuck on class project-- HELP!

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2009
Posts: 6
Reputation: ivanhuk is an unknown quantity at this point 
Solved Threads: 0
ivanhuk ivanhuk is offline Offline
Newbie Poster

Completely Stuck on class project-- HELP!

 
0
  #1
Jul 7th, 2009
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.

  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4.  
  5. using namespace std;
  6.  
  7. class Question {
  8. private:
  9. string quest;
  10. string answers[4];
  11. int correct[5];
  12. int numquest;
  13. int num;
  14. int n;
  15. public:
  16. void enterquestion(string&,fstream&,int&);
  17. void readanswers(string&);
  18. Question(const Question& q); // copy constructor declaration
  19. ~Question(); // destructor
  20. Question& operator=(const Question& q); // assignment operator
  21. void readfile(fstream&);
  22.  
  23. };
  24.  
  25. // Copy Constructor
  26. Question::Question (const Question& q) {
  27.  
  28. cout<<"Constructor is being created"<<endl;
  29. correct[0] = 2;
  30. correct[1] = 4;
  31. correct[2] = 3;
  32. correct[3] = 4;
  33. correct[4] = 1;
  34.  
  35. for(int i =0; i<4;i++)
  36. correct[i] = q.correct[i];
  37. numquest = q.numquest; quest = q.quest;
  38. for (int i=0; i<4; i++)
  39. answers [i]= q.answers[i]; // copy array of strings
  40. num = 0;
  41. n = 0;
  42.  
  43. }
  44.  
  45. // Assignment Operator
  46. Question& Question::operator=(const Question& q) {
  47.  
  48. cout<<"Operator equals...."<<endl;
  49. if ( this != &q) { // check for self assignment
  50. delete [] this->answers;
  51. delete [] this->correct;
  52. }
  53. for(int i =0; i<4;i++)
  54. correct[i] = q.correct[i];
  55. numquest = q.numquest; quest = q.quest;
  56. for (int i=0; i<4; i++)
  57. answers [i]= q.answers[i]; // copy array of strings
  58. num = q.num;
  59. n = 0;
  60. return *this;
  61.  
  62. }
  63. // Destructor
  64. Question::~Question() {
  65. delete [] answers;
  66. delete [] correct;
  67. }
  68.  
  69. void Question::readfile(fstream& in) {
  70. in.open("questions.txt");
  71. if (!in)
  72. cout<<"Error - cannot open file\n"<<endl;
  73. }
  74. //
  75. void Question::enterquestion (string& quest,fstream& in,int& n) {
  76.  
  77. in>>n;
  78. cout<<n;
  79. }
  80.  
  81.  
  82. ifstream in;
  83.  
  84. int main ()
  85. {
  86. // How do you properly create the object below
  87. Question b (Question);
  88. enterquestion(quest,in,n);// The Compiler gives me an error
  89. // quest and n undeclared -- even //though they are declared in the class
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96. in.close();
  97. system("pause");
  98. return 0;
  99. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 3
Reputation: Eagle-Man is an unknown quantity at this point 
Solved Threads: 0
Eagle-Man Eagle-Man is offline Offline
Newbie Poster

Re: Completely Stuck on class project-- HELP!

 
1
  #2
Jul 7th, 2009
Originally Posted by ivanhuk View Post
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
  1. Question()
where you tell it the default values, or something like
  1. 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
  1. int getn() {
  2. return n;
  3. }
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:
  1. 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.
Last edited by Eagle-Man; Jul 7th, 2009 at 9:11 pm. Reason: fixed code structure and added a point
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 6
Reputation: ivanhuk is an unknown quantity at this point 
Solved Threads: 0
ivanhuk ivanhuk is offline Offline
Newbie Poster

Re: Completely Stuck on class project-- HELP!

 
0
  #3
Jul 7th, 2009
Thank you for pointing me in the right direction. I was ready to go postal....thanks again!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC