Multiple Functions Definition Errors - c++

I have a header, and a cpp file I was attempting to build.
.cpp file:
#include "SelectionSort.h"
void SelectionSort::Fill(){
Buffer = new char[Size];
for(int i=0;i<Size;i++){
Buffer[i] = rand() % 10;
}
}
void SelectionSort::PrintOut(){
for(int i=0;i<Size;i++){
cout<<Buffer[i]<<endl;
}
}
void SelectionSort::Sort(){
int lowest;
for(int i=0;i<Size;i++){
lowest=i;
for(int j=i;j<(Size-i);++j)
if(Buffer[j]>lowest) lowest = j;
swap(Buffer[lowest], Buffer[i]);
}
}
.h file:
#ifndef SELECTIONSORT_H
#define SELECTIONSORT_H
#include <algorithm>
#include <stdlib.h>
#include <iostream>
using namespace std;
class SelectionSort {
public:
SelectionSort();
SelectionSort(int S){Size= S;}
void Fill();
void PrintOut();
void Sort();
private:
int Size;
char * Buffer;
};
#endif /* SELECTIONSORT_H */
But I get these errors:
SelectionSort.cpp:17: multiple definition of 'SelectionSort::PrintOut()'
SelectionSort.cpp:17: first defined here
SelectionSort.cpp:23: multiple definition of 'SelectionSort::Sort()'
SelectionSort.cpp:23: first defined here
SelectionSort.cpp:10: multiple definition of 'SelectionSort::Fill()'
SelectionSort.cpp:10: first defined here,
How am I defining my functions incorrectly?
I am using netbeans and their generic make/build settings. I've been meaning to get more into make files, should i try to write my own and solve the problem?

Once you get your code to compile, you have a number of logical mistakes (see comments):
void SelectionSort::Sort()
{
int lowest;
for(int i = 0; i < Size; i++)
{
lowest = i;
for(int j = i; j < (Size - i); ++j) // j should terminate at the end of Buffer, not one before the end
if(Buffer[j] > lowest) lowest = j; // comparing a data element to an index, comparison operator reversed
swap(Buffer[lowest], Buffer[i]);
}
}

Related

Visual studio C programming function defenition

