String or int as part of variable name

1
2
3
4
5
int n = 9;
while (n > 0)
{ 
Question???.ask ();
};

Can I replace ??? with the value of n to avoid having to write out Question.ask a bunch of times?
You could if you had an array of Questions. :-)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
const int NUM_QUESTIONS = 9;
Question questions[NUM_QUESTIONS];

int n = NUM_QUESTIONS;

while( --n )
{
   questions[n].ask();
}

// Or as a for loop...
for( int i=0l i < NUM_QUESTIONS; ++i )
{
   questions[n].ask();
}
Thank you. I'm probably making a really stupid mistake but I keep getting these errors:
error C2065: 'Question' : undeclared identifier
error C2146: syntax error : missing ';' before identifier 'questions'
error C2065: 'questions' : undeclared identifier
error C2228: left of '.ask' must have class/struct/union

you need to actually write a Question class.

(That is, if you are following iHutch's suggestion).
I did
1
2
3
4
5
6
7
8
9
10
class Question
{
private:
string stn;
string opt1;
string opt2;
string opt3;
public:
void setValues (string, string, string, string);
void ask ();};
Last edited on
You need a semicolon after the closing brace of a class definition.
Yeah, I just didn't copy and paste it it seems. That doesn't really solve my problem though
Last edited on
And you're including the header file that contains the class definition in your source file?
Yes, I think so.
1
2
3
4
#include <iostream>
#include <conio.h>
#include <windows.h>
#include <string> 
Those are all standard headers (either standard C++ headers, or 3rd-party headers relating to the platform you're using).

I don't see any include statement for a header file that would contain the definition of Question.
Oh.. I feel really stupid now.. What would that include statement look like?
You have to declare the class before you can refer to it, so if you put the class in the same file as the example code above then it needs to go above line 2 which is where you refer to it.

if you put the class in a separate file then you need to include that file..

#include "my-file-that-contains-question"

Note that the include uses "" not <> because it is a file in your project not a system file.
Is this not considered declaring the class?
1
2
3
4
5
6
7
8
9
10
class Question
{
private:
string stn;
string opt1;
string opt2;
string opt3;
public:
void setValues (string, string, string, string);
void ask ();};


Is this not considered declaring the class?

Yes. But what file have you put that code in? Is it in the same source file as the one that's failing to compile? Is it in a separate header file? Somewhere else?
Last edited on
The same file
Then whatever the problem is, it isn't in any of the code you've shown us.
I was getting a feeling some time ago that the simplest way to get to the heart of this would be to post the complete source code which is giving rise to the errors, rather than just individual snippets.
I will be torn to pieces for this, but guess what? A very thorough search has revealed the problem to be a typo.
I'd like to thank you all for your help and apologize for having wasted your time. I should have found that error before posting.
No worries - glad you managed to find the problem.
Topic archived. No new replies allowed.