运行这段代码的时候会报下面的错误,不知道咋解决
#include "RadixNode.h"
#include <iostream>
int main(){
RadixNode a = RadixNode("",false);
// root.insert("banana");
// vector<string> vecstr={"banana","bananas","apple"};
// root.insert(vecstr);
// root.print();
return 0;
}
main.cpp:(.text+0x62): undefined reference to `RadixNode::RadixNode(std::__cxx11::basic_string<char, std::char_traits, std::allocator >, bool)'
collect2: error: ld returned 1 exit status
// RadixNode.h
#ifndef RADIXNODE_H
#define RADIXNODE_H
#pragma once
#include <string>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;
class RadixNode{
public:
RadixNode(string prefix="", bool is_leaf=false);
vector<string> match(string word);
void insert(string word);
void insert(vector<string> words);
bool find(string word);
bool remove(string word);
void print(int height=0);
void setprefix(string prefix);
void setnodes(string key, RadixNode node);
void setisleaf(bool is_leaf);
private:
map<string, RadixNode> nodes;
bool is_leaf;
string prefix;
};
#endif
//RadixNode.h
#include "RadixNode.h"
RadixNode::RadixNode(string prefix="", bool is_leaf=false){
this->nodes = {};
this->is_leaf = is_leaf;
this->prefix = prefix;
}
vector<string> RadixNode::match(string word){
int x = 0;
int len = min(this->prefix.size(), word.size());
for(int i=0;i<len;i++){
if(this->prefix[i] != word[i]){
break;
}
x++;
}
vector<string> res = {this->prefix.substr(0,x),this->prefix.substr(x),word.substr(x)};
return res;
}
void RadixNode::insert(string word){
// CASE1: the word is the prefix of the nodes
// SOLUTION: set the current node as leaf
if(this->prefix==word){
this->is_leaf=true;
}
// CASE2: the node has no edges that have a prefix to the word
// SOLUTION: create an edge from the current node to a new one
else if(this->nodes.count(word.substr(0,1))==0){
this->nodes.insert(make_pair(word.substr(0,1),RadixNode(prefix=word, is_leaf=True)));
}else{
RadixNode incoming_node = this->nodes[word.substr(0,1)];
vector<string> res = incoming_node.match(word);
string matching_string = res[0];
string remaining_prefix = res[1];
string remaining_word = res[2];
// CASE3: the node prefix is equal to the matching
// SOLUTION: insert remaining word on the next node
if(remaining_prefix==""){
this->nodes[matching_string.substr(0,1)].insert(remaining_word);
}
// CASE4: thw word is greater equal to the matching
// SOLUTION: create a node in between both nodes, change prefixes and add the new node for the remaining word
else{
incoming_node.setprefix(remaining_prefix);
RadixNode aux_node = this->nodes[matching_string.substr(0,1)]
this->nodes[matching_string.substr(0,1)] = RadixNode(matching_string, false);
this->nodes[matching_string.substr(0,1)].setnodes(remaining_prefix.substr(0,1),aux_node);
if(remaining_word==""){
this->nodes[matching_string.substr(0,1)].setisleaf(true);
}else{
this->nodes[matching_string.substr(0,1)].insert(remaining_word);
}
}
}
return;
}
void RadixNode::insert(vector<string> words){
for(vector<string>::iterator word=words.begin(),word!=words.end(),++word){
this->insert(*word);
}
return;
}
bool RadixNode::find(string word){
return true;
}
bool RadixNode::remove(string word){
return true;
}
void RadixNode::print(int height=0){
if(this->prefix!=""){
string heightstr(height,'-')
printf(heightstr+"%s", this->prefix)
for(auto iter=this->nodes.begin(),iter!=this->nodes.end(),++iter){
iter->second.print(height+1);
}
}
}
void RadixNode::setprefix(string prefix){
this->prefix=prefix;
return;
}
void RadixNode::setnodes(string key, RadixNode node){
this->nodes[key]=node;
return;
}
void RadixNode::setisleaf(bool is_leaf){
this->is_leaf=is_leaf;
return;
}