1

我有 3 个文件:

SilverLines.h
SilverLines.cpp
main.cpp

在 SilverLines.cpp 我有:

#include "SilverLines.h."

当我不这样做时:

#include "SilverLines.h"

在 main.cpp,一切都很好。项目编译。

但是当我这样做时:

#include "SilverLines.h"

在 main.cpp (我需要做的)中,我收到以下 2 个错误:

致命错误

有什么问题?

> 编辑:

根据您的要求,这是整个 *.h 代码:

#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

//抽象客户端类:

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
};

//临时客户端类:

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;}
};

//注册客户端类:

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;}
};

//VIP客户类

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;}
};

//SilverLines(公司)类

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 的回答:

您的代码有多个问题(

使用命名空间标准;

一),但编译失败的是在头文件中声明全局变量:

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

我很确定您的大多数来源(至少 main.cpp 和 SilverLines.cpp)都包含此标头。

您应该在一个源文件中定义您的全局对象:

// SilverLines.h

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

然后在标题中声明它(作为外部):

// 全局.cpp

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

4 回答 4

0

很有可能您的“SilverLines.h”实际上确实定义了一些东西,而不仅仅是声明它。不过,我不会尝试破译错误消息:-)

于 2012-08-26T17:51:26.260 回答
0

您的代码中可能还有另一个错误。还要确保您没有在 .h 文件中编写主代码或任何其他代码。.h 文件应该只包含函数的原型,后跟分号。

于 2012-08-26T17:51:31.163 回答
0

由于包含 SilverLines.h 两次,似乎有一些多重定义。PL。在您的 SilverLines.h 头文件中使用如下标头保护,然后包含它。

这可能会有所帮助。

 #ifndef SILVER_H
 #define SILVER_H

 //Write the contents of SilverLines.h here.

 #endif  // SILVER_H
于 2012-08-26T17:57:01.333 回答
0

谢谢各位,我解决了:

findCompany()现在是一个全局函数,并且map<string,string> companies是其中的一个static字段。它编译得很好。

于 2012-08-26T19:05:56.870 回答