重载操作符的错误!

haozi 2002-05-23 12:17:27
我在我的 seqlist.h 定义了
struct FilmData
{
char filmName[32];
char customerName[32];

};

int operator == (const FilmData &A , const FilmData &B)
{
return strcmp (A.filmName ,B.customerName);
}

typedef FilmData DataType;


然后我在MAIN()函数中调用了 这个操作符可是老报错!

SeqList.obj : error LNK2005: "int __cdecl operator==(struct FilmData const &,struct FilmData const &)" (??8@YAHABUFilmData@@0@Z) already defined in main.obj

Debug/pg4_2.exe : fatal error LNK1169: one or more multiply defined symbols found

而我没有在MAIN() 中重复定义啊!

望高手解答!

...全文
60 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
haozi 2002-05-27
  • 打赏
  • 举报
回复
再问
zhang1000 2002-05-23
  • 打赏
  • 举报
回复
不会呀,运行好好的!看看头文件有没有重复包含?

struct FilmData
{
char filmName[32];
char customerName[32];

};

int operator == (const FilmData &A , const FilmData &B)
{
if(strcmp(A.filmName ,B.customerName)) return 0;
return 1;
}

typedef FilmData DataType;

void main()
{
DataType dt1, dt2;
strcpy(dt1.filmName, "name1");
strcpy(dt2.filmName, "name2");
if(dt1==dt2) printf("OK");
else printf("No");

}
Bind 2002-05-23
  • 打赏
  • 举报
回复
你的代码本身也有问题,更正一下:
struct FilmData
{
char filmName[32];
char customerName[32];
int operator == (const FilmData &A )
{
return strcmp (this.filmName ,A.customerName);
}

};
haozi 2002-05-23
  • 打赏
  • 举报
回复
望高手解答,为什么会提示,重复提示呀???????

以上是全部的源码。
Bind 2002-05-23
  • 打赏
  • 举报
回复
struct FilmData
{
char filmName[32];
char customerName[32];

};
int operator == (const FilmData &A , const FilmData &B)
{
return strcmp (A.filmName ,B.customerName);
}

------>>>放在结构体声明中定义
struct FilmData
{
char filmName[32];
char customerName[32];
int operator == (const FilmData &A , const FilmData &B)
{
return strcmp (A.filmName ,B.customerName);
}

};

或:
struct FilmData
{
char filmName[32];
char customerName[32];
int operator == (const FilmData &A , const FilmData &B);
};

int FilmData::operator == (const FilmData &A , const FilmData &B)
{
return strcmp (A.filmName ,B.customerName);
}


haozi 2002-05-23
  • 打赏
  • 举报
回复


#include <fstream.h>
//#include "video.h"
#include "seqlist.h" // include the seqlist class

// reads film inventory from disk
void SetupInventoryList (SeqList &inventoryList)
{
ifstream filmFile;
FilmData fd;

// open the file with error checking
filmFile.open("Films", ios::in | ios::nocreate);
if (!filmFile)
{
cerr << "File 'films' not found ! " << endl;
exit(1);
}

// read lines until EOF; insert film names in inventory list
while (filmFile.getline(fd.filmName, 32,'\n'))
inventoryList.Insert(fd);


}

// cycle through inventory list and print film names
void PrintInventoryList (const SeqList &inventoryList)
{
int i;
FilmData fd;

for (i = 0 ; i < inventoryList.ListSize(); i++)
{
fd = inventoryList.GetData(i); // get film record
cout << fd.filmName << endl; // print film name
}
}

// cycle through coustomer list . print customer and film names
void PrintCustomerList(const SeqList &customerList)
{
int i;
FilmData fd;

for (i = 0 ; i < customerList.ListSize(); i ++ )
{
fd = customerList.GetData(i); // get customer record
cout << fd.customerName<< "(" << fd.filmName<<")" << endl;
}
}

int main (void)
{
// the two data bases
SeqList inventoryList, customerList;
int i;

// film inventory file
FilmData fdata;

char customer[20];

SetupInventoryList (inventoryList); // read inventory file

// process 4 customers, asking for name and film desired. if
// the film is available, insert film record into customer
// list and delete it from inventory list; otherwise,
// indicate it is not availbale.

for (i = 0 ; i <4 ; i++ )
{
// get customer name and film request
cout << " Customer Name:";
cin.getline(customer,32,'\n');
cout << " Film Request:";
cin.getline(fdata.filmName,32,'\n');
// check if film available if so, create customer record

if (inventoryList.Find(fdata))
{
strcpy (fdata.customerName, customer);
// inster in customer list
customerList.Insert(fdata);
// delete from inventory list
inventoryList.Delete(fdata);
}
else
cout << "Sorry ! " << fdata.filmName
<<" is not available." << endl;

}
cout << endl;

// print the final customer and inventory lists.
cout << "Customers Renting Fims " << endl;
PrintCustomerList(customerList);
cout << endl;
cout << "Films Remaining in Inventory:" << endl;
PrintInventoryList(inventoryList);

return 0;
}
haozi 2002-05-23
  • 打赏
  • 举报
