Creating a package

Hey everybody,

I have this assignment where I need to program something I guess like a package, so I would be able to include the header file and run the functions from my c++ file in my main c++ file, so what I have is something like this, and it doesn't work :3

myHeaderfile.h
_____________________________________

#ifndef MYSORT
#define MYSORT

int[] bubbleSort(int[] arr);

#endif
_____________________________________
myHeaderfile.cpp
_____________________________________

#include "Sort.h"

int[] Sort::bubbleSort(int[] arr){

for (int i=1 ; i<10 ; i++)
for (int t=1 ; t<10-i ; t++)

if (arr[t] < arr [t-1]){

int temp = arr[t];
arr[t] = arr[t-1];
arr[t-1] = temp;

}

return arr;
}

_____________________________________
myMainFile.cpp
_____________________________________

#include "Sort.h"
#include "stdafx.h"
#include <iostream>
#include <math.h>

using namespace std;

int main(int argc, char** argv){

int myArray[10];

for(int j = 0; j < 10; j++){

myArray[j] = rand() % 10;
cout << myArray[j];

}

int bubbleArray[10];

bubbleArray = bubbleSort(myArray);


for(int i = 0; i < 10; i++){

bubbleArray[i] = rand() % 10;
cout << bubbleArray[i];

}

return 0;

}


__________________________
So the error I keep getting is:
1 IntelliSense: identifier "bubbleSort" is undefined c:\users\padv62090eo\documents\visual studio 2010\projects\sortingalgorithms\sortingalgorithms\sortingalgorithms.cpp 43 2 SortingAlgorithms


I hope you have an idea of what's going on, because I'm pretty lost :/
You show myHeaderfile.h, myHeaderfile.cpp, and myMainFile.cpp.
You include "Sort.h", but what is in this file?

Assuming from the compiler error and Sort::bubbleSort, Sort is a class or namespace?
Sorry, that's me messing up :3
Well Sort.h is the one which is corresponding to myHeaderfile.h
:3
Alright, try editing that in the post.

int[] Sort::bubbleSort(int[] arr)

Refers to a function which is either member of a class or inside a namespace.
You declare int[] bubbleSort(int[] arr); in myHeaderfile.h, which is in the file/global scope, so you do not need the Sort:: in front of the function.

I got it to run in C::B;

Your include hpp
int* bubbleSort(int*);

Your function cpp
1
2
3
4
5
6
7
8
9
10
11
12
int* bubbleSort(int* arr)
{
    for (int i=1 ; i<10 ; i++)
        for (int t=1 ; t<10-i ; t++)
            if (arr[t] < arr [t-1])
            {
                int temp = arr[t];
                arr[t] = arr[t-1];
                arr[t-1] = temp;
            }
    return arr;
}


Your main cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include "yourfunction.hpp"
int main()
{
    int* myArray = new int[10];

    for(int j = 0; j < 10; j++)
    {
        myArray[j] = rand() % 10;
        std::cout << myArray[j];
    }

    int* bubbleArray;

    bubbleArray = bubbleSort(myArray);


    for(int i = 0; i < 10; i++)
    {
            bubbleArray[i] = rand() % 10;
            std::cout << bubbleArray[i];
    }
    return 0;
}


Edit:

I should say, that it didn't work for me returning an undefined array size (int[]), since you have only 1 array size in your program, why not return a pointer? Note that an array is basically a pointer that has allocated x elements, just not as explicitly as we do with pointers.
Last edited on
Thanks for the reply, I tried to take another approach to it all, and I seem to have it solved somehow :D
I did more or less as you described :3 but now it gives me an error like this:
Unable to start program
missing .dll file

anything you know about? :3
Are you starting the program from your binary output folder instead of from your compiler / IDE directly?
I am not really sure what you mean by that ^_^'
But I don't have an .exe file for the project and since I can't build my program, I can't create one :3

here's the updated code ^^



myLibrary.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

#pragma once

using namespace System;

namespace myLibrary {

	public ref class Class1
	{
		#ifndef MYSORT
		#define MYSORT

		void bubbleSort(int arr[]);
		void insertionSort(int arr[]);
	
		#endif
	};
}



myLibrary.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

#include "myLibrary.h"
#include "StdAfx.h"
#include <iostream>

using namespace std;

void bubbleSort(int arr[]){

	for (int i=1 ; i<10 ; i++){

        for (int t=1 ; t<10-i ; t++){
			 
			if (arr[t] < arr [t-1]){
                    
				int temp = arr[t];
                arr[t] = arr[t-1];
                arr[t-1] = temp;
				
			}

		}

	}

	for(int j = 0; j < 10; j++){
		
		cout << arr[j] << endl;
	 
	}

}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include "myLibrary.h"
#include "stdafx.h"
#include <iostream>
#include <math.h>

using namespace std;

void main(){
	
	int myArray[10];

	for(int j = 0; j < 10; j++){
	
		myArray[j] = rand() % 10;
		cout << myArray[j];

	}

	bubbleSort(myArray);

}
Your include guard is placed wrongly, it should encapsulate the entirety of the header file. "StdAfx.h" in one file and "stdafx.h" in another. Back when I used visual studio, the pre-compiled header was named "stdafx.h", not "StdAfx.h", try renaming that.
I tried to rename it, but it didn't seem to have any effect :3
I still get the missing .dll file error :3
what about <math.h>? If you're using an up-2-date IDE I think it should be <cmath>, they did the same for other STL headers like <time.h> being renamed to <ctime>.

Also, I did not include <cmath> when I compiled the project. I think the arithmetics you are using do not utilize anything in <cmath>, so maybe it could be left out.

Try including <cstdlib> for std::rand();
Apparently it didn't matter having it or not so I guess rand(); is a built in function :3
either that or my program won't give me any errors before I get the dll thingie XD haha
When does the "dll thingie" occur? When you run or during compilation? What does it say?
it appears when I try to run the application, it pops up with the window stating that there are errors in the program, then you press continue, and it fails to build because of the missing dll file :3
apparently it should be a myLibrary.dll located in my Debug folder, however it isn't there,
could it be because I made the myLibrary.h a namespace myLibrary?
Well firstly you should put a namespace around both cpp and h content functions. Secondly, you need to call the function in main using the following syntax:

NameSpaceName::FunctionName(Parameters);


Also public ref class Class1
makes no sense to me... I have to go now tho... good luck fixing it !
Last edited on
I changed the namespace ^^
So that should work now thanks :3
but the .dll file is stil M.I.A. XD
Nevermind guys, as far as I can see it's a problem with my IDE <.<
Topic archived. No new replies allowed.