我有一个名为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 )
说)
令牌之前有预期的&
。这对我没有任何意义,因为没有开放(
。我只是不确定为什么它会给出这个错误。 Printer
和NameServer
只是项目的一部分,但是..我是否还需要包含头文件?
非常感谢任何帮助!
答案 0 :(得分:5)
您需要包含您正在使用的任何类的头文件,甚至包括同一项目中的类。编译器将每个单独的源文件作为单独的translation unit进行处理,如果定义它的标头未包含在该转换单元中,则不会知道该类是否存在。
答案 1 :(得分: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