Error With Double Definitions Resulting from a Struct
Nov 29, 2017 at 6:09am UTC
Hello, I've recently been doing a modified version of my previous address book, but in a more simplified version. I have gotten the program to work when in one main.cpp, but upon splitting it up into a header and two cpp files, I run into variations of the same error. The error is:
main.obj : error LNK2005: "struct Person * person" (?person@@3PAUPerson@@A) already defined in addressBook.obj
No matter what I do, I run into similar or the same error, so I'm wondering what do I need to do to allow it to properly compile. Here is my 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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
#include "addressBook.h"
#include <iostream>
using namespace std;
int main()
{
char last[20];
char choice;
Person add;
Person getStruct;
cout << "Address Book" << endl << endl;
while (true )
{
cout << endl << " Enter 1 to add to the address book. " << endl;
cout << " Enter 2 to get a person. " << endl;
cout << " Enter 3 to find a person by last name only." << endl;
cout << " Enter 4 to print Book." << endl;
cout << " Enter any other key to quit. " << endl;
cout << endl << "Please make a selection: " << endl;
cin >> choice;
switch (choice)
{
case '1' :
{
cout << "Enter First Name" << endl;
cin >> add.firstname;
cout << "Enter Last Name" << endl;
cin >> add.lastname;
cout << "Enter Address" << endl;
cin.clear();
cin.ignore(255, '\n' );
cin.getline(add.address, 40);
addPerson(add);
continue ;
}
case '2' :
{
cout << endl << "Getting the next person in the address book: " << endl << endl;
getPerson(getStruct);
printStruct(getStruct);
continue ;
}
case '3' :
{
cout << "Please enter the last name to search for: " << endl;
cin >> last;
if (findPerson(last, add))
printStruct(add);
else
cout << "Sorry could not find the name." << endl;
continue ;
}
case '4' :
{
printBook();
continue ;
}
default :
{
return 0;
break ;
}
}
cin.clear();
cin.ignore(255, '\n' );
}
return 0;
}
addressBook.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 97 98 99 100 101
#include <iostream>
#include "addressBook.h"
int head = 0;
int tail = -1;
using namespace std;
void printStruct(struct Person &print)
{
cout << print.firstname << " " << print.lastname << endl;
cout << print.address << endl;
}
bool addPerson(const Person p)
{
if (head < MAXIMUM)
{
person[head] = p;
head++;
if (tail == -1)
tail++;
return true ;
}
return false ;
}
bool getPerson(Person &p)
{
if (tail >= 0)
{
if (tail >= MAXIMUM)
tail = 0;
p = person[tail];
tail++;
return true ;
}
return false ;
}
bool findPerson(char *lName, Person &p)
{
for (int i = 0; i < head; i++)
{
if (person[i].lastname, lName)
{
p = person[i];
return true ;
}
}
return false ;
}
void printBook()
{
for (int i = 0; i < head; i++)
{
cout << person[i].firstname << "\t" << person[i].lastname << "\t" << person[i].address << endl;
}
}
addressBook.h
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
#pragma once
#ifndef addressBook
#define addressBook
#include <string>
const int MAXIMUM = 10;
struct Person
{
char firstname[20];
char lastname[20];
char address[50];
};
Person person[MAXIMUM];
void printStruct(Person &print);
bool addPerson(Person p);
bool getPerson(Person &p);
bool findPerson(char *lName, Person &p);
void printBook();
#endif
Last edited on Nov 29, 2017 at 6:10am UTC
Nov 29, 2017 at 3:57pm UTC
You shall not define variables within headers. If global variables are really required declare them with extern in the header and put the definition in a cpp.
Topic archived. No new replies allowed.