-
April 24th, 2010, 08:16 PM
#1
error LNK2005
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?
Also it seems if I change the names of the functions in My_MergeSort it works for one build but then comlains again(the errors above) after a 2nd build
Thank you again for reading.
/*
* 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;
}
**
//mergeSort.cpp
#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.
-
April 24th, 2010, 11:23 PM
#2
Re: error LNK2005
You have multiple link failures after a link warning about your incremental linker settings being messed up... so address the warning and I suspect the failures will go away.
Let me google that for you real quick... k.
http://msdn.microsoft.com/en-us/libr...fh(VS.71).aspx
-
April 24th, 2010, 11:41 PM
#3
Re: error LNK2005
Please use code tags when posting code, to preserve indenting.
In this case, however, the problem is obvious: It's almost always wrong to #include a .cpp file.
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
|