回复
// SeqList.cpp: implementation of the SeqList class.
//
//////////////////////////////////////////////////////////////////////

#include "SeqList.h"


//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////



SeqList::SeqList():size(0)
{

}

SeqList::~SeqList()
{

}

void SeqList::ClearList()
{
size = 0;

}

//DataType SeqList::DeleteFront()
//{

//}


// search for item in the list and delete it if found
void SeqList::Delete(const DataType &item)
{
int i = 0;

// search for item
while (i < size && ! (item == listitem[i]))
i ++ ;

if ( i < size ) // successful if i < size
{
// shift the tail of the list to the left one position
while ( i < size -1 )
{
listitem[i] = listitem[i+1];
i ++ ;
}
size -- ; // decrement size
}
}


// insert item at rear of list. terminate the program
// if the list size would exceed MaxListSize.

void SeqList::Insert(const DataType & item)
{
// will an insertion exceed maximum list size allowed?
if (size +1 > MaxListSize)
{
cerr << "Maximum list size exceeded " << endl;
exit(1);
}

// index of rear is current value of size
listitem[size] = item ;
size ++ ; // increment list size

}

// return the value at position pos in the list. if pos is not a
// valid list position, terminate program with error message.
DataType SeqList::GetData(int pos) const
{
// terminate program if pos out of range
if (pos < 0 || pos >= size)
{
cerr << "pos is out of range! " << endl;
exit(1);
}
return listitem[pos];

}

// Use item as key and search the list. return True it item
// is in the list and False otherwise. If found,
// assign the list element to the reference parameter item

int SeqList::Find(DataType &item) const
{
int i = 0;
if (ListEmpty())
return 0 ; // return False when list empty
while ( i < size && ! (item == listitem[i]))
i ++ ;
if ( i < size )
{
item = listitem[i]; // assign list element to item
return 1; // return True
}
else
return 0 ; // return False

}


int SeqList::ListEmpty() const
{
if (size >0 )
{
return 1;
}
else
return 0;

}


int SeqList::ListSize() const
{
return size;

}
haozi 2002-05-23
  • 打赏
  • 举报
回复
//video.h

#include <string.h>

// record structure to store film and customer data
struct FilmData
{
char filmName[32];
char customerName[32];

};
int operator == (const FilmData &A , const FilmData &B)
{
return strcmp (A.filmName ,B.customerName);
}

typedef FilmData DataType;

------------------------------------------------------------------

// SeqList.h: interface for the SeqList class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_SEQLIST_H__7BEB921B_F2F0_458D_BA59_7050B38444DB__INCLUDED_)
#define AFX_SEQLIST_H__7BEB921B_F2F0_458D_BA59_7050B38444DB__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include <iostream.h>
#include <stdlib.h>
#include <string.h>
#include "video.h"

const int MaxListSize = 50;

class SeqList
{
public:
// list access methods
int ListSize(void) const;
int ListEmpty(void) const;
int Find(DataType &item) const;
DataType GetData(int pos) const;

// list modification methods
void Insert (const DataType &item);
void Delete(const DataType &item);
//DataType DeleteFront(void);
void ClearList(void);

// constructor
SeqList();
virtual ~SeqList();

private:
// list storage array and number of current list elements
int size;
DataType listitem[MaxListSize];
};

#endif // !defined(AFX_SEQLIST_H__7BEB921B_F2F0_458D_BA59_7050B38444DB__INCLUDED_)

--------------------------------------------------------------------

Yings 2002-05-23
  • 打赏
  • 举报
回复
我试了也能运行啊,要不你试试这个
typedef struct FilmData
{
char filmName[32];
char customerName[32];
operator== (const FilmData &B);
} FilmData;

int FilmData::operator== (const FilmData &B)
{
if(strcmp(filmName ,B.filmName)) return 0;
return 1;
}

typedef FilmData DataType;

void main()
{
DataType dt1, dt2;
strcpy(dt1.filmName, "name1");
strcpy(dt2.filmName, "name1");
if(dt1==dt2) printf("OK");
else printf("No");
}

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