mergesort.obj : error LNK2005: "void __cdecl X_mergesort(int * const,int

Hello world. This is my first post so I hope I do it right. I am working on a mergesort with linked list and had to code it in a certain way. I was given a LinkedList.cpp, LinkedList.h, a mergeSort(for an array), and a LinkedList_test.cpp.

I don't need help with the algorithms but I do need help with this error.

It will run once on MS visual studio but then it seems to remember the last run and gives me the following errors

LINK : warning LNK4076: invalid incremental status file 'C:\Users\Zachary\Documents\Visual Studio 2008\Projects\lab9V2\Debug\lab9V2.ilk'; linking nonincrementally

My_MergeSort.obj : error LNK2005: "void __cdecl My_Merge(class LinkedList,int,int,int)" (?My_Merge@@YAXVLinkedList@@HHH@Z) already defined in linkedList_test.obj

1>My_MergeSort.obj : error LNK2005: "void __cdecl My_MergeSort(class LinkedList,int,int)" (?My_MergeSort@@YAXVLinkedList@@HH@Z) already defined in linkedList_test.obj

1>C:\Users\Zachary\Documents\Visual Studio 2008\Projects\lab9V2\Debug\lab9V2.exe : fatal error LNK1169: one or more multiply defined symbols found

What does this error mean and how should I fix it?

Thank you again for reading.

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
 *  linkList_test.cpp
 *  
 *
 *  Created by Enkh-Amgalan Baatarjav on 3/3/10.
 *
 */

//#include "LinkedList.h"

#include <iostream>
#include <string>
#include <fstream>
#include "My_MergeSort.cpp"

//using namespace std;



int main () {
	LinkedList list = LinkedList ();
	list.putAtEnd (12);
	list.putAtEnd (13);
	list.putAtEnd (15);
	list.putAtEnd (65);
	list.putAtEnd (7);
	list.putAtBeginning (7);
	list.insertItemAt (4, 77);
	
	//list.~LinkedList ();  //was commented out
	list.display ();
	cout << "Item at 4: " << list.getItemAt (4) << endl;
	
	list.removeAt (7);
	list.display ();
	cout << endl;
	
//	cout << "Item at 4: " << list.getItemAt (4) << endl;

	//read in information.
	LinkedList list2 = LinkedList ();
	
	ifstream mazefile("data_Lab9.txt"); // create the stream variable for file
//	int i=0;
	//int grid[99999];

	if (!mazefile) { // variable will be zero if error
		cout << "Could not open the file " <<" hw4_mazemap.txt "<< " for reading.\n";
		exit(1); // exit the program with error code of one 
		return 1;

	}
/*
	while ((mazefile.getline(grid[i], MAXCOLSIZE)) && (i<MAXROWSIZE)) {
		cout << grid[i] << "\n"; // echo the input
		i++;
		maxrow=i;
	}*/
	int loc =0;
	
	char data[10];
	if (mazefile.is_open())	//open file
	{
	while(!mazefile.eof()){
		mazefile.getline(data,10);
		int in;
		in = atoi(data);
		list2.insertItemAt(loc, in);
		loc++;

	}


	}

	//create list. 

	list2.display ();
	//My_MergeSort(list2, list2.getItemAt (0), list2.getItemAt(list2.getSize()));


	list2.display ();
	int look;
	cin>>look;

	
	return 0;
}


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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include "LinkedList.h"


/** Merges two sorted array segments theArray[first..mid] and
 *  theArray[mid+1..last] into one sorted array.
 * @pre first <= mid <= last. The subarrays theArray[first..mid]
 * and theArray[mid+1..last] are each sorted in increasing order.
 * @post theArray[first..last] is sorted.
 * @param theArray  The given array.
 * @param first  The beginning of the first segment in theArray.
 * @param mid  The end of the first segement in theArray.  mid + 1
 *        marks the beginning of the second segment.
 * @param last  The last element in the second segment in theArray.
 * @note This function merges the two subarrays into a temporary
 * array and copies the result into the original array theArray. */
