7 Replies - 628 Views - Last Post: 28 April 2011 - 10:15 PM Rate Topic: -----

#1 Gradientpixel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 11-October 10

Sorting a dynamic array

Posted 28 April 2011 - 08:47 PM

I am having problems trying to figure out how to pass what into functions to get my dynamic array sorted.
I am not really sure how to get this accomplished.

Here is, first, my header file.
// Test Scores function specification file
#ifndef TEST_SCORE_H
#define TEST_SCORE_H

// Test Score class declaration
class TestScore
{

private:
	int *numTests;			// Holds number of tests
	double *scoreArray;		// Dynamic array for test scores
	double testAverage;		// Holds the average of tests
	double score;			// Holds a score value
	double *testPtr;		// Pointer
	scoreArray = new double[numTests];	// Array that holds test scores
	testPtr = scoreArray;	// Assign the array address
public:
	TestScore();						// Defualt constructor
	double setScore(double value);		// Stores the score value
	double getScore();					// Returns a stored scored value
	double setNumScore(int amount);		// Stores the amount of tests
	double getNumScore();				// Returns the amount of tests
	double getAverage();				// Gets the average test score
	void sort(scoreArray array[], int);	// Sorts the array
	void showScores(testPtr, int);		// Displays the array values
};
#endif



And here is the code snippet for the sorting function:
/******************************************
sort
Sorts the contents of TestScores array into
ascending order
*******************************************/
void TestScore::sort(scoreArray array[], int size)
{
	TestScore temp;		// Holds temporary object
	bool swap;

	do
	{
		swap = false;
		for(int count = 0; count < (size - 1); count++)
		{
			if(array[count].getScore() > array[count + 1].getScore())
			{
				temp = array[count];
				array[count] = array[count + 1];
				array[count + 1] = temp;
				swap = true;

			
			}
		}
	}while(swap);
}



The error I am getting for the function is that the scoreArray is not a type name. Not sure what is going on here. Any help will be appreciated a lot.
Thanks :-)

Is This A Good Question/Topic? 0
  • +

Replies To: Sorting a dynamic array

#2 jimblumberg  Icon User is online

  • member icon

Reputation: 3043
  • View blog
  • Posts: 9,278
  • Joined: 25-December 09

Re: Sorting a dynamic array

Posted 28 April 2011 - 08:52 PM

Post the complete error message just as it appears in your development environment.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1394
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Sorting a dynamic array

Posted 28 April 2011 - 09:00 PM

> void TestScore::sort(scoreArray array[], int size)

So scoreArray is a type?
Was This Post Helpful? 0
  • +
  • -

#4 Gradientpixel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 11-October 10

Re: Sorting a dynamic array

Posted 28 April 2011 - 09:00 PM

View Postjimblumberg, on 28 April 2011 - 08:52 PM, said:

Post the complete error message just as it appears in your development environment.

Jim



I am getting 28 errors in all... Here is what they are:

Error 11 error C2040: '<' : 'int' differs in levels of indirection from 'int *'
Error 15 error C2059: syntax error : ')'
Error 9 error C2061: syntax error : identifier 'array'
Error 26 error C2061: syntax error : identifier 'array'
Error 2 error C2065: 'numTests' : undeclared identifier
Error 19 error C2065: 'numTests' : undeclared identifier
Error 6 error C2065: 'scoreArray' : undeclared identifier
Error 23 error C2065: 'scoreArray' : undeclared identifier
Error 16 error C2143: syntax error : missing ';' before '{'
Error 13 error C2146: syntax error : missing ')' before identifier 'array'
Error 1 error C2327: 'TestScore::numTests' : is not a type name, static, or enumerator
Error 18 error C2327: 'TestScore::numTests' : is not a type name, static, or enumerator
Error 5 error C2327: 'TestScore::scoreArray' : is not a type name, static, or enumerator
Error 22 error C2327: 'TestScore::scoreArray' : is not a type name, static, or enumerator
Error 10 error C2446: '<' : no conversion from 'int *' to 'int'
Error 17 error C2447: '{' : missing function header (old-style formal list?)
Error 12 error C2597: illegal reference to non-static member 'TestScore::scoreArray'
Error 14 error C2761: 'void TestScore::sort(void)' : member function redeclaration not allowed
Error 4 error C2864: 'TestScore::scoreArray' : only static const integral data members can be initialized within a class
Error 21 error C2864: 'TestScore::scoreArray' : only static const integral data members can be initialized within a class
Error 8 error C2864: 'TestScore::testPtr' : only static const integral data members can be initialized within a class
Error 25 error C2864: 'TestScore::testPtr' : only static const integral data members can be initialized within a class
Error 3 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
Error 7 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
Error 20 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
Error 24 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
28 IntelliSense: member "TestScore::scoreArray" is not a type name
27 IntelliSense: operand types are incompatible ("int" and "int *")
Was This Post Helpful? 0
  • +
  • -

