【问题标题】:How to insert an object into a set如何将对象插入集合
【发布时间】:2013-12-10 22:56:41
【问题描述】:

我试图将一个对象插入到一个集合中,我重载了“

class domino {

public:
domino();
domino(int l, int r);

void SetRightDots(int rightDots);
int GetRightDots() const;

void SetLeftDots(int leftDots);
int GetLeftDots() const;

string toString() const;
bool operator<(const domino &d) const;
bool operator==(const domino &d) const;

private:
int leftDots;
int rightDots;

};


int main(int argc, char** argv){
set<domino> sd=set<domino>();
set<domino>::iterator ite;

for (int i = 1; i <= 6; i++) {
    for (int j = i; i <= 6; j++) {
        sd.insert(domino(i,j));
    }
}

}

但我仍然收到以下错误:

/home/joju/NetBeansProjects/CppApplication_1/main.cpp:51:未定义对 `domino::domino(int, int)' 的引用

build/Debug/GNU-Linux-x86/main.o:在函数`std::less::operator()(domino const&, domino const&) const'中:

/usr/include/c++/4.6/bits/stl_function.h:236: 未定义对 `domino::operator

collect2: ld 返回 1 个退出状态

【问题讨论】:

  • 你真的有 domino 类的实现吗? (ps 建议将类命名为 Domino)
  • 需要链接domino的实现,是在domino.cpp中实现吗?

标签: c++


【解决方案1】:

您的编译器抱怨没有找到采用两个整数的构造函数的实现。即使您将其留空,您也需要实际定义该构造函数。

例如:

domino::domino(int a, int b) { /* do something */ }

在你的类之外或者你可以直接在类中定义它:

class domino {
    /* stuff */
    domino(int l, int r) { /* do something */ }
    /* stuff */
};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-27
    • 2013-11-21
    • 1970-01-01
    • 2018-01-13
    • 1970-01-01
    • 2020-11-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多