void My_Merge(LinkedList theArray,
           int first, int mid, int last)
{
   //DataType tempArray[MAX_SIZE];    // temporary array
   LinkedList tempArray;

   // initialize the local indexes to indicate the subarrays
   int first1 = first;       // beginning of first subarray
   int last1  = mid;         // end of first subarray
   int first2 = mid + 1;     // beginning of second subarray
   int last2  = last;        // end of second subarray

   // while both subarrays are not empty, copy the
   // smaller item into the temporary array
   int index = first1;    // next available location in
                          // tempArray
   for (; (first1 <= last1) && (first2 <= last2); ++index)
   {  // Invariant: tempArray[first..index-1] is in order
//      if ((theArray.getNodeAt(first1)).item < (theArray.getNodeAt(first2)).item)
	  if ((theArray.getItemAt(first1)) < (theArray.getItemAt(first2)) )
      {  //tempArray[index] = theArray[first1];
		  tempArray.insertItemAt(index, theArray.getItemAt(first1));
         ++first1;
      }
      else
      { // tempArray[index] = theArray[first2];
	  tempArray.insertItemAt(index, theArray.getItemAt(first2));
         ++first2;
      }  // end if
   }  // end for

   // finish off the nonempty subarray

   // finish off the first subarray, if necessary
   for (; first1 <= last1; ++first1, ++index){
      // Invariant: tempArray[first..index-1] is in order
     // tempArray[index] = theArray[first1];
		tempArray.insertItemAt(index, theArray.getItemAt(first1));
   }

   // finish off the second subarray, if necessary
   for (; first2 <= last2; ++first2, ++index){
      // Invariant: tempArray[first..index-1] is in order
      //tempArray[index] = theArray[first2];
	 tempArray.insertItemAt(index, theArray.getItemAt(first2));
   }

   // copy the result back into the original array
   for (index = first; index <= last; ++index)
    //  theArray[index] = tempArray[index];
	tempArray.insertItemAt(index, theArray.getItemAt(index));
}  // end merge

/** Sorts the items in an array into ascending order.
 * @pre theArray[first..last] is an array.
 * @post theArray[first..last] is sorted in ascending order.
 * @param theArray  The given array.
 * @param first  The first element to consider in theArray.
 * @param last  The last element to consider in theArray. */
void My_MergeSort(LinkedList theArray, int first, int last)
{
   if (first < last)
   {  // sort each half
      int mid = (first + last)/2;    // index of midpoint
      // sort left half theArray[first..mid]
      My_MergeSort(theArray, first, mid);
      // sort right half theArray[mid+1..last]
      My_MergeSort(theArray, mid+1, last);

      // merge the two halves
      My_Merge(theArray, first, mid, last);
   }  // end if
}  // end mergesort 


Also tell me if you want to see the LinkedList.cpp or .h files.

Thanks again.
Also if I rename the functions in the My_MergeSort.cpp It runs fine for one build but after that it complains again. Does anyone know what is wrong or could at least push me in the right direction? I have been hitting my head against this problem for about 8 hours now. I don't even know if my mergeSort works yet.

Thanks again for reading.
I guess problem is you're inserting .cpp file in your source.
Try this ;
In your header file keep only prototypes ( not any implementation )
And in source file make all implementations.
Int main include the header file.

What i mean is this ;

A.h shall include only prototypes
1
2
3
4
5
6
..
void  funcA(void);
int funcB(void);
double func(int num);
...
..


A.cpp shall include declerations of those functions in A.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
...
void funcA(void)
{
   ..
}
int funcB(void)
{
   ..
}
double func(int num)
{
   ..
}
...


And in main include only header.

main.cpp
1
2
3
4
5
6
7
8
9
10
11
#include "A.h"

int main()
{
 ..
...
A();
value = B();
...
return 0;
}


Hope this help.
Topic archived. No new replies allowed.