简体   繁体   English

在C ++中创建类对象向量的问题

[英]Issues creating a vector of class object in c++

I created the following class 我创建了以下课程

#include "cliques.h"
#include "vector"
#include <iostream>
using namespace std;

cliques::cliques(){

}

cliques::cliques(int i) {
    clique.push_back(i);
    clique_prob = 1;
    mclique_prob = 1;
}

cliques::cliques(const cliques& orig) {
}

cliques::~cliques() {
}

void cliques::addvertex(int i) {

clique.push_back(i);

}



double cliques::getclique_prob() const {
    return clique_prob;
}

double cliques::getMaxclique_prob() const {
    return mclique_prob;
}

void cliques::showVertices() {
    for (vector<int>::const_iterator i = clique.begin(); i !=clique.end(); ++i)
    cout << *i << ' ';
    cout << endl;
}

vector<int> cliques::returnVector() {
    return clique;
}

void cliques::setclique_prob(double i) {
    clique_prob = i;
}

void cliques::setMaxclique_prob(double i) {
    mclique_prob = i;
}

Here's the header file 这是头文件

#include "vector"


#ifndef CLIQUES_H
#define CLIQUES_H

class cliques {
public:
    void addvertex(int i);
    cliques();
    cliques(int i);
    cliques(const cliques& orig);
    virtual ~cliques();
    double getclique_prob() const;
    double getMaxclique_prob() const;
    void showVertices();
    std::vector<int> returnVector();
    void setclique_prob(double i);
    void setMaxclique_prob(double i);
private:
    float clique_prob;
    float mclique_prob;
    std::vector <int> clique;
};

#endif /* CLIQUES_H */

I want to create a vector of these objects in order to implement a heap 我想创建这些对象的向量以实现堆

int main(int argc, char** argv) {


cliques temp(1);
cliques temp1(2);
temp.setclique_prob(0.32);
temp.setclique_prob(0.852);
temp.showVertices();
temp1.showVertices();

vector <cliques> max_heap;
max_heap.push_back(temp);
max_heap.push_back(temp1);
double x =max_heap.front().getclique_prob();
cout<<"prob "<<x<<endl;
cliques y = max_heap.front();
y.showVertices();

//make_heap (max_heap.begin(),max_heap.end(),max_iterator());
//sort_heap (max_heap.begin(),max_heap.end(),max_iterator());

return 0;
}

For reasons unknown to me none of my class functions work properly after i create my vector, meaning that while the following function works as intended 由于我未知的原因,创建矢量后,我的所有类函数均无法正常工作,这意味着尽管以下函数按预期工作

temp.showVertices()

the next one doesn't, 下一个没有,

 y.showVertices()

You miss implementation for 您错过了实施

cliques::cliques(const cliques& orig) {
}

STL vector uses copy constructor inside when you add values to it. 当向STL向量添加值时,它会在内部使用复制构造函数。 As your cliques class does not allocate any memory, you can just remove the copy constructor from the code and compiler will generate one for you. 由于您的集团类不会分配任何内存,因此您只需从代码中删除复制构造函数,编译器就会为您生成一个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM