简体   繁体   中英

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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