Alaa M.
Alaa M.

Reputation: 5273

error LNK1169: one or more multiply defined symbols found

I have 3 files:

SilverLines.h
SilverLines.cpp
main.cpp

At SilverLines.cpp I have:

#include "SilverLines.h."

When I don't do:

#include "SilverLines.h"

at the main.cpp, everything is just fine. The project compiles.

But when I do:

#include "SilverLines.h"

in the main.cpp (Which i need to do), i get these 2 errors:

fatal errors

What's the problem?

> edit:

As you requested, this is the entire *.h code:

#ifndef SILVER_H
#define SILVER_H

#include <iostream>
#include <algorithm>
#include <iterator>
#include <vector>
#include <string>
#include <map>

using namespace std;

typedef unsigned short int u_s_int;

map<string /*phone*/,string /*company*/> companies; // a map of a phone number and its     company
string findCompany(const string& phoneNum); //Given a phone number, the function     returns the right company

//The Abstract Client Class:

class AbsClient //Abstract Client Class
{
protected:
//string phone_number;
int counter; //CALL DURATION TO BE CHARGED
double charge;
public:
string phone_number; //MOVE TO PROTECTED LATER
void setCharge(double ch) {charge=ch;}
void setCoutner(int count) {counter=count;}
AbsClient(string ph_num): phone_number(ph_num),counter(0), charge(0.0) {} //c'tor     that must be given a phone#.
                                                                            //Initializes the counter and the charge to 0.
virtual void registerCall(string /*phone#*/, int /*minutes*/) = 0; //make a call     and charge the customer
void printReport(string /*phone#*/) const; //prints a minutes report
};

//The Temporary Client Class:

class TempClient : public AbsClient //TempClient class (NO DISCOUNT!) 2.3 NIS PER MINUTE, inherits from ABS
{
private:
string company;
public:
//string company;
TempClient(string ph_num, string cmp_name): AbsClient(ph_num), company(cmp_name) {}
virtual void registerCall(string /*phone#*/, int /*minutes*/); //make a call and     charge the customer
//virtual void printReport(string phone) const {cout << "the number of minutes: " << this->counter << endl;}
};

//The REgistered Client Class:

class RegisteredClient : public AbsClient //RegisteredClient, 10% DISCOUNT! , inherits from ABS
{
protected:
string name;
string id;
long account; //ACCOUNT NUMBER
private:
static const int discount = 10; //STATIC DISCOUNT OF 10% FOR ALL Registered Clients
public:
RegisteredClient(string ph_num, string n, string i, long acc_num):     AbsClient(ph_num), name(n) , id(i) , account(acc_num) {}
virtual void registerCall(string /*phone#*/, int /*minutes*/); //make a call and     charge the customer
//virtual void printReport(string /*phone#*/) const {cout << "the number of     minutes: " << this->counter << endl;}
};

//The VIP Client Class

class VIPClient : public RegisteredClient //VIP Client! X% DISCOUNT! also, inherits from RegisteredClient
{
private: //protected?
int discount; //A SPECIAL INDIVIDUAL DISCOUTN FOR VIP Clients
public:
VIPClient(string ph_num, string n, string i, long acc_num, int X):     RegisteredClient(ph_num,n,i,acc_num), discount(X) {}
virtual void registerCall(string /*phone#*/, int /*minutes*/); //make a call and     charge the customer
//virtual void printReport(string /*phone#*/) const {cout << "the number of     minutes: " << this->counter << endl;}
};

//The SilverLines (the company) class

class SilverLines // The Company Class
{
protected:
vector<AbsClient*> clients;
vector<string> address_book; //DELETE
public:
//static vector<AbsClient*> clients;
//SilverLines();
void addNewClient(string /*phone#*/);
void addNewClient(string /*phone#*/, string /*name*/ , string /*id*/ , u_s_int     /*importance lvl*/ , long /*account#*/);
void registerCall(string /*phone#*/ , int /*call duration*/);
void resetCustomer(string /*phone#*/); //given a phone#, clear the appropriate     customer's data ('charge' and 'counter')
void printReport(string /*phone#*/) const;

~SilverLines(){
    vector<AbsClient*>::iterator it;
    for(it = clients.begin(); it != clients.end(); ++it)
        delete(*it);
}
};

#endif

paercebal's Answer:

Your code has multiple issues (the

using namespace std;

for one), but the one failing the compilation is the declaration of a global variable in a header:

map<string /*phone*/,string /*company*/> companies;

I'm quite sure most of your sources (main.cpp and SilverLines.cpp, at least) include this header.

You should define your global object in one source file:

// SilverLines.h

extern map<string /*phone*/,string /*company*/> companies;

and then declare it (as extern) in the header:

// global.cpp

extern map<string /*phone*/,string /*company*/> companies;

Upvotes: 1

Views: 7364

Answers (4)

Alaa M.
Alaa M.

Reputation: 5273

Thank you guys I worked it out:

findCompany() is now a global function, and map<string,string> companies is a static field inside it. It compiles great.

Upvotes: 0

Coding Mash
Coding Mash

Reputation: 3346

You may have another error in your code. ALso make sure that you are not writing your main or any other piece of code in .h file. .h file should just contain the prototypes of the functions followed by a semi colon.

Upvotes: 0

Tanmoy Bandyopadhyay
Tanmoy Bandyopadhyay

Reputation: 985

Seems there are some multiple definitions coming due to inclusion of the SilverLines.h twice. Pl. use a header guard like below in your SilverLines.h header file and then incude it.

This may help.

 #ifndef SILVER_H
 #define SILVER_H

 //Write the contents of SilverLines.h here.

 #endif  // SILVER_H

Upvotes: 0

Christian Stieber
Christian Stieber

Reputation: 12496

Chances are that your "SilverLines.h" actually DOES define something, instead of just declaring it. I'm not going to try and decipher the error message, though :-)

Upvotes: 0

Related Questions