任我冰鸟 2017-03-01 07:38 采纳率: 0%
浏览 1670

关于运算符重载遇到重定义的问题

错误 1 error LNK2005: "bool __cdecl operator<(struct Datatype const &,struct Datatype const &)" (??M@YA_NABUDatatype@@0@Z) 已经在 main.obj 中定义 c:\Users\admin\documents\visual studio 2013\Projects\SeqPQueue\SeqPQueue\SeqPQueue.obj SeqPQueue
错误 2 error LNK1169: 找到一个或多个多重定义的符号 c:\users\admin\documents\visual studio 2013\Projects\SeqPQueue\Debug\SeqPQueue.exe 1 1 SeqPQueue

大家能帮我看一下错在哪了吗?

//类的定义
#ifndef SEQQUEUE_H
#define SEQQUEUE_H

#include
using namespace std;
const int maxQueueSize = 100;
struct Datatype{
int taskNo;
int priority;
};
bool operator<(const Datatype& a, const Datatype& b){ return a.priority < b.priority; }
class SeqPQueue{
private:
Datatype data[maxQueueSize];
int count;
public:
SeqPQueue() : count(0){}
~SeqPQueue(){ cout << "destroyed" << endl; }
bool PIsEmpty()const{ return count==0; }
bool PIsFull()const{ return count == maxQueueSize; }
void PEnqueue(Datatype const& item);
Datatype PDequeue();
Datatype PQfront()const;
int Pgetsize()const{ return count; }
void clearPQueue(){ count=0; }
};

#endif


//seqpqueue.cpp。类的实现
#include"SeqPQueue.h"

void SeqPQueue::PEnqueue(const Datatype& item){
if (count == maxQueueSize){
cout << "队列已满" << endl;
exit(1);
}
data[count++] = item;
}

Datatype SeqPQueue::PQfront()const{
if (PIsEmpty())
{
cout << "队列已空" << endl;
exit(1);
}
Datatype min = data[0];
int minIndex = 0;
for (int i = 0; i < count;++i)
if (data[i]<min)
{
min = data[i];
minIndex = i;
}
return min;
}

Datatype SeqPQueue::PDequeue(){
if (PIsEmpty())
{
cout << "队列已空" << endl;
exit(1);
}
Datatype min = data[0];
int minIndex = 0;
for (int i = 0; i < count; ++i)
if (data[i]<min)
{
min = data[i];
minIndex = i;
}
for (int i = minIndex; i < count - 1; ++i)
{
data[i] = data[i + 1];
}
count--;
return min;

}


//主程序
#include
#include
using namespace std;

#include"SeqPQueue.h"

void main(){
SeqPQueue myPQueue;
ifstream fin;
Datatype task;

fin.open("task.txt", ios::in | ios::_Nocreate);
if (!fin){
    cerr << "不能打开文件task.txt" << endl;
    exit(1);
}

while (!fin.eof())
{
    fin >> task.taskNo;
    fin >> task.priority;
    myPQueue.PEnqueue(task);
}

int i= 1;
cout << "序号 "  << "任务号 " << "优先级 " << endl;
while (!myPQueue.PIsEmpty())
{
    cout << i << " ";
    task = myPQueue.PDequeue();
    cout << task.taskNo << " " << task.priority << endl;
    ++i;
}

}

  • 写回答

1条回答 默认 最新

  • devmiao 2017-03-01 22:29
    关注
    评论

报告相同问题?

悬赏问题

  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程