0

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()
  • 4
    I don't know whether you didn't get around to it yet, but your copy constructor does nothing. – chris Dec 08 '15 at 22:50
  • Adding to that, unless you need to do something special in the copy constructor or destructor you can remove them. The compiler will generate ones that do what you'd expect. – Kevin Dec 08 '15 at 22:53
  • You're also passing a `vector` by value, you might want to pass by reference instead. – cehnehdeh Dec 08 '15 at 22:53
  • actually the code is still in testing. I am trying to figure out what i am doing wrong with my vector implementation for now. – Kaliakoudis Dec 08 '15 at 22:54
  • @Kevin, Speaking of which, the destructor actually harms the class because it disables compiler-generated move operations (useful for that vector member). – chris Dec 08 '15 at 22:55
  • @chris Good point, I didn't think of that. – Kevin Dec 08 '15 at 22:56
  • @Kevin Just tested it and it was my constructor that cause the problem. I still can't figure out why the problem was caused but at least it is fixxed. Cheers to both of you. – Kaliakoudis Dec 08 '15 at 23:06
  • @chris Just tested it and it was my constructor that cause the problem. I still can't figure out why the problem was caused but at least it is fixxed. Cheers to both of you. – Kaliakoudis Dec 08 '15 at 23:07
  • Well, `cliques y = max_heap.front();` doesn't seem too helpful when copying doesn't create copies. – chris Dec 08 '15 at 23:34

1 Answers1

0

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.