? multi-definition error of a class?
Sep 29, 2010 at 7:47pm UTC
so i'm testing some of my code with a basic Address Class first. It runs fine as long as it's in one file but when i try to split it into headers i get this multiple definition of 'Address::getAddr(char *, char *, char *, char *, char*)'
and the same error for the Address::printAddress()
if anyone would be so kind as to help me out i would be very thankful...i've never ran into this problem before. i will post my header and .cpp file and my test main below
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
// address.h
#ifndef ADDRESS_H_EXISTS
#define ADDRESS_H_EXISTS
#include <iostream>
#include <cstring>
using namespace std;
class Address
{
private :
char address_line_1[50];
char address_line_2[50];
char city[20];
char state[3];
char zip[10];
public :
void getAddr(char *AL1, char *AL2, char *c, char *s, char *z);
void printAddress();
};
#endif
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
//address.cpp
#include "address.h"
void Address::getAddr(char *AL1,char *AL2,char *c,char *s,char *z)
{
strcpy(address_line_1, AL1);
strcpy(address_line_2, AL2);
strcpy(city, c);
strcpy(state, s);
strcpy(zip, z);
}
void Address::printAddress()
{
cout << "Address Line 1: " << address_line_1 << endl;
cout << "Address Line 2: " << address_line_2 << endl;
cout << "City, State, Zip: " << city << ", " << state << ", " << zip << endl;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
//main.cpp
#include "address.h"
#include "address.cpp"
int main(){
Address a;
a.getAddr("7839 Corey Court" , "NONE" , "Indianapolis" , "IN" , "46227" );
a.printAddress();
return 0;
}
Sep 29, 2010 at 7:49pm UTC
Don't include address.cpp in your main file.
Sep 29, 2010 at 7:50pm UTC
lol wow...THANKS PAX!!!! i'm a idiot
Sep 29, 2010 at 7:51pm UTC
No, it's just one of those things that seems like it should be the way to do it when you first start. Glad to help. :)
Sep 29, 2010 at 7:58pm UTC
I appreciate it! :D
Topic archived. No new replies allowed.