【问题标题】:Unresolved Externals between functions函数之间未解决的外部问题
【发布时间】:2014-05-19 00:08:15
【问题描述】:

我遇到了一个未解决的外部错误,但我不知道究竟是什么原因造成的。

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:\Users\Matt\documents\visual studio 2010\Projects\GSP_125_Lab5\Debug\GSP_125_Lab5.exe : 致命错误 LNK1120: 1 unresolved externals

当我删除这段代码时程序运行:

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] = '\0';
    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;
}

【问题讨论】:

    标签: c++ unresolved-external


    【解决方案1】:

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

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

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

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

    【讨论】:

    • 非常感谢。那行得通,但现在我收到“访问冲突写入位置 0xcdcdcdcd”。错误。知道是什么原因造成的吗?至于char数组的用途,是学校作业,所以我必须使用给我的标题。
    • 显示新代码,以及发生崩溃的位置(根据调试器)。我们无法调试我们甚至看不到的东西。
    【解决方案2】:

    您忘记为 ABC(const ABC & rs) 定义实现;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-20
      • 2020-10-15
      • 2011-12-19
      • 2017-09-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多