Hey everyone. I'm brand new to C++ and programming in general and this is my first post here. I've found lots of good info here and these forums helped me a lot last semester. I've got a problem here though. This is a small bit of code I had to turn in yesterday, but for the life of me I couldn't understand why VC++ kept giving me compile errors but g++ compiled it just fine. Here it is:

/*CSIS 297
 * Chapter 10 Challenge 5: Pie a la Mode
 */

#include <iostream>
#include <algorithm>
using namespace std;


/***********************************************
 *                                             *
 * 			  Function Prototype			   *
 * 											   *
 ***********************************************/
int pieSort(int*, const &int);
	
	
/***********************************************
 *                                             *
 * 				     MAIN					   *
 * 											   *
 ***********************************************/
int main()
{
	int mode;
	const int SIZE = 31;
	int *piesPtr = new int[SIZE];
	
	cout << "This program will accept the number of pies eaten by\n"
		 << "30 individuals, calculate and display the mode.\n" << endl;
	
	for (int count = 1; count < SIZE; count++) {
		cout << "Person #" << count << ": ";
		cin >> *(piesPtr + count);
	}
	
	 mode = pieSort(piesPtr, SIZE);
	 
	 delete [] piesPtr;
	 piesPtr = 0;
	 
	 cout << "Mode = " << mode << endl;
	
	
	return 0;
}

/**************************************************
 *            pieSort() function:                 *
 * sorts pie slices eaten from lowest to highest  *
 * and returns the mode.                          *
 **************************************************/
int pieSort(int *p, const int &S) {
	
	int m = 0;
	int modeCounter[S];  // This is where the compiler in VC++ complains
	modeCounter[1] = 0;  // It wants a const value for S.
	
	sort( (p + 1), (p + S) );
	
	for (int count = 1; count < S; count ++) {
		cout << *(p + count ) << endl;
	}
	
	for (int index = 1; index < 31; index++) {
		for (int count = index; count < 31; count++) {
			if ( *(p + count) == *(p + count +1) ) {
				modeCounter[count] += 1;
			}
			if (modeCounter[count] > m) {
				m = *(p + count);
			}
		}
	}
	
	return m;
}

No matter what I do, whether it's being passed as a pointer, by reference, or just copy it simply won't compile in Visual Studio but it works great with Geany and g++. Why does the "const int" assignment to SIZE not transfer when passing the value to the function? And yes, I'm sure my code is a mess. Any and all criticism is welcome. Thanks!

I think that

int pieSort(int*, const &int);

should be

int pieSort(int *, const int &);

on line 15.

Sorry, you're right, that was a typo. I was pretty tired when I posted this last night... I changed it to:

int pieSort(int *, const int &);

but the compiler is still giving me this:

------ Build started: Project: Chapter10_Challenge5, Configuration: Debug Win32 ------
  Chapter10-Challenge5.cpp
c:\users\user\documents\school\csis 297\assignments\chapter 10\chapter10_challenge5\chapter10-challenge5.cpp(57): error C2057: expected constant expression
c:\users\user\documents\school\csis 297\assignments\chapter 10\chapter10_challenge5\chapter10-challenge5.cpp(57): error C2466: cannot allocate an array of constant size 0
c:\users\user\documents\school\csis 297\assignments\chapter 10\chapter10_challenge5\chapter10-challenge5.cpp(57): error C2133: 'modeCounter' : unknown size
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

I even tried just passing by copy and removing the '&' but it still complains. :confused:
The only way I can get it to work in the current configuration is to actually enter a number (31) where S should go on line 56:

int modeCounter[31];

Edited 5 Years Ago by strungoutfan78: n/a

I can't remember, but do you have to dereference a built in type when you pass it like that? So:

int modeCounter[*S];

If not, did you try something like:

int pieSort(int *p, const int &S) {
 
	int m = 0;
        int temp = S;
	int modeCounter[temp];  // This is where the compiler in VC++ complains
	modeCounter[1] = 0;  // It wants a const value for S.

Edited 5 Years Ago by ravenous: n/a

You have to assign a static const variable in order to be accepted as an array size.Try using a template function and pass it a constant integral when calling it.

template <int size> int* func ()  { //example
   int array[size];
   return array;
 }

You shouldn't need to dereference when passing by reference, only when it's a pointer.

It seems peculiar but it could be a quirk of VC, especially if it compiles fine under other compilers. Does it run fine after you compile it in g++?

Variable sized arrays are not supported in ISO C++ (enabling warning high enough in g++ will produce this error as well).
You can use the solution posted by caut_baia to get what you want or you can use an enum

enum { SIZE = 10 };
int foo () {
   int array[SIZE];
   // ...
}

Of course, you could always just use a vector.

Edited 5 Years Ago by L7Sqr: n/a

You have to assign a static const variable in order to be accepted as an array size.Try using a template function and pass it a constant integral when calling it.

template <int size> int* func ()  { //example
   int array[size];
   return array;
 }

Don't do that. That code is undefined since you are returning a pointer to a variable that
will be destroyed at the end of its scope.


@OP you would want to use either a container or use dynamic memory allocation, for the variable modeCounter. In fact, just changed this line int modeCounter[S]; to this std::vector<int> modeCounter(S,0) and it should all work fine.

Don't do that. That code is undefined since you are returning a pointer to a variable that
will be destroyed at the end of its scope.


@OP you would want to use either a container or use dynamic memory allocation, for the variable modeCounter. In fact, just changed this line int modeCounter[S]; to this std::vector<int> modeCounter(S,0) and it should all work fine.

Yes, that works very well. Thank you. Now that that is fixed I see my horrible logic errors. At least I can work ahead now. Thanks so much. I'm not very familiar with vectors, but the syntax is pretty much the same as arrays right? And as far as templates end enums our instructor hasn't even touched on those so I'm pretty sure he doesn't want us using them.

This question has already been answered. Start a new discussion instead.