|
-
May 6th, 2006, 01:16 PM
#1
sorting program: invalid conversion from `int' to `int*'
hello
good day!
following is my insertion sort program to sort a set of numbers ascendingly.
Code:
#include <iostream>
using namespace std;
class Sorting { //sorting class
private:
int number[];
int num[];
int sun;
public:
void setNumber(int, int, int, int);
int getNumber();
void insertionSort(int*, int);
void print();
};
void Sorting::setNumber(int n1, int n2, int n3,int n4) //setter for the set of numbers
{
int num[] = {n1, n2, n3, n4};
int *kums = num;
sun = *kums;
}
int Sorting::getNumber() //getter for the set of numbers
{
return sun;
}
void Sorting::insertionSort(int numbers[], int array_size) //insertion sort
{
int i, j, index;
for (i=1; i < array_size; i++)
{
index = numbers[i];
j = i;
while ((j > 0) && (numbers[j-1] > index))
{
numbers[j] = numbers[j-1];
j = j - 1;
}
numbers[j] = index;
}
}
void Sorting::print() // to print out the sorted set of numbers
{
for (int i=0; i<4; i++){
cout<<num[i]<<endl;
}
}
int main () {
int result;
int num1, num2, num3, num4;
Sorting a; //created an object, lets name it a
cout<<"Numbers to insert -> ";
cin>> num1>> num2>> num3>> num4; //user enters the 4 numbers
a.setNumber(num1,num2,num3,num4); // assign the numbers
a.insertionSort(a.getNumber(),4); //to sort the numbers (compilation error here)
a.print(); //print out the sort elements
system("PAUSE");
return 0;
}
could someone please copy and paste the whole source code, try to compile and help me find out the errors.
my compiler is dev C++
i got this error message: 66 invalid conversion from `int' to `int*'
a.insertionSort(a.getNumber(),4); <----- this line
i really appreciate your help...
thank you very much...
-
May 6th, 2006, 01:21 PM
#2
Re: sorting program: invalid conversion from `int' to `int*'
seems obvious to me. and what are you trieing todo with sun? al your doing is setting sun to the first number given, and you define num twice...
-
May 6th, 2006, 01:24 PM
#3
Re: sorting program: invalid conversion from `int' to `int*'
if i use sun = num; straightaway
i got another compilation error..
invalid conversion from `int*' to `int'
-
May 6th, 2006, 01:34 PM
#4
Re: sorting program: invalid conversion from `int' to `int*'
because sun. is an int, num is a pointer to an int. if you use sun = *num you set sun to the first element of num.
-
May 6th, 2006, 02:16 PM
#5
Re: sorting program: invalid conversion from `int' to `int*'
I think you could make this much simpler by just passing an existing
array to the InsertionSort() function instead of having one as a data
member of your class. This seems to work good, although I am not sure
this is the way you want it.
Code:
#include <iostream>
using namespace std;
//sorting class
class Sorting {
public:
void insertionSort(int [], int);
};
//insertion sort
void Sorting::insertionSort(int numbers[], int array_size){
int i, j, k, index;
for (i=1; i < array_size; i++)
{
index = numbers[i];
j = i;
while ((j > 0) && (numbers[j-1] > index))
{
numbers[j] = numbers[j-1];
j = j - 1;
}
numbers[j] = index;
}
//print out the array after sorting
for( k=0; k < array_size; k++ )
cout << numbers[k];
cout << endl;
}
int main () {
int result;
int num[4];
Sorting a; //created an object, lets name it a
cout<<"Numbers to insert -> ";
cin>> num[0]>> num[1]>> num[2]>> num[3]; //user enters the 4 numbers
a.insertionSort( num, 4); //to sort the numbers (compilation error here)
system("PAUSE");
return 0;
}
Alternatively, if you wanted to encapsulate data and functions into your class, you could use this. Vectors are a lot more nice to use than simple arrays 
Code:
#include <iostream>
#include <vector>
using namespace std;
class Sorting {
public:
void set();
void insertionSort();
void print();
private:
vector<int> numbers;
};
//set function
void Sorting::set( ){
int temp;
do{
cout << "Number to sort (-999 to stop) -> ";
cin >> temp;
numbers.push_back( temp );
} while( temp != -999 );
}
//insertion sort
void Sorting::insertionSort(){
int i, j, index;
for (i=1; i < numbers.size()-1; i++)
{
index = numbers[i];
j = i;
while ((j > 0) && (numbers[j-1] > index))
{
numbers[j] = numbers[j-1];
j = j - 1;
}
numbers[j] = index;
}
}
//print out sorted array
void Sorting::print(){
for( unsigned int i=0; i < numbers.size()-1; i++ )
cout << numbers[i] << " ";
cout << endl;
}
int main () {
Sorting a;
a.set();
a.insertionSort();
a.print();
system("PAUSE");
return 0;
}
Last edited by dcjr84; May 6th, 2006 at 03:42 PM.
-
May 6th, 2006, 03:37 PM
#6
Re: sorting program: invalid conversion from `int' to `int*'
Code:
int number[];
int num[];
This is non-standard (illegal) C++ syntax. This will not compile on a standard C++ compiler. Array sizes must be specified at compile time.
could someone please copy and paste the whole source code, try to compile and help me find out the errors.
my compiler is dev C++
Turn on the ANSI switch, and you will see this is an error.
Regards,
Paul McKenzie
-
May 6th, 2006, 04:25 PM
#7
Re: sorting program: invalid conversion from `int' to `int*'
i thought, int number[]; would compile as number*?
-
May 6th, 2006, 04:56 PM
#8
Re: sorting program: invalid conversion from `int' to `int*'
 Originally Posted by Mitsukai
i thought, int number[]; would compile as number*?
No.
Code:
#include <iostream>
using namespace std;
class Sorting { //sorting class
private:
int number[];
int num[];
int sun;
public:
void setNumber(int, int, int, int);
int getNumber();
void insertionSort(int*, int);
void print();
};
Comeau C/C++ 4.3.3 (Aug 6 2003 15:13:37) for ONLINE_EVALUATION_BETA1
Copyright 1988-2003 Comeau Computing. All rights reserved.
MODE:strict errors C++
"ComeauTest.c", line 7: error: incomplete type is not allowed
int number[];
^
"ComeauTest.c", line 8: error: incomplete type is not allowed
int num[];
^
2 errors detected in the compilation of "ComeauTest.c".
Regards,
Paul McKenzie
-
May 7th, 2006, 06:58 AM
#9
Re: sorting program: invalid conversion from `int' to `int*'
There are two ways when you can define an array without giving its size:
1. When it is initialised. eg
Code:
char text[] = "hello world";
2. When it is a function parameter, in which case it does convert to a pointer:
Code:
int main( int argc, char* argv[] );
argv becomes char**.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|