The program below is to sort number in ascending order using a function. It is written in Visual Studio.
I know that I have made a mistake in function declaration as I have declared int LinearSort(); above main and not inside main.
The program executes without error but sorting does not happen since the function is not called.
The program is saved as C++.
Can anyone help me to call the function and do sorting by editing program below?
Main file
#include <stdio.h>
#include <stdafx.h>
#include "sort.h"
#include <conio.h>
#include <iostream>
int LinearSort();
int main()
{
int sort[50];
int i=0;
int j=0;
int k=0;
int a = 0;
printf("Enter 10 Numbers");
for ( i = 0; i < 10; i++ )
{
scanf_s("%d",&sort[i]);
}
for ( i = 0; i < 10; i++ )
{
printf("%d\n",sort[i]);
}
return 0;
}
.C file
#include "stdafx.h"
#include "sort.h"
#include <stdio.h>
#include <conio.h>
#include <iostream>
void LinearSort(int i, int j, int k, int a, int sort[])
{
for ( j=0; j < i-1; j++ )
{
for ( k=0; k < i-j-1; k++ )
{
if(sort[k] < sort[k+1])
{
a = sort[k];
sort[k] = sort[k+1];
sort[k+1] = a;
}
else
{
sort[j] = sort[j];
}
}
}
for ( j = 0; j < i; j++ )
{
printf("ascending %d\n",sort[j]);
}
_getch();
}
Header file
#pragma once
#include <stdio.h>
extern void LinearSort(int i, int j, int k, int a, int sort[]);
You are on the right way and your code needs only a little tweeking. Others have given valuable suggestions, most of which I won't repeat.
First the definition of your LinearSort(). You are passing a number of variables (i..k) that we call local variables and that should not be passed. Local variables are only used by your function and are declared inside the function. The proper definition now becomes:
void LinearSort(int a, int sort[]); // prototype; put in header file or above main
void LinearSort(int a, int sort[]) // function itself
{
int i, j, k; // local variables
Then you must call it from your main, after you read all the data. Call it like:
LinearSort(10, sort[]);
You passs 10 for a because you read a fixed number of integers; would you have read an arbitrary number (but less than 50), you would have passed a variable with this amount.
For your information: you pass the array variable sort[] but note that this name sort is the same in your main and in your function. There is no need and this just coincidence.
As for the sort algorithm, it seems based on bubble sort but uses for-loops. That is at least unusual and probaby wrong; the outer loop must go as many times as is needed until no more elements have been exchanged; however, a for loop normally executes a fixed number of times, so you see why this is probably wrong. I suggest you read about bubble sort.
First, remove this statement
int LinearSort();
As you have already declared the function in header file and header is included in all files.
you should call LinearSort() with proper argument in your main() , after scanf() . And in your LinearSort() function, like this
int main()
{
int sort[50];
int i=0;
int j=0;
int k=0;
int a = 0;
printf("Enter 10 Numbers");
for ( i = 0; i < 10; i++ )
{
scanf_s("%d",&sort[i]);
}
LinearSort(i, j, k, a, sort);
for ( i = 0; i < 10; i++ )
{
printf("%d\n",sort[i]);
}
return 0;
}
And in LinearSort(), inside for loop,i don't see any specific use of else statement.

How can you print a dynamic array in C++?

Below is my current code for my latest assignment and I cannot figure out what the problem is printing the array. Forgive me for the crappy code, in my class we were thrown into C++ and none of us have ever used it before so it may be a simple mistake but no one in the house can help me.
Header file DynamicArray.h:
//
// DynamicArray.h
///#include <rpcndr.h>
#ifndef DYNAMIC_DYNAMICARRAY_H
#define DYNAMIC_DYNAMICARRAY_H
#endif //DYNAMIC_DYNAMICARRAY_H
// union
// intersection
// relative complement
// insertion - if the element is already in the set, then nothing happens
// deletion - if the element is not in the set, then nothing happens
// query to check whether an element is in a set
// query to find the number of number of elements in a set
// display the set
//destructor
// copy constructor
// ***********************************overloading assignment operator***************************************************
class DynamicArray{
public:
DynamicArray(int size);
DynamicArray(const DynamicArray &original, int Size);
/// DynamicArray(int Size);
~DynamicArray();
void Union();
void Intersection();
void Complement();
int Insert(int position, int entry, int size);
int Delete(int position, int entry, int size);
bool Qelement(int size, int entry);
int Qset(int size);
int size = 20;
int *array;
};
//
//
//
Source file DynamicA.cpp- here I define the constructors and member functions:
//
// DynamicA.cpp
//
//Union();
//Intersection();
//Complement();
//Insert();
//Delete();
//Qelement();
//Qset();
#include <iostream>
#include "DynamicArray.h"
using namespace std;
DynamicArray::DynamicArray(int &size = 30){
size = 20;
*array = new int[size];
for(int i = 0; i < size; i++){
array[i] = 0;
};
}
/// DynamicArray::DynamicArray(int Size) {
///
/// }
DynamicArray::DynamicArray(const DynamicArray &original, int size) {
size = original.size;
array = new int[size];
for (int i = 0; i < size; i++) {
array[i] = original.array[i];
}
}
DynamicArray::~DynamicArray(){
delete[] array;
}
void DynamicArray::Union(){
}
void DynamicArray::Intersection() {
}
void DynamicArray::Complement(){
}
int DynamicArray::Insert(int position, int entry, int size) {
if(!Qelement()){
for(int i = size+1; i > position+1; i--){
array[i] = array[i-1];
}
array[position] = entry;
}
}
int DynamicArray::Delete(int position, int entry, int size){
for(int i = 0; i < size; i++){
if(array[i] == entry) {
for(int x = i; x < size; i++){
array[x] = array[x+1];
}
size--;
}
}
}
bool DynamicArray::Qelement(int size, int entry) {
for(int i = 0; i < size; i++){
if(array[i] == entry){
return true;
}
}
}
int DynamicArray::Qset(int size){
return size;
}
main.cpp - this is where my issue is. The error I continue to receive is that dArray is not an array.
//main.cpp
#include <iostream>
//#include <DynamicArray.h>
#include "DynamicArray.h"
//#include "DynamicA.cpp"
//using namespace std;
int main() {
DynamicArray dArray();
for(int i = 0; i < array; i++) {
cout << dArray[i];
}
}
Your class DynamicArray is not an array, the compiler has it right. It's just a class you've defined. For your code to work, you need to overload DynamicArray::operator[](int), for example, like so:
#include <cassert>
int DynamicArray::operator[](int idx)
{
assert(idx < size);
return array[i];
}
The reason is that operator[] is only defined for the built-in array type, where it has an established meaning and understood by the compiler. But you have defined an arbitrary class, and your understanding that it is an array is only your understanding, i.e. an assumption, which in no way is perceived by the compiler, so to say.
Now, let me point this one out before you run into issues caused by that mistake: the fields size and array must be private or at least protected! Read up on encapsulation in C++, as well as the other two or three founding principles of this language. You may wonder how to access the size of the array form the outside given this change, well that's where the so-called getter-setter methods come into play. Define int DynamicArray::size() const { return size; } to let the array tell its size to its clients.
Now you can use the previously defined operator[](int) with int size():
DynamicArray myArray(5);
for(int i = 0; i < myArray.size(); ++i)
std::cout << myArray[i] << " ";
Other errors: two pointed out by#crashmstr: *array = new int[size]; should be array = new int[size]; and DynamicArray myArray(); isn't going to build, since this calls the undefined default constructor.

Invalid types int[int] for array subscript error

I keep receiving the error listed above for my methods. Here is the relevant code:
BubbleSort<int> bs;
analysis << "Working with Bubble Sort with " << size << " random elements\n";
start = clock();
for(int i=0;i<=size;i++)
{
bs.sort(array[size], 1+(rand() )); --> error
}
Here is my header file:
#ifndef BUBBLESORT_H_
#define BUBBLESORT_H_
#include <iostream>
using namespace std;
template <class Type>
class BubbleSort
{
private:
public:
int array[], size;
Type sort(Type array[], Type size);
};
template<class Type>
Type
BubbleSort<Type>::sort(Type array[], Type size)
{
int i, j, flag = 1;
int num_cmps = 0;
int temp;
for (i = 1; (i <= size) && flag; i++)
{
flag = 0;
for (j = 0; j < (size - 1); j++)
{
if(array[j+1] < array[j])
{
temp = array[j];
++num_cmps;
array[j] = array[j+1];
++num_cmps;
array[j+1] = temp;
++num_cmps;
flag = 1;
++num_cmps;
}
}
}
}
#endif
Previously I had asked the user for the number of elements and then cin >> size;. I thought this would make size constant, which is what my research has told me is the problem with this error. Any help would be greatly appreciated.
Problem 1
In BubbleSort, instead of
Type sort(Type array[], Type size);
You need something like:
Type sort(Type array[], size_t size);
Problem 2
When you call the function,
array[size] evaluates to an int, not an array. Use:
bs.sort(array, size);
bs.sort(array[size], 1+(rand() ));
The function sort requires an array, but you are passing the item in array at index size (possibly out of bound)

c++ print/ build 2d array

I'm trying to build and print a 2d array but its showing up as empty when I try to print it out so there's an error somewhere but I cant find it. Can someone help? I added the code for initializing the array.
#ifndef MAZE_HPP_
#define MAZE_HPP_
#include <fstream>
#include <vector>
#include <string>
class Maze
{
public:
Maze(int size);
~Maze() {}
enum Direction { DOWN, RIGHT, UP, LEFT };
// Implement the following functions:
// read maze from file, find starting location
void readFromFile(std::ifstream &f);
// make a single step advancing toward the exit
void step();
// return true if the maze exit has been reached, false otherwise
bool atExit();
// set row and col to current position of 'x'
void getCurrentPosition(int &row, int &col);
//print function
void printMaze();
// You can add more functions if you like
private:
// Private data and methods
int size, rowX, colY;
char matrix[30][30];
};
#endif /* MAZE_HPP_ */
void Maze::readFromFile(std::ifstream &f) {
std::string line;
int i, j;
getline(f, line);
for(i = 0; i < size; i++) {
getline(f, line);
for(j = 0; j < size; j++) {
matrix[j][i] = line[j];
}
}
f.close();
}
void Maze::printMaze() {
int i, j;
for(i = 0; i < size; i++) {
for(j = 0; j < size; j++) {
std::cout << matrix[i][j] << "";
std::cout << "line";
}
std::cout << std::endl;
}
}
The initialization does not matter here although it is good practice. Back to the problem: there can be several reasons
1. You have passed zero or negative number for size in the constructor when you create a Maze object.
2. You have passed positive number for size but forgot to assign it to the size variable in the constructor.
If it enters into the loop in print function and shows the string "line" size times then it means it can not read anything from file.
It would be helpful if you send full code or at least the constructor.

Can't fill the array that is a member of a class. C++

#include <iostream>
using namespace std;
class SomeClass {
public:
bool someArray[4][4]={{0,0,0,0},{0,0,0,0}};
};
int main()
{
SomeClass super;
super.someArray={{1,1,1,0},{1,0,0,1}}; //This goes red, indicates a mistake. How do i properly fill it?
for (int i=0;i<4;i++){
for (int j=0;j<4;j++){
cout<<super.someArray[i][j];
}
cout<<endl;
}
return 0;
}
Please see the comments in the code above.
By the way: super.someArray[4][4]={{1,1,1,0},{1,0,0,1}}; doesn't work either and it probably shouldn't.
You probably mean to use bool someArray[2][4] (i.e, an array with two elements that contains arrays with four boolean elements).
You can't assign one array into another in C++; you'll need to copy the individual elements. I.e., something like:
super.someArray[0][0] = 1;
super.someArray[0][1] = 1;
super.someArray[0][2] = 1;
super.someArray[0][3] = 0;
super.someArray[1][0] = 1;
super.someArray[1][1] = 0;
super.someArray[1][2] = 0;
super.someArray[1][3] = 1;
(If you have some source for your data, you could use a loop of course.)
The following worked for me using the GNU compiler. Notice that I replaced your raw array with std::tr1::array. This class is more flexible with respect to assigning entire arrays (as opposed to just initializing arrays from literals).
#include <iostream>
#include <tr1/array>
using namespace std;
using namespace tr1;
typedef array<array<bool,4>,4> array4x4;
class SomeClass {
public:
array4x4 someArray;
SomeClass() : someArray((array4x4){{{{0,0,0,0}},{{0,0,0,0}}}}) {}
};
int main()
{
SomeClass super;
super.someArray=(array4x4){{{{1,1,1,0}},{{1,0,0,1}}}}; //Now works
for (int i=0;i<4;i++){
for (int j=0;j<4;j++){
cout<<super.someArray[i][j];
}
cout<<endl;
}
return 0;
}
However, the following approach is a bit closer to where you started, and demonstrates some of the things suggested in other comments...
#include <iostream>
#include <algorithm>
using namespace std;
class SomeClass {
public:
bool someArray[4][4];
SomeClass()
{
bool temp[4][4] = {{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0}};
for ( int j=0; j<4; j++ ) for ( int i=0; i<4; i++ ) someArray[j][i] = temp[j][i];
}
};
int main()
{
SomeClass super;
bool temp[4][4] = {{1,1,1,0},{1,0,0,1}}; // a local source of data
for ( int j=0; j<4; j++ ) for ( int i=0; i<4; i++ ) super.someArray[j][i] = temp[j][i];
for (int i=0;i<4;i++){
for (int j=0;j<4;j++){
cout<<super.someArray[i][j];
}
cout<<endl;
}
return 0;
}
super.someArray[4][4]={{1,1,1,0},{1,0,0,1}};
The line above just needs to be:
super.someArray[4][4]={1,1,1,0,1,0,0,1};
Explaination:
it will automatically go the next section of the array. If you think of it as a table, once the first row is filled up, it will start declaring it for the next row.
So if you wrote:
super.someArray[4][4]={1,1,1,1,1};
it would set:
someArray[0][0]
someArray[0][1]
someArray[0][2]
someArray[0][3]
someArray[1][0]
all equal to 1.
(I might have the numbers switched so it could be x and y places are changed, I can't recall for c++)