【问题标题】:error LNK2019: unresolved external symbol "..." referenced in function _main错误 LNK2019:函数 _main 中引用的未解析的外部符号“...”
【发布时间】:2014-04-05 21:47:39
【问题描述】:

我实际上是编程新手,我对这个错误一无所知。我知道这类问题之前已经回答过,但我找不到我的问题的答案。

错误是:" 1>a4main.obj : error LNK2019: unresolved external symbol "bool __cdecl operator==(class EAN const &,class EAN const *)" (??8@YA_NABVEAN@@PBV0 @@Z) 在函数 _main 中引用 "

1>C:\Users\LUV\documents\visual studio 2013\Projects\a4\Debug\a4.exe : 致命错误 LNK1120: 1 unresolved externals

**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];
                    }
        } 

包含'== operator'定义的EAN.cpp文件

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;


}

【问题讨论】:

    标签: c++ oop


    【解决方案1】:

    == 运算符需要两个通过引用传递的 EAN 参数。但是,用作第二个参数的getEAN 方法将指针传递给EAN,而不是引用。修改 == 运算符以接受指针作为第二个参数,或更改代码以通过引用传递两个 EANs。

    【讨论】:

    • 我将 EAN& 权限更改为 EAN* 权限,但是我不知道如何使用 EAN* 权限访问 EAN 类的私有数据成员。我的意思是它是一个 EAN 类型的指针,但我如何使用它来访问数据成员
    【解决方案2】:

    GetEAN 函数返回一个指针,所以行

    if (ean == order[i]->getEAN())
    

    实际上是在尝试将 EAN 对象与内存地址进行比较

    换行

    if (ean == *( order[i]->getEAN() ) )
    

    应该在通过对象的值初始化引用时解决问题

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-18
      • 2013-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多