I am writing a program and I need to put certain things in different .cpp and .h files but when I try to seperate certain things into .cpp files I get an error that says "error LNK2005: "struct node_2 * beginning" (?beginning@@3PAUnode_2@@A) already defined in Autocomplete.obj" and I think based on the error it has to do with "*beginning" in my Autocomplete.h file but I am not positive and I do not know how to fix it. Any help would be greatly appreciated! Here are my files..
Term.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
#ifndef TERM_H
#define TERM_H
#include <iostream>
using namespace std;
struct node
{
string text;
int number;
struct node *next;
};
class Term
{
public:
struct node* SortedMerge(node* a, node* b);
void FrontBackSplit(node* source, node** front, node** back);
void MergeSort(struct node** head);
void print(node *node, int num);
void push(node** head, string data, int num);
};
#endif
|
Autocomplete.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
#ifndef AUTOCOMPLETE_H
#define AUTOCOMPLETE_H
#include "Term.h"
struct node_2
{
string txt;
int digits;
struct node_2 *after;
struct node_2 *before;
}*beginning;
class Autocomplete
{
public:
void CreateList(string file);
void Insert(int num,string file);
void print(int results);
int sort_weight();
void search(string txt, string prefix, int num);
void Delete(string file);
};
#endif
|
Term.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 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
|
#include "Term.h"
#include <string>
void Term :: push(node** head, string data, int num)
{
node *temp = new node;
temp->text = data;
temp -> number = num;
temp->next = (*head);
(*head) = temp;
}
void Term :: MergeSort(struct node** head)
{
node* temp_head = *head;
node* a;
node* b;
if ((temp_head == NULL) || (temp_head->next == NULL))
{
return;
}
FrontBackSplit(temp_head, &a, &b);
MergeSort(&a);
MergeSort(&b);
*head = SortedMerge(a, b);
}
node* Term :: SortedMerge(struct node* a, struct node* b)
{
node* result = NULL;
if (a == NULL)
return b;
else if (b==NULL)
return a;
if (a->text <= b->text)
{
result = a;
result->next = SortedMerge(a->next, b);
}
else
{
result = b;
result->next = SortedMerge(a, b->next);
}
return result;
}
void Term :: FrontBackSplit(node* source, node** front, node** back)
{
node* fast;
node* slow;
if (source==NULL || source->next==NULL)
{
*front = source;
*back = NULL;
}
else
{
slow = source;
fast = source->next;
while (fast != NULL)
{
fast = fast->next;
if (fast != NULL)
{
slow = slow->next;
fast = fast->next;
}
}
*front = source;
*back = slow->next;
slow->next = NULL;
}
}
void Term :: print(node *node, int num)
{
int count = 0;
while (node != NULL)
{
count++;
cout<<count << " " << node->number << " " << node-> text << endl;
node = node->next;
}
}
|
Autocomplete.cpp
code]
#include "Autocomplete.h"
#include <string>
void Autocomplete :: CreateList(string file)
{
node_2 *temp;
temp = new(node_2);
temp -> txt = file;
temp -> after = NULL;
if (beginning == NULL)
{
temp -> before = NULL;
beginning = temp;
}
else
{
while (beginning -> after != NULL)
{
beginning = beginning -> after;
beginning -> after = temp;
temp -> before = beginning;
}
}
}
void Autocomplete :: Insert(int num, string file)
{
node_2 *temp, *temp_2;
temp_2 = beginning;
temp = new(node_2);
temp -> txt = file;
temp -> digits = num;
if (temp_2 -> after == NULL)
{
temp_2 -> after = temp;
temp -> after = NULL;
temp -> before = temp_2;
}
else
{
temp -> after = temp_2 -> after;
temp -> after -> before = temp;
temp_2 -> after = temp;
temp -> before = temp_2;
}
}
int Autocomplete:: sort_weight ()
{
node_2 * temphead = beginning;
int temproll;
string tempname;
int counter = 0;
while (temphead )
{
temphead = temphead->after;
counter++;
}
temphead = beginning -> after;
if (counter == 1)
{
return 1;
}
for (int j=0; j<counter; j++)
{
while (temphead->after)
{
if (temphead->digits < temphead->after->digits)
{
temproll = temphead->digits;
temphead->digits = temphead->after->digits;
temphead->after->digits = temproll;
tempname = temphead->txt;
temphead->txt = temphead->after->txt;
temphead->after->txt = tempname;
temphead = temphead->after;
}
else
temphead = temphead->after;
}
temphead = beginning;
}
}
void Autocomplete :: search(string txt, string prefix, int num)
{
Autocomplete sort;
for(int i = 1; i <= prefix.size(); i++)
{
if((prefix.substr(i - 1, 1) == txt.substr(i,1)))
{
continue;
}
else
{
return;
}
}
sort.Insert(num,txt);
}
void Autocomplete :: print(int results)
{
int line_number = 1;
node_2 *temp_2;
temp_2 = beginning;
if( temp_2->txt == "")
{
//proceed to followinf line skipping over this plan line
temp_2 = temp_2-> after;
}
//enter loop if temp_2 != null (have not reached end of file)
while (temp_2 != NULL && temp_2-> txt != "")
{
if(results <= 0)
{
return;
}
//output line number and each line in the list till you reach the end
cout << temp_2 -> digits << " " << temp_2 -> txt << endl;
//progress to next line
temp_2 = temp_2 -> after;
//increment line number
line_number += 1;
results--;
}
}
void Autocomplete :: Delete(string file)
{
////declare nodes temp and set equal to start which is declared in the class LinkedList. So it starts from beginning.
struct node_2* temp = beginning;
while(temp -> after != NULL)
{
beginning = beginning -> after;
delete temp;
temp = beginning;
}
}
[/code]
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 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 89 90 91 92 93 94 95 96
|
#include <fstream>
#include <string>
#include <ctime>
#include <chrono>
#include "Term.h"
#include "Autocomplete.h"
int main()
{
node* a = NULL;
Term txt_file;
string file;
int num;
string file_name;
ifstream infile;
int size = 0;
int largest_pos = 0;
string prefix;
int results = 0;
Autocomplete sort;
int position = 0;
sort.CreateList(file);
//ask user for file to open and store in variable
cout << "What file would you like to open: ";
getline(cin, file_name);
infile.open(file_name);
//check if file name is valid
if(infile.fail())
{
//if not display error message and end program
cout <<"The file cannot be opened, terminating program!" << endl;
system("pause");
exit(0);
}
const auto cpu_start = std::clock() ;
//use loop to read through file
while(infile >> num && (getline(infile, file)))
{
txt_file.push(&a, file, num);
//call function to insert each line in file into linked list one following the other
}
txt_file.MergeSort(&a);
const auto cpu_end = std::clock() ;
const auto cpu_milliseconds = (((cpu_end-cpu_start) * 1000.0 / CLOCKS_PER_SEC) * .001) ;
// compute elapsed real time
using namespace std::chrono ;
std::cout << endl << "Time for sorting list is " << cpu_milliseconds << " seconds.\n" << endl << endl;
txt_file.print(a, num);
while(true)
{
infile.clear();
infile.seekg(infile.beg);
cout <<"Please enter query: ";
getline(cin, prefix);
if(prefix == "Exit" || prefix == "exit")
{
cout <<"You have ended the program!" << endl;
cout <<"Thanks for using it and have a nice day!" << endl;
system("Pause");
return 0;
}
cout <<"How many results would you like to return" << endl;
cin >> results;
const auto cpu_start = std::clock() ;
while(infile >> num && (getline(infile, file)))
{
sort.search(file, prefix, num);
}
if(sort.sort_weight() == 1)
{
cin.clear();
cin.ignore();
cout << "There are no matches to your search query." << endl;
continue;
}
sort.print(results);
const auto cpu_end = std::clock() ;
const auto cpu_milliseconds = (((cpu_end-cpu_start) * 1000.0 / CLOCKS_PER_SEC) * .001) ;
// compute elapsed real time
using namespace std::chrono ;
std::cout << endl << endl << endl << "Time for searching and sorting all matched terms is " << cpu_milliseconds << " seconds.\n" << endl << endl;
sort.Delete(file);
cin.clear();
cin.ignore();
}
//txt_file.print(file_name);
system("pause");
return 0;
}
|