undefined reference to with constructor
Jun 1, 2020 at 12:29pm UTC
Hi Community,
i try make a disjointsets with rank,
when i compile the files, i have this error:
main.cpp:6: undefined reference to `disjointset::disjointset(int)'
collect2.exe: error: ld returned 1 exit status
The terminal process terminated with exit code: 1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
#ifndef TREENODE_HPP
#define TREENODE_HPP
#include <vector>
#include <string>
using namespace std;
class TreeNode //struct albero
{
private :
int key;
string value;
int rango;
TreeNode *parent;
public :
TreeNode(int key, string value)
{
this ->key=key;
this ->value=value;
this ->rango=0;
this ->parent=this ;
}
void setKey(int key) { this ->key = key; }
int getKey() { return this ->key; }
void setValue(string value) { this ->value = value; }
string getValue() { return this ->value; }
void setRango(int rango) { this ->rango += rango; }
int getRango() { return this ->rango; }
void setParent(TreeNode* parent) { this ->parent = parent; }
TreeNode* getParent() { return this ->parent; }
};
#endif
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
#ifndef DISJOINTSET_HPP
#define DISJOINTSET_HPP
#include <string>
#include <iostream>
#include "TreeNode.hpp"
using namespace std;
class disjointset
{
private :
vector<TreeNode*> *D;//vector del nodo tree
public :
disjointset(int size);
~disjointset()
{
delete [] D;
}
void makeSet(int key, string value);
TreeNode* findSet(TreeNode* x);
bool link(TreeNode* x, TreeNode* y);
bool connect(TreeNode* x, TreeNode* y); //union
void print() { for ( int i; i<6; i++ ) cout << this ->D->at(i)->getKey(); };
};
#endif
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
#include "disjointset.hpp"
void disjointset::makeSet(int key, string value)
{
this ->D->push_back( new TreeNode(key,value) );
}
TreeNode* disjointset::findSet(TreeNode* x)
{
if ( x->getParent() != x )
x->setParent( findSet( x->getParent() ) );
return x->getParent();
}
bool disjointset::link(TreeNode* x, TreeNode* y)
{
if ( x == y )
return false ;
if ( x->getRango() > y->getRango() )
y->setParent(x);
else
x->setParent(y);
if ( x->getRango() == y->getRango() )
y->setRango(1);
return true ;
}
bool disjointset::connect(TreeNode* x, TreeNode* y)
{
if ( link( findSet(x), findSet(y) ) )
return true ;
return false ;
} //union
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
#include "disjointset.cpp"
int main()
{
disjointset* ciccio = new disjointset(5);
ciccio->makeSet(0,"ciao0" );
ciccio->makeSet(1,"ciao1" );
ciccio->makeSet(2,"ciao2" );
ciccio->makeSet(3,"ciao3" );
ciccio->makeSet(4,"ciao4" );
ciccio->print();
return 0;
}
if you see other errors please report them,thanks.
Jun 1, 2020 at 12:32pm UTC
You've declared that your class has a constructor:
disjointset(int size);
Nowhere in your code do you actually define that constructor. Hence, your linker is complaining that it can't find a definition.
Jun 1, 2020 at 2:45pm UTC
thanks, i don't see it, now fix:
this ->D = new vector<TreeNode*> ();
Last edited on Jun 1, 2020 at 2:45pm UTC
Jun 1, 2020 at 3:09pm UTC
What's the problem?
Topic archived. No new replies allowed.