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);
}
}
No comments:
Post a Comment