Monday, October 1, 2018

c++ - undefined reference to CLASS::function()




So when I try to simply compile my code using "g++ Asg5.cpp" I receive the following error




/tmp/cczhpSGO.o: In function 'main':



Asg5.cpp:(.text+0x2fb): undefined reference to 'BinomialTree::insert(int)'



collect2: ld returned 1 exit status





If anyone's wondering why I'm not using a makefile, my professor simply wants to type g++ <.cpp with main()> to compile..



Anyway here's my code I really appreciate the assistance!



Asg5.cpp



#include "BinomialTree.h"
#include "BinomialNode.h"
#include
#include

#include
#include
#include
#include
#include

using namespace std;
int main(int argc, char* argv[])
{
//input handling

if(argc != 2)
{
cout << "Incorrect Usage. \n Example: ./a.out " << endl;
exit(1);
}
BinomialTree *tree = new BinomialTree();

char *buffer;
char *token;
//read file into buffer.**************************************

string input;
ifstream file;
file.open(argv[1]);
if(file.is_open())
{
string str;
while(file.good())
{
getline(file,str);
input += " " + str;

}
}
else{
cout << "File not found"<< endl;
return 1;
}
file.close();

int buf;
stringstream ss(input);


vector tokens;

while(ss >> buf)
{
tokens.push_back(buf);
}
int i = 0;
for(i = 0; i < tokens.size(); i++)
tree->insert(tokens[i]);

//end file reading *******************************************
delete tree;
}


BinomialNode.h



#ifndef _BINOMIALNODE_H_
#define _BINOMIALNODE_H_
#include "BinomialTree.h"

class BinomialNode
{
public:
int k;
BinomialNode *children[20];
int data;

BinomialNode();
};
#endif



BinomialNode.cpp



class BinomialNode
{
BinomialNode::BinomialNode(int n)
{
this->k = 0;
this->data = n;

}
}


BinomialTree.h



#ifndef _MULTIMAP_H_
#define _MULTIMAP_H_
#include "BinomialNode.h"


class BinomialTree
{
public:
BinomialNode * BQ[20];


void insert(int n);
void merge(BinomialNode *queue, BinomialNode *in, int k);
void print(BinomialNode *root, int tab);
};

#endif


BinomialTree.cpp



#include "BinomialNode.h"
#include "BinomialTree.h"
#include
#include



class BinomialTree
{
void BinomialTree::insert(int n)
{
BinomialNode *in = new BinomialNode(n);
if(BQ[0] == NULL)
{
BQ[0] = in;
return;

}
else
merge(BQ[0], in, 0);
}
void BinomialTree::merge(BinomialNode *queue, BinomialNode *in, int k)
{
if(queue == NULL)
{
BQ[k] = in;
return;

}
if(n == NULL)
{
BQ[k] = queue;
return;
}
if(queue->data > in->data)
{
merge(in, queue);
return;

}
queue->k++;
BinomialNode* temp[queue->k];
int i;
for(i = 0; i < queue->k-1; i++)
temp[i] = queue->children[i];
temp[queue->k-1] = in;
for(i = 0; i < queue->k; i++)
queue->children[i] = temp[i];
if(BQ[queue->k] == NULL)

{
BQ[queue->k] = queue;
return;
}
else
merge(queue, BQ[queue->k]);
}
void BinomialTree::print(BinomialNode *root, int tab)
{
if(root == NULL)

return;
int i;
for(i = 0; i < tab*5; i++) cout << " ";
cout << root->data << endl;
for(i = 0; i < root->k; i++) print(root->children[i], tab+1);
}
}

Answer



You cpp files shouldn't have Class in them. They should look more like:




BinomialNode.cpp



#include "BinomialNode.h"

BinomialNode::BinomialNode(int n) :
k(0)
{
data = n;
}



And of course the corollary for the much longer BinomialTree.cpp. Also, you should compile it with something like:



g++ BinomialTree.cpp BinomialNode.cpp Asg5.cpp -o asg5


Also you're going to run into a lot of other problems with you code. For instance:



BinomialNode * BQ[20];



I don't see BQ being initialized anywhere, which means you're pretty much guaranteed a seg fault if you were to run this. You need to initialize this or allocate it. Seeing lines like:



if(BQ[0] == NULL)


Makes me think you really wanted:



BinomialNode BQ[20];



Though you would still need to initialize it to all NULLs since you aren't guaranteed that will be full of NULLs when you run the program. Also, this is recursive and infinite and can't possibly work (in BinomialNode.h):



BinomialNode *children[20];


There are likely more issues with this code, but that wasn't your question, so I'll stop now!


No comments:

Post a Comment

plot explanation - Why did Peaches&#39; mom hang on the tree? - Movies &amp; TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...