Solved

# Stupid Question: Easy 20 Points

Posted on 2004-11-18
290 Views
Okay, im a huge retard and in my incredible tiredness cannot figure out why this wont work....

#include <stdlib.h>
#include <iostream.h>

class Sorts
{

public:

void bubbleSort(int a[], int length)
{
int top, i, temp;

for (top = 0; top < length - 1; top++)
{
for (i = length - 1; i > top; i--)
{
if (a[i] < a[i-1])
{
temp = a[i];
a[i] = a[i-1];
a[i-1] = temp;
}
}
}
}
};

#include <stdlib.h>
#include <iostream.h>
#include "Sorts.cpp"

main()
{
int a[] = {1,9,3,5,2,8,6};

for (int x = 0; x <= 6; x++)
{
cout << a[x];
}

bubbleSort (a , 7);
}

The problem is when i try to compile my driver. I get an error that says:
"Driver.cpp:14: implicit declaration of function `int bubbleSort(...)'"

Thanks for the help.
0
Question by:MexicanHeat

LVL 2

Expert Comment

Try rearranging:

#include <stdlib.h>
#include <iostream.h>

to

#include <iostream.h>
#include <stdlib.h>
0

LVL 2

Accepted Solution

Also you need to use the functions in Class Sorts;

Try this way:

#include <iostream.h>
#include <stdlib.h>

main()
{
int a[] = {1,9,3,5,2,8,6};

for (int x = 0; x <= 6; x++)
{
cout << a[x];
}
Sorts s;

s.bubbleSort (a , 7);
}
0

LVL 4

Expert Comment

You dont need to put bubbleSort in a class. Compile it this way:

#include <stdlib.h>
#include <iostream.h>

void bubbleSort(int a[], int length)
{
int top, i, temp;

for (top = 0; top < length - 1; top++)
{
for (i = length - 1; i > top; i--)
{
if (a[i] < a[i-1])
{
temp = a[i];
a[i] = a[i-1];
a[i-1] = temp;
}
}
}
}

#include <stdlib.h>
#include <iostream.h>
#include "Sorts.cpp"

main()
{
int a[] = {1,9,3,5,2,8,6};

for (int x = 0; x <= 6; x++)
{
cout << a[x];
}

bubbleSort (a , 7);
}
0

Author Comment

Like i said....im stupid and im tired. Thanks pb. I appreciate it.
0

Author Comment

Yep, thanks for that flippy. Whats gonna end up happening is that im going to be testing all types of sorts and the code in my driver is going to get kind of long so i figured it would be easier to seperate the sorts. thanks for the help guys.
0

LVL 4

Expert Comment

I dont really see any point in putting bubble sort in a class (maybe you are used to Java?). Classes are for when you spawn multiple instances of a type of objects. Classes makes handling compex data types easy by automating some things, and encapsulating them nicely. Bubble sort is simply a function you perform on a list.
0

Author Comment

You hit it right on the head flippy. I've only ever done Java...and all of a sudden my class is requiring us to program in C++. I am completely lost in this language and am having a hell of a time figuring out how to do things. I do appreciate the help flippy.
0

## Featured Post

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.