Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
488 views
in Technique[技术] by (71.8m points)

c++ - 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;
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 :-)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...