0

我有一个名为 BottlingPlant 的课程。我创建了以下头文件:

#ifndef __BOTTLINGPLANT_H__
#define __BOTTLINGPLANT_H__

#include <iostream>

class BottlingPlant {
public:
BottlingPlant( Printer &prt, NameServer &nameServer, unsigned int numVendingMachines, unsigned int maxShippedPerFlavour, unsigned int maxStockPerFlavour, unsigned int timeBetweenShipments );
void getShipment( unsigned int cargo[ ] );
void action();  
};

#endif

以及以下 .cc 文件:

#include <iostream>
#include "PRNG.h"
#include "bottlingplant.h"

BottlingPlant::BottlingPlant( Printer &prt, NameServer &nameServer, unsigned int numVendingMachines, unsigned int maxShippedPerFlavour, unsigned int maxStockPerFlavour, unsigned int timeBetweenShipments ) {


}

void BottlingPlant::getShipment( unsigned int cargo[ ] ) {

}

void BottlingPlant::action() {

}

当我尝试编译 .cc 时,它在 .cc 和 .h 行中出现错误:

BottlingPlant::BottlingPlant( Printer &prt, NameServer &nameServer, unsigned int numVendingMachines, unsigned int maxShippedPerFlavour, unsigned int maxStockPerFlavour, unsigned int timeBetweenShipments )

说在token)之前有一个expect。&这对我来说没有任何意义,因为没有 open (。我只是不确定为什么会出现此错误。Printer并且NameServer只是项目的一部分单独的类,但是..我是否也需要包含它们的头文件?

任何帮助是极大的赞赏!

4

2 回答 2

5

您需要包含您正在使用的任何类的头文件,甚至是同一个项目中的类。编译器将每个单独的源文件作为一个单独的翻译单元处理如果定义它的头文件没有包含在该翻译单元中,它就不会知道一个类的存在。

于 2012-07-19T01:08:21.917 回答
1

您的 .h 文件应包含具有 Printer 和 NameServer 类定义的标头。例如,如果它们位于 MyHeader.h 中,则显示的以下示例应修复这些错误。

#ifndef __BOTTLINGPLANT_H__
#define __BOTTLINGPLANT_H__

#include <iostream>
#include "MyHeader.h"

class BottlingPlant {
public:
BottlingPlant( Printer &prt, NameServer &nameServer, unsigned int numVendingMachines, unsigned int maxShippedPerFlavour, unsigned int maxStockPerFlavour, unsigned int timeBetweenShipments );
void getShipment( unsigned int cargo[ ] );
void action();  
};

#endif
于 2012-07-19T01:12:54.157 回答