#5 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1394
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Sorting a dynamic array

Posted 28 April 2011 - 09:01 PM

Do you just count your errors or actually try and read and understand the error messages? I mean, when you see the error messages, how do you attempt to solve the problem?
Was This Post Helpful? 1
  • +
  • -

#6 Gradientpixel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 11-October 10

Re: Sorting a dynamic array

Posted 28 April 2011 - 09:15 PM

View PostOler1s, on 28 April 2011 - 09:01 PM, said:

Do you just count your errors or actually try and read and understand the error messages? I mean, when you see the error messages, how do you attempt to solve the problem?


No, I don't just count the errors. I have tried all kinds of ways of seeing if I can use a different way of code. Its saying that the declartion of the function is not allowed. All these errors are being created because of the dynamic array and its pointer. I know that sometimes errors are so vague and and become complete chaos over a single character. I have experianced that with a simple ; missing at the end of a class declaration. I did manage to fix a few however.
Was This Post Helpful? 0
  • +
  • -

#7 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1394
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Sorting a dynamic array

Posted 28 April 2011 - 09:29 PM

> I have tried all kinds of ways of seeing if I can use a different way of code

No, you don't just try different code. You first understand the error. Not just monkey around until the error goes away.

> Its saying that the declartion of the function is not allowed.

But that's not what it is saying at all.

> All these errors are being created because of the dynamic array and its pointer.

No, they are not.

Because you aren't actually looking and understanding the errors properly, you have built up a significant block of incorrect code. You do not blindly forge ahead in the face of errors, hoping it all goes away. It will not.

> scoreArray = new double[numTests];

This is within a class definition. How is that supposed to work? You can declare member variables. You can't have actual statements. For example:

class Foo {
 private:
  for (int i = 0; i < 2 ; ++i );
}



Can you have that? No. Because such expressions need to be within functions. Not classes.

> testPtr = scoreArray;

Again, same problem.

And both these issues were flagged by the compiler. Did you spot these issues?

> void sort(scoreArray array[], int);

Ok, how does this work? For example, int is a type. You can have:

int a;
int b[10];
a = 5;



int is a type called integer. Now look at the first parameter. scoreArray array[]. Based on the syntax in C++, that means scoreArray is a type. Can we have:

scoreArray a;
scoreArray b[10];
scoreArray = ?



No? Because if we can't, scoreArray is not a type. And if it's not a type, what is it doing there in the function argument? Hmm?

> void showScores(testPtr, int);

The same issue!

You have accumulated a mass of mistakes. Which means you either didn't bother to check your code while writing it. Or you ignored the errors as you were writing it. Either is unexcusable.

I realize errors can be confusing and less than straightforward to debug. But it seems like you don't actually investigate errors properly. And this will be your downfall. You cannot get a working program if you can't investigate issues in your own code properly...
Was This Post Helpful? 1
  • +
  • -

#8 Gradientpixel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 11-October 10

Re: Sorting a dynamic array

Posted 28 April 2011 - 10:15 PM

I can understand what you said "scoreArray = new double[numTests]; is a statement and should not belong into a class definition and also that goes for the pointer being assigned the array's address. I can see the error pointing to that line of code, however if you didnt mention that, it would be hard for me to understand.

Also I realize what you said about "void sort(scoreArray array[], int);" I honestly don't know why I was coding it like that. If that was a function just using, say an integer, I was using this:

int a;
show(a)



Instead of:

int a;
show(int)




Thank you for being a great help to me. The function error was petty considering how I really should have known that. However with the two actual statements being declared into the class definition, I would have never known. This is my first time using dynamic allocating in a program. Like you said I should not continue to code with errors. I do go and fix the ones that actually have the red line appear under but I need to code in blocks and build the program from that part to see if there are any errors.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1