编译文件时出现C++错误

C++ error when compiling file

本文关键字:C++ 错误 文件 编译      更新时间:2023-10-16

我有一个名为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 )

表示在&令牌之前存在期望的)。这对我来说没有任何意义,因为没有打开的(。我只是不知道为什么会出现这个错误。PrinterNameServer只是单独的类,是项目的一部分,但。。我是否也需要包括它们的头文件?

非常感谢您的帮助!

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

您的.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