size function not working.

I'm using a code in which I have to access the size of an array and I'm using the format arrayName.size(), but the compiler is giving me an error whenever I try to do so. What might be the problem here? I'm using DevC++.

Also, I was reading a book in which the type of variable was size_t instead of int or char. Does any such type exist?

1
2
  	for (int answer = 0; answer < responses.size(); ++answer)
		++frequency [ responses[answer]];
closed account (48T7M4Gy)
It depends on what you mean by array, however, this might be worth having a look at:
http://www.cplusplus.com/faq/sequences/arrays/sizeof-array/

size_t exists, checkout the online search on this site.
Last edited on
What error do you get from the compiler?

What might be the problem here?

Did you declare responses?
What type does responses contain?
What type does frequency contain?

Try it like this:
++(frequency [ responses[answer]]);

Also, I was reading a book in which the type of variable was size_t instead of int or char. Does any such type exist?

Yes it does, size_t is the return type of all size-methods of the STL
It is basically an unsigned integer, but the MAX_SIZE may be different than that of an unsigned integer
[Error] request for member 'size' in 'responses', which is of non-class type 'unsigned int [20]'

^This is the error given.
Yes, responses is declared and initialized. It's an int type array with 20 elements.
Frequency is also an int type array with 6 elements and is initialized.

I have also used the <iomanip> library.


The error is in the for loop condition, not the statement below it.
closed account (48T7M4Gy)
try for ( size_t answer = 0; etc

the "size" (number of elements) of a c-style int array is sizeof(arrayName)/sizeof(int)
Last edited on
If that doesn't work, try declaring answer as unsigned int.
The problem still persists. Should I post the whole code here? That might make the problem clearer.
[Error] request for member 'size' in 'responses', which is of non-class type 'unsigned int [20]'

Normal arrays are no classes and therefore have no methods and therefore have no method called size()

You could use the C++ class std::array instead:
1
2
3
4
5
#include <array>

// ...

std::array<unsigned int, 20> responses;
closed account (48T7M4Gy)
go for it :)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
	const int responseSize = 20;
	const int frequencySize = 6;
	
	unsigned int responses[responseSize] = {1, 2, 5, 4, 3 , 5, 2, 1, 3, 1, 4,3,3,3,2,3,3,2,2,5};
	
	unsigned int frequency [frequencySize] = {};
	
	for (size_t answer = 0; answer < (sizeof(responses) - 1); ++answer)
		++frequency [ responses[answer]];
		
	cout << "Rating" << setw(17) << "Frequency" << endl;
	
	for (size_t rating = 1; rating < (sizeof(frequency) - 1); ++rating)
		cout << setw(6) << rating << setw(17) << frequency [rating] << endl;

return 0;
}


This fixes the compilation problem, but the program is crashing now. Is it because I'm trying to access the index of an array which exceed the dimensions of that array?

Why do I have to use the sizeof function and not just the size here?
Last edited on
As far as I think, updating the variables answer and rating would eventually lead to trying accessing the elements beyond the dimensions of the arrays. For that purposes, I used

 
(sizeof(frequency) - 1)


so that the range isn't violated.
Why do I have to use the sizeof function and not just the size here?

C-Arrays don't have a size function, that's why

sizeof gives you the amount of bytes of your object
you need to divide by the size of an integer, otherwise you get an out of range error:
sizeof(responses) / sizeof(int)

Alternatively use std::array as I posted above
Why are you trying to calculate the size of the array when you know it already?
closed account (48T7M4Gy)
1
2
for (size_t answer = 0; answer < sizeof(responses)/sizeof(int); ++answer)
		++frequency[responses[answer]];


and it runs! There you go.

Dev C++ is crap. Why not use codeblocks, Visual Studio, or xcode. They're all free and have a degree of modernity about them. :)
Last edited on
So in essence, whenever I have to use the
 
sizeof(array) 

function, I have to divide by the
 
sizeof(int)

function? That means that this function doesn't actually access the number of variables in the array, but accesses the total memory allocated to the array and by dividing with the sizeof an integer, I'm indirectly finding out how many int are there in my variable?

Does the
 
arrayName.size()

function work in the same manner for a C++ array or does it tell you the number of elements?

Also, how do I differentiate between a C++ array and a C array?


I read this program from a book and wanted to check it's working. It was written as it is in the book.
Last edited on
For some reason, codeblocks doesn't function properly. I'll try switching to Visual Studio then. Thanks!
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
	//const int responseSize = 20; <-----------------------
	const int frequencySize = 6;

	unsigned int responses[] = { 1, 2, 5, 4, 3 , 5, 2, 1, 3, 1, 4,3,3,3,2,3,3,2,2,5 };

	unsigned int frequency[frequencySize] = {};

	for (size_t answer = 0; answer < sizeof(responses)/sizeof(int); ++answer)
		++frequency[responses[answer]];

	cout << "Rating" << setw(17) << "Frequency" << endl;

	for (size_t rating = 1; rating < (sizeof(frequency)/sizeof(int)); ++rating)
		cout << setw(6) << rating << setw(17) << frequency[rating] << endl;
	int x;
	cin >> x;
	return 0;
}


You had a couple of bloopers and I think this fixes it up. Its because there are two lines with size problems. Interestingly you don't need to know the size hence working it out becomes valid even though the earlier comment is technically right as your code stood at the time.

PS VStudio is pretty good. Codeblocks is good bu it has a problem with stdio if I remember which is a real pain. Xcode is the best but VS is a great windows solution. :)
Last edited on
Alright. Thank you so much!
Topic archived. No new replies allowed.