任我冰鸟 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
    关注
    评论

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能