Using arrays in member functions

Hi,

In lines 94 and 95 i'm getting an error message that says

94 :invalid conversion from `int*' to `int'
94 :initializing argument 1 of `void Country::setPopulation(int)'
95 :no matching function for call to `Country::setArea(double[20])'
note :candidates are: void Country::setArea(double)

I can't seem to figure out what the issue is, I have run the whole program without using any arrays and it worked perfectly however after adding array's i get these issues that i'm don't know how to fix, can anyone help?

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// This program implements a Rectangle class.
#include <iostream>
using namespace std;

const int MAX=20;

// Country class declaration
class Country
{
	private:
		int population;
		double area;
	public:
		void   setPopulation(int);
		void   setArea(double);
		int    getPopulation();
		double getArea();
};

// Member function implementation section

//*******************************************************************
//                     Country::setPopulation                       *
// This function sets the value of the member variable population.      *
// If the argument passed to the function is zero or greater, it is *
// copied into length. If it is negative, 1.0 is assigned to length.*
//*******************************************************************
void Country::setPopulation(int pop)
{
   if (pop >= 0)
      population = pop;
   else
   {   population = 1;
       cout << "Invalid population. Using a default value of 1.0\n";
   }
}

//*******************************************************************
//                      Country::setArea                         *
// This function sets the value of the member variable area.       *
// If the argument passed to the function is zero or greater, it is *
// copied into width. If it is negative, 1.0 is assigned to width.  *
//*******************************************************************
void Country::setArea(double a)
{
   if (a >= 0.0)
      area = a;
   else
   {   area = 1.0;
       cout << "Invalid area. Using a default value of 1.0\n";
   }
}

//****************************************************************************
//                       Country::getPopulation                              *
// getPopulation returns the value in the private member variable population.*
//****************************************************************************
int Country::getPopulation()
{
	return population;
}

//********************************************************************
//                        Country::getArea                           *
// getArea returns the value in the private member variable area.    *
//********************************************************************
double Country::getArea()
{
	return area;
}



//*******************************************************************
//                             main                                 *
//*******************************************************************
int main()
{
	Country info[MAX];         // Declare a Country object
	int num;
    int infoPop[MAX];
    double infoArea[MAX];

	//Get info population and area
	cin >> num;
	for (int i=0; i < num; i++){
        cout << "What is the population? ";
        cin  >> infoPop[MAX];
        cout << "What is the area? ";
        cin  >> infoArea[MAX];
    }
	for (int i=0; i < num; i++){       
	    // Call member functions to set info dimensions 
	    info[i].setPopulation(infoPop);
   	    info[i].setArea(infoArea);
    }
   	    
     
       

	 // Call member functions to get info to display
	 cout << "\nHere is the data:\n";
	 for (int i=0; i<num; i++){
         cout << "Population: " << info[i].getPopulation() << endl;
         cout << "Area: " << info[i].getArea()  << endl;
     }

    system("PAUSE");
	return 0;
}
Last edited on
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// This program implements a Rectangle class.
#include <iostream>
using namespace std;

const int MAX=20;

// Country class declaration
class Country
{
	private:
		int population;
		double area;
	public:
		void   setPopulation(int);
		void   setArea(double);
		int    getPopulation();
		double getArea();
};

// Member function implementation section

//*******************************************************************
//                     Country::setPopulation                       *
// This function sets the value of the member variable population.      *
// If the argument passed to the function is zero or greater, it is *
// copied into length. If it is negative, 1.0 is assigned to length.*
//*******************************************************************
void Country::setPopulation(int pop)
{
   if (pop >= 0)
      population = pop;
   else
   {   population = 1;
       cout << "Invalid population. Using a default value of 1.0\n";
   }
}

//*******************************************************************
//                      Country::setArea                         *
// This function sets the value of the member variable area.       *
// If the argument passed to the function is zero or greater, it is *
// copied into width. If it is negative, 1.0 is assigned to width.  *
//*******************************************************************
void Country::setArea(double a)
{
   if (a >= 0.0)
      area = a;
   else
   {   area = 1.0;
       cout << "Invalid area. Using a default value of 1.0\n";
   }
}

//****************************************************************************
//                       Country::getPopulation                              *
// getPopulation returns the value in the private member variable population.*
//****************************************************************************
int Country::getPopulation()
{
	return population;
}

//********************************************************************
//                        Country::getArea                           *
// getArea returns the value in the private member variable area.    *
//********************************************************************
double Country::getArea()
{
	return area;
}



//*******************************************************************
//                             main                                 *
//*******************************************************************
int main()
{
	Country info[MAX];         // Declare a Country object
	int num;
    int infoPop = MAX;      //<----
    double infoArea = MAX;    //<----

	//Get info population and area
	cin >> num;
	for (int i=0; i < num; i++){
        cout << "What is the population? ";
        cin  >> infoPop;  //<----
        cout << "What is the area? ";
        cin  >> infoArea;  //<----
    }
	for (int i=0; i < num; i++){       
	    // Call member functions to set info dimensions 
	    info[i].setPopulation(infoPop);
   	    info[i].setArea(infoArea);
    }
   	    
     
       

	 // Call member functions to get info to display
	 cout << "\nHere is the data:\n";
	 for (int i=0; i<num; i++){
         cout << "Population: " << info[i].getPopulation() << endl;
         cout << "Area: " << info[i].getArea()  << endl;
     }

    system("PAUSE");
	return 0;
}
Last edited on
94 :invalid conversion from `int*' to `int'


Line 81: You declare infoPop as an array. Line 28: You declare that setPopulation takes a single int. These are different types, hence the error.

What you probably meant at liine 84 was:
 
	    info[i].setPopulation(infoPop[i]);


Ditto for infoArea and setArea().





Topic archived. No new replies allowed.