'noOfRegions' was not declared in this scope

I am trying to compile an example program from Malik chapter 10: Election Results and I am getting the error below:

4 0 C:\Dev-Cpp\MalikChapter10\main.cpp In file included from main.cpp
52 20 C:\Dev-Cpp\MalikChapter10\candidateType.h [Error] 'noOfRegions' was not declared in this scope
C:\Dev-Cpp\MalikChapter10\main.cpp In function 'int main()':
32 16 C:\Dev-Cpp\MalikChapter10\main.cpp [Error] 'class orderedArrayListType<candidateType>' has no member named 'selectionSort'
42 15 C:\Dev-Cpp\MalikChapter10\main.cpp [Error] 'printHeading' was not declared in this scope
28 C:\Dev-Cpp\MalikChapter10\Makefile.win recipe for target 'main.o' failed


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
 #ifndef CANDIDATETYPE_H
#define CANDIDATETYPE_H

#include "personType.h"

class candidateType : public personType
{
public:
	const candidateType& operator=(const candidateType&) ;
		//Overload the assignment operator for the objects of the type candidateType
		
	const candidateType& operator=(const personType&);
		//Overload the assignment operator for the object so that the value 
		//of an object of the type personType can be assigned to an object of the type candidateType
		
	void setVotes(int region, int votes);
		//Function to set the votes of a candidate for a particular region.
		//Postcondition: The votes specified by the paramenter votes
		//              are assigned to the region specified by the parameter region
	void updateVotesByRegion(int region, int votes);
		//Function to update the votes of a candidate for a particular region
		//Postcondition: The votes specified by the parameter votes are added to the region
		//specified by the parameter region.
	void calculateTotalVotes();
		//Function to calculate the total votes received by a candidate
		//Postcondition: The votes received in each region are added.
		
	int getTotalVotes();
		//Function to return the total votes received by a candidate.
		//Postcondition: The total votes received by the candidate are returned
		
	void printData() const;
		//Function to output the candidate's name, the votes received
		//in each region, and the total votes received.
//	void printResults(string cNames[], 
  //                int votesByRegion[][noOfRegions],
    //              int totalVotes[], int noOfRows);	
	candidateType();
		//default constructor
		//Postcondition: Initialize the votes received in each region,
		//              and the total votes received , to zero.
		
		//Overload the relational operators.
	bool operator==(const candidateType& right) const;
	bool operator!=(const candidateType& right) const;
	bool operator<=(const candidateType& right) const;
	bool operator<(const candidateType& right) const;
	bool operator>=(const candidateType& right) const;
	bool operator>(const candidateType& right) const;
	
private:
	int votesByRegion[noOfRegions];
	int totalVotes; 
};

#endif 


Please help.
It's hard to say. That header won't compile on its own. It requires (at least) the header

#include "personType.h"

Possibly there are other headers needed too?
Definitely something is missing, but it isn't clear what. without the full context.
Hello Bopaki,

First this int votesByRegion[noOfRegions]; is not allowed and does not work. At compile time what is between the [] brackets needs to be a constant number so the compiler can know how much space to set aside for this array. This can be done with const int NOOFREGIONS{ 5 }; or the newer constexpr int NOOFREGIONS{ 5 }; or even #define NOOFREGIONS 5 .

By your error message it looks like "noOfRegions" is not defined anywhere before it is used in the class.

Hope that helps,

Andy
Thank you both Chervil and Handy Andy.

I only had to add in this line:

 
const int noOfRegions = 4;


and everything worked fine.
Topic archived. No new replies allowed.