我有3个文件:
SilverLines.h
SilverLines.cpp
main.cpp
在SilverLines.cpp,我有:
#include "SilverLines.h."
当我不这样做时:
#include "SilverLines.h"
在main.cpp,一切都很好。该项目编译。
但是当我这样做时:
#include "SilverLines.h"
在main.cpp(我需要做),我得到这两个错误:
有什么问题?
>编辑:
根据您的要求,这是整个* .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的回答:
您的代码有多个问题(
使用namespace std;
for one),但是编译失败的是标题中的全局变量声明:
map<string /*phone*/,string /*company*/> companies;
我很确定你的大多数资源(至少是main.cpp和SilverLines.cpp)都包含这个标题。
您应该在一个源文件中定义全局对象:
// SilverLines.h
extern map<string /*phone*/,string /*company*/> companies;
然后在标题中声明它(作为extern):
// global.cpp
extern map<string /*phone*/,string /*company*/> companies;
答案 0 :(得分:0)
有可能你的“SilverLines.h”实际上定义了一些东西,而不仅仅是声明它。我不打算尝试解密错误消息,但是: - )
答案 1 :(得分:0)
您的代码中可能有其他错误。 还要确保您没有在.h文件中编写主代码或任何其他代码。 .h文件应该只包含函数的原型,然后是半冒号。
答案 2 :(得分:0)
由于包含SilverLines.h两次,似乎有一些多个定义。 PL。在SilverLines.h头文件中使用如下所示的标题保护,然后将其包含在内。
这可能有所帮助。
#ifndef SILVER_H
#define SILVER_H
//Write the contents of SilverLines.h here.
#endif // SILVER_H
答案 3 :(得分:0)
谢谢你们,我把它解决了:
findCompany()
现在是一个全局函数,map<string,string> companies
是一个static
字段。它汇编得很好。