ZhangzrJerry 2022-12-07 14:21 采纳率: 0%
浏览 25

C++为嘛类外定义失败

运行这段代码的时候会报下面的错误,不知道咋解决

#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;
}
  • 写回答

2条回答 默认 最新

  • 於黾 2022-12-07 14:29
    关注

    RadixNode(string prefix="", bool is_leaf=false);
    这里是函数声明,也叫函数原型,里面不要写默认值之类的乱七八糟的代码,把类型写清楚就行了,变量名都可以缺省
    但是这里不是错误原因
    错误原因是你的函数要求参数类型是string,而你传入的是"",这是个char[],类型不一致

    评论 编辑记录

报告相同问题?

问题事件

  • 创建了问题 12月7日

悬赏问题

  • ¥15 C++ 句柄后台鼠标拖动如何实现
  • ¥15 有人会SIRIUS 5.8.0这个软件吗
  • ¥30 comsol仿真等离激元
  • ¥15 静电纺丝煅烧后如何得到柔性纤维
  • ¥15 (标签-react native|关键词-镜像源)
  • ¥100 照片生成3D人脸视频
  • ¥15 伪装视频时长问题修改MP4的时长问题,
  • ¥15 JETSON NANO
  • ¥15 VS开发qt时如何在paintgl函数中用pushbutton控制切换纹理
  • ¥20 关于 openpyxl 处理excel文件地问题