c++ - undefined reference to CLASS::function() -
so when try compile code using "g++ asg5.cpp" receive following error
/tmp/cczhpsgo.o: in function 'main':
asg5.cpp:(.text+0x2fb): undefined reference 'binomialtree::insert(int)'
collect2: ld returned 1 exit status
if anyone's wondering why i'm not using makefile, professor wants type g++ <.cpp main()> compile..
anyway here's code appreciate assistance!
asg5.cpp
#include "binomialtree.h" #include "binomialnode.h" #include <iostream> #include <vector> #include <fstream> #include <sstream> #include <stdlib.h> #include <string> #include <stdio.h> using namespace std; int main(int argc, char* argv[]) { //input handling if(argc != 2) { cout << "incorrect usage. \n example: ./a.out <filename>" << endl; exit(1); } binomialtree *tree = new binomialtree(); char *buffer; char *token; //read file 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<int> tokens; while(ss >> buf) { tokens.push_back(buf); } int = 0; for(i = 0; < 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 <iostream> #include <cstdlib> 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; < queue->k-1; i++) temp[i] = queue->children[i]; temp[queue->k-1] = in; for(i = 0; < 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; < tab*5; i++) cout << " "; cout << root->data << endl; for(i = 0; < root->k; i++) print(root->children[i], tab+1); } }
you cpp files shouldn't have class in them. should more like:
binomialnode.cpp
#include "binomialnode.h" binomialnode::binomialnode(int n) : k(0) { data = n; }
and of course corollary longer binomialtree.cpp. also, should compile like:
g++ binomialtree.cpp binomialnode.cpp asg5.cpp -o asg5
also you're going run lot of other problems code. instance:
binomialnode * bq[20];
i don't see bq being initialized anywhere, means you're pretty guaranteed seg fault if run this. need initialize or allocate it. seeing lines like:
if(bq[0] == null)
makes me think wanted:
binomialnode bq[20];
though still need initialize nulls since aren't guaranteed full of nulls when run program. also, recursive , infinite , can't possibly work (in binomialnode.h):
binomialnode *children[20];
there more issues code, wasn't question, i'll stop now!
Comments
Post a Comment