我有 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;