943,749 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 322
  • C++ RSS
Jul 7th, 2009
0

Completely Stuck on class project-- HELP!

Expand 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.

C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ivanhuk is offline Offline
7 posts
since Jul 2009
Jul 7th, 2009
1

Re: Completely Stuck on class project-- HELP!

Click to Expand / Collapse  Quote originally posted by ivanhuk ...
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
C++ Syntax (Toggle Plain Text)
  1. Question()
where you tell it the default values, or something like
C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 26
Solved Threads: 0
Newbie Poster
Eagle-Man is offline Offline
3 posts
since Sep 2007
Jul 7th, 2009
0

Re: Completely Stuck on class project-- HELP!

Thank you for pointing me in the right direction. I was ready to go postal....thanks again!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ivanhuk is offline Offline
7 posts
since Jul 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: subtraction algorithm
Next Thread in C++ Forum Timeline: Trouble finding enough RAM for malloc() in Vista 64?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC