函数之间的未解析外部

Unresolved Externals between functions

本文关键字:外部 之间 函数      更新时间:2023-10-16

我遇到了一个未解决的外部错误,但我无法弄清楚究竟是什么导致了它。

error LNK2019: unresolved external symbol "public: __thiscall ABC::ABC(class ABC const &)" (??0ABC@@QAE@ABV0@@Z) referenced in function "public: __thiscall hasDMA::hasDMA(class hasDMA const &)" (??0hasDMA@@QAE@ABV0@@Z)

1>C:\用户\Matt\documents\Visual Studio 2010\Projects\GSP_125_Lab5\Debug\GSP_125_Lab5.exe:致命错误LNK1120:1 个未解析的外部

当我删除此代码块时,程序将运行:

hasDMA::hasDMA(const hasDMA & hs) : ABC(hs)
{
    style = new char[std::strlen(hs.style) + 1];
    std::strcpy(style, hs.style);
}

但我不知道其中的哪一部分在其他地方被引用。

这是我的ABC标头和hasDMA标头。

class ABC
{
private:
    enum {MAX = 35};
    char label[MAX];
    int rating;
protected:
    const char * Label() const {return label;}
    int Rating() const {return rating;}
public:
    ABC(const char * l = "null", int r = 0);
    ABC(const ABC & rs);
    virtual ~ABC() {};
    virtual ABC & operator*()  { return *this; }
    ABC & operator=(const ABC & rs);
    virtual void View() const = 0;
    friend std::ostream & operator<<(std::ostream & os, const ABC & rs);
};

class hasDMA :public ABC
{
private:
    char * style;
public:
    hasDMA(const char * s = "none", const char * l = "null",
              int r = 0);
    hasDMA(const char * s, const ABC & rs);
    hasDMA(const hasDMA & hs);
    ~hasDMA(){};
    hasDMA & operator=(const hasDMA & rs);
    virtual void View() const;
};

这是我仅有的两种ABC方法:

ABC::ABC(const char *l, int r)
{
    std::strcpy(label, l);
    label[MAX - 1] = '';
    rating = r;
}
ABC & ABC::operator=(const ABC & rs)
{
    if (this == &rs)
        return *this;
    strcpy(label, rs.label);
    return *this;
}

如果有帮助,这些是我的其余 hasDMA 方法:

hasDMA::hasDMA(const char *s, const char *l, int r) : ABC (l, r)
{
    std::strcpy(style, s);
}
hasDMA::hasDMA(const char *s, const ABC & rs)
{
    std::strcpy(style, s);
}
void hasDMA::View() const
{
    cout << "Record Label: " << Label() << endl;
    cout << "Rating: " << Rating() << endl;
    cout << "Style: " << style << endl;
}
hasDMA & hasDMA::operator=(const hasDMA & hs)
{
    if (this == &hs)
        return *this;
    ABC::operator=(hs);
    style = new char[std::strlen(hs.style) +1];
    std::strcpy(style, hs.style);
    return *this;
}

你已经声明了一个ABC复制构造函数,但你还没有定义它。

ABC(const ABC & rs); // declaration. 

您需要提供定义或删除声明。

您可以通过使用 std::string 而不是 char 数组来大大简化类。无需提供赋值运算符、复制构造函数或析构函数。

你忘了为 ABC(const ABC & rs) 定义实现;