我实际上是编程新手,我对此错误一无所知。我知道此类问题之前已经得到解答,但我无法找到问题的答案。
错误是:“1> a4main.obj:错误LNK2019:未解析的外部符号”bool __cdecl运算符==(类EAN const&,类EAN const *)“(?? 8 @ YA_NABVEAN @@ PBV0 @@ Z)在函数_main中引用 “
1> C:\ Users \ LUV \ documents \ visual studio 2013 \ Projects \ a4 \ Debug \ a4.exe:致命错误LNK1120:1个未解析的外部
**main file (a4main.cpp)**
这只是主程序的一部分,我认为问题出在这里。如果需要,我稍后会发布整个主要功能。
#include <iostream>
//#include "GS1Prefix.h"
#include "Order.h"
#define MAXORDERS 100
char menu(std::istream& is);
bool style(std::istream& is, char&);
int main() {
char choice;
int noOrders = 0;
iOrder* order[MAXORDERS];
Prefix prefix("prefixRanges.txt");
std::cout << "Bookstore Order Processor\n"
<< "=========================\n";
// process user input
do {
choice = menu(std::cin);
std::cout << std::endl;
switch (choice) {
case 'P':
{
EAN ean;
if (ean.read(std::cin, prefix)) {
int index = -1, created = false;
for (int i = 0; i < noOrders && index == -1; i++)
if (ean == order[i]->getEAN())
index = i;
if (index == -1)
if (noOrders < MAXORDERS) {
index = noOrders;
order[noOrders++] = new Order(ean);
created = true;
}
else
std::cerr << "No space for more orders!" << std::endl;
if (!order[index]->add(std::cin) && created)
delete order[--noOrders];
}
}
EAN.cpp文件,其中包含'== operator'
的定义
bool operator==(const EAN& left, const EAN& right)
{
int flag = 0;
if (!strcmp(left.prefixele,right.prefixele))
{
if (!strcmp(left.area,right.area))
{
if (!strcmp(left.publisher,right.publisher))
{
if (!strcmp(left.title,right.title))
{
if (left.checkdigit == right.checkdigit)
{
flag = 1;
}
}
}
}
}
if (flag == 1)
return true;
else
return false;
}
Order.h文件
#include "EAN.h"
class iOrder{
public:
virtual bool add(std::istream&) = 0;
virtual void display(std::ostream& os) const = 0;
virtual bool add(int n) = 0;
virtual EAN* getEAN() = 0;
virtual bool receive(std::istream&) = 0;
virtual int outstanding() const = 0;
};
class Order: public iOrder
{
int ordered;
int delivered;
EAN ean_o;
bool empty;
public:
Order();
Order(const EAN& );
EAN* getEAN();
int outstanding() const;
bool add(std::istream& is);
bool add(int n);
bool receive(std::istream& is);
void display(std::ostream& os) const;
//virtual ~Order();
};
std::ostream& operator<< (std::ostream& os, const iOrder& order);
class SpecialOrder :public Order
{
char* instructions;
public:
SpecialOrder();
SpecialOrder(const EAN& isbn, const char* instr);
SpecialOrder(const SpecialOrder& source);
SpecialOrder& operator=(const SpecialOrder& source);
bool add(std::istream& is);
void display(std::ostream& os) const;
~SpecialOrder();
};
std::ostream& operator<< (std::ostream& os, const SpecialOrder& order);
EAN * getEAN()函数
EAN* Order::getEAN()
{
EAN p = (*this).ean_o;
EAN* pp = &p;
return pp;
}
答案 0 :(得分:2)
==
运算符需要通过引用传递两个EAN
个参数。但是,用作第二个参数的getEAN
方法会将指针传递给EAN
,而不是引用。修改您的==
运算符以接受指针作为第二个参数,或者更改代码以通过引用传递两个EAN
。
答案 1 :(得分:1)
函数GetEAN返回一个指针,所以行
if (ean == order[i]->getEAN())
实际上是在尝试将EAN对象与内存地址进行比较
将行更改为
if (ean == *( order[i]->getEAN() ) )
在通过对象的值
初始化引用时,应该解决问题