| | |
Completely Stuck on class project-- HELP!
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jul 2009
Posts: 6
Reputation:
Solved Threads: 0
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)
#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; }
•
•
Join Date: Sep 2007
Posts: 3
Reputation:
Solved Threads: 0
•
•
•
•
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)
Question()
C++ Syntax (Toggle Plain Text)
Question(string quest, string answers[], int correct[], int numquest, int num, int n)
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)
int getn() { return n; }
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)
b.enterquestion(...)
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
![]() |
Similar Threads
- Need Help in doing reservation page for class project...:P (PHP)
- How to use a class that i created within my project (Java)
- Simple EEPROM Programmer Through Parallel PC Port (also posted in Developers lounge) (Assembly)
- Simple EEPROM Programmer using Parallel Port (IT Professionals' Lounge)
- Help with class project (Java)
- Printing a sorted array. (VB.NET)
- Completely Lost : ( Can anyone help (Java)
- how can i call class in other project (C#)
Other Threads in the C++ Forum
- Previous Thread: subtraction algorithm
- Next Thread: Trouble finding enough RAM for malloc() in Vista 64?
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets





