队列错误 C2371:'ItemType':重新定义;不同的基本类型
Queue error C2371: 'ItemType' : redefinition; different basic types
我正在为c++赋值编写代码,我已经获得了Queue.h和Queue.cpp文件。但是不能使用它们,因为当我在main()中放入#include "Queue.h"时。我在Queue.h文件的第12行中得到了上述问题。
error C2371: 'ItemType' : redefinition; different basic types
我想把RecordService类作为Itemtype。
My Queue.h文件如下。
#pragma once
#ifndef Queue_H
#define Queue_H
#include<iostream>
#include<string>
#include "RecordService.h"
using namespace std;
typedef RecordService ItemType; // << problem here
/** ADT queue - Pointer-based implementation. */
class Queue
{
private:
/** A node on the queue. */
struct Node
{
/** A data item on the queue. */
ItemType item;
/** Pointer to next node. */
Node *next;
}; // end Node
/** Pointer to front node in the queue. */
Node *frontNode;
/** Pointer to back node in the queue. */
Node *backNode;
public:
/** Default constructor. */
Queue();
/** Destructor. */
~Queue();
// Queue operations:
bool isEmpty() const;
bool enqueue(ItemType& newItem);
bool dequeue();
bool dequeue(ItemType& item);
void getFront(ItemType& item) const;
}; // end Queue
// End of header file.
#endif
My Queue.cpp is here
#include "Queue.h" // header file
Queue::Queue(void)
{
} // end default constructor
Queue::~Queue()
{
while (!isEmpty())
dequeue();
} // end destructor
bool Queue::isEmpty() const
{
return backNode == NULL;
} // end isEmpty
bool Queue::enqueue(ItemType& newItem)
{
// create a new node
Node *newNode = new Node;
newNode->item = newItem;
newNode->next = NULL;
// insert the new node
if (isEmpty())
// insertion into empty queue
frontNode = newNode;
else
// insertion into nonempty queue
backNode->next = newNode;
backNode = newNode; // new node is at back
return true;
} // end enqueue
bool Queue::dequeue()
{
if(isEmpty())
{
cout<<"The queue is empty."<<endl;
}
else
{
Node *temp= frontNode;
if(frontNode==backNode)
{
frontNode=NULL;
backNode=NULL;
}
else
frontNode=frontNode->next;
temp->next=NULL;
delete (temp);
}
return true; // end if
} // end dequeue
bool Queue::dequeue(ItemType& item)
{
if(isEmpty())
{
cout<<"The queue is empty."<<endl;
}
else
{
item = frontNode->item;
dequeue();
}
return true;
} // end dequeue
void Queue::getFront(ItemType& item) const
{
if (!isEmpty())
// queue is not empty; retrieve front
item = frontNode->item;
else
cout << "The queue is empty." << endl;
} // end getFront
你可以看到,我想把RecordService作为Itemtype在"Queue.h"头。这里是RecordService.h
#pragma once
#ifndef RecordService_H
#define RecordService_H
#include <iostream>
#include <string>
#include <ctime>
using namespace std;
class RecordService
{
public:
RecordService();
RecordService(string, string, string, int, double, bool, string);
~RecordService();
//set methods
void setTransID(char);
void setCusName(string);
void setVehicleNo(string);
void setCarType(string);
void setWashDuration(int);
void setShampooDuration(int);
void setPolishDuration(int);
void setVacuumDuration(int);
void setTotalDuration(int,int,int,int);
void setWashingCharge(double);
void setShampooingCharge(double);
void setPolishingCharge(double);
void setVacuumingCharge(double);
void setTotalCharge(double,double,double,double);
void setRewardStatus(bool);
void setDateOfTrans(string);
//get methods
char getTransID();
string getCusName();
string getVehicleNo();
string getCarType();
int getWashDuration();
int getShampooDuration();
int getPolishDuration();
int getVacuumDuration();
int getTotalDuration();
double getWashingCharge();
double getShampooingCharge();
double getPolishingCharge();
double getVacuumingCharge();
double getTotalCharge();
bool getRewardStatus();
string getDateOfTrans();
private:
char transID;
string CusName;
string vehicleNo;
string carType;
int WashDuration;
int ShampooDuration;
int PolishDuration;
int VacuumDuration;
int TotalDuration;
double WashingCharge;
double ShampooingCharge;
double PolishingCharge;
double VacuumingCharge;
double TotalCharge;
bool RewardStatus;
string DateOfTrans;
};
#endif
提前感谢你的帮助。
ItemType
可以移动到Queue
类中。现在,它可能会与您定义了ItemType
的其他头文件发生冲突。
所以不用
typedef RecordService ItemType; // << problem here
/** ADT queue - Pointer-based implementation. */
class Queue
{
...
}
使用/** ADT queue - Pointer-based implementation. */
class Queue
{
typedef RecordService ItemType;
...
}
ItemType
仍然可以作为Queue
中的ItemType
和Queue
类之外的Queue::ItemType
访问(如果使public
)
相关文章:
- 使用QJsEngine在Qt中注册自定义类型
- 在UE4中使用未定义类型'UTextBlock'
- 修改"std::set"中用户定义类型的值
- 当我使用自定义类型创建动态数组时,即使使用字符串,它似乎也不起作用
- QtQuick - qml:28:错误:未知方法返回类型:自定义类型
- 如何使自定义类型在unordered_map中用作键
- 在 C++20 中是否不再允许在 std 中对程序定义类型的函数模板进行专用化?
- Qt5 远程对象 + 自定义类型,但不在 POD 中
- 为什么转换函数声明不需要至少一个定义类型说明符
- 标准::原子中的自定义类型
- 如何使用自定义类型声明Arduino数组?
- 如何在 Cython 中定义返回 cpp 定义类型的函数?
- 使用自定义访问者时具有自定义类型的提升变体失败(源自 boost::static_visitor)
- 您可以将binary_search应用于具有自定义类型的矢量吗?
- 在自定义类型图中重用 SWIG 映射
- 扩展自定义类型的spdlog
- vim使用户定义类型的COLOR与C++中的基本类型相同
- 重载自定义类型的 std::to_string 和 std::to_chars?
- 具有未声明/未定义类型的 typedef 结构
- 函数重载:内置类型与用户定义类型