功能之间未解决的外部问题

时间:2014-04-06 22:20:43

标签: c++ unresolved-external

我收到了一个未解决的外部错误,但我无法确定究竟是什么原因造成的。

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个未解析的外部

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

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

2 个答案:

答案 0 :(得分:0)

您已声明ABC复制构造函数,但尚未定义它。

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

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

您可以使用std::string代替char数组来大大简化课程。您不需要提供赋值运算符,复制构造函数或析构函数。

答案 1 :(得分:0)

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