簡體   English   中英

未定義引用“其他 function 名稱”中的“函數名稱”C++

[英]undefined reference to "function name" in "other function name" C++

作為項目的一部分,我試圖從 Tree cpp 文件調用 function 到 Graph cpp,但編譯器向我拋出一個錯誤,指出 function iam 調用未定義 2 天,我一直在嘗試解決此問題但沒有成功

我得到的錯誤是:

bin/Graph.o: In function `Graph::BFS(Session&, int)':
/home/spl211/CLionProjects/Assign1/src/Graph.cpp:24: undefined reference to `Tree::createTree(Session const&, int)'
/home/spl211/CLionProjects/Assign1/src/Graph.cpp:39: undefined reference to `Tree::createTree(Session const&, int)'

這是我的圖形 cpp:誰實現了調用 createTree 的 BFS 算法(問題出在哪里)

#include "../include/Graph.h"
#include "../include/Tree.h"
class Tree;


//--constructor--//
Graph::Graph():edges(),size(),neighbours(),infV(){}
Graph::Graph(std::vector<std::vector<int>> matrix) : edges({{}}), size(0), neighbours({}), infV({}) {
    edges = matrix;
    size = edges.size();
    neighbours = matrix;
    for (int i = 0; i < edges.size(); i++) {
        infV[i] = 0;
    }
};
//--getter--//

std::vector<std::vector<int>> Graph::getEdg()const{
    return edges;
};

// --function--//
int Graph::BFS(Session &session, int node) {
    Tree *BfsTree = Tree::createTree(session, node);
    //--build bfs--//
    std::queue<Tree *> que;
    std::vector<bool> visited;
    for (int i = 0; i < size; i++) {
        visited.push_back(false);
    }
    que.push(BfsTree);
    visited[node] = true;
    while (!que.empty()) {
        Tree *curr_root = que.front();
        int nodeId = curr_root->getNode();
        que.pop();
        for (int j = 0; j < edges[nodeId].size(); j++) {
            if (edges[nodeId][j] == 1 && !visited[j]) {   //if they are neighbours
                Tree *child = Tree::createTree(session, j); //create new child
                curr_root->addChild(child); //add the child to the tree
                que.push(child); //add the child to que
                visited[j] = true;  //mark him as visited
            }
        }

    }
    //--end of bfs--//
    //--now we have shortest path tree for our sick node--//
    int output = BfsTree->traceTree();
    delete (BfsTree);
    return output;
}

void Graph::infectNode(int nodeInd) {
    infV[nodeInd] = 1;
};

bool Graph::isInfected(int nodeInd) {
    return infV[nodeInd] == 1;
};

這是我的 Tree cpp,它實現了 createTree:

#include "../include/Tree.h"
#include "../include/Session.h"
#include <vector>

//--constructor--//
Tree::Tree():node(0),children(){};
Tree::Tree(int rootLabel):node(0),children({}){
node = rootLabel;
}
//--destructor--//
Tree::~Tree(){
    for(Tree* tree:children){
        delete(tree);
        tree= nullptr;
    }
};
//----------------RULE OF 5------------------//
//--copy constructor--//
Tree::Tree(const Tree &other):node(0),children(){
    node = other.getNode();
    for(Tree* child:other.children){
        Tree* clone = child->clone();
        children.push_back(clone);
    }
}
//--copy assignment operator--//
const Tree& Tree::operator=(const Tree& other){
    if(this==&other){return *this;}
    for(Tree* child:children){
        delete (child);
    }
    for(Tree* child:other.children){
        Tree* clone = child->clone();
        children.push_back(clone);
    }
    node = other.getNode();

}
//--move constructor--//
Tree::Tree(Tree&& other){
    node = other.getNode();
    for(Tree* tree:other.children){
        addChild(tree);
        tree= nullptr;
    }
}
//--move assignment operator--//
const Tree& Tree::operator=(Tree& other){
    if(this==&other){return *this;}
    node = other.getNode();
    for(Tree* child:children){
        delete (child);
    }
    for(Tree* tree:other.children){
        addChild(tree);
        tree= nullptr;
    }
}

//----------------------------------------//


//---FUNCTIONS---//

    //--getters--//
    int Tree::getNode()const{
    return this->node;
    }
    std::vector<Tree*> Tree::getChildren(){
    return children;
    };


    //--setters--//
    void Tree::setNode(int node){
    this->node = node;
    };

   //-add children--//
    void Tree::addChild(const Tree& child){
    Tree* clone = child.clone();
    children.push_back(clone);
    };

    void Tree::addChild(Tree* child){
       children.push_back(child);
    };

    //--create tree--//
    static Tree* createTree(const Session& session, int rootLabel){
        if(session.getTreeType()==Cycle){
            return new CycleTree(rootLabel,session.getCycle());
        }
        if(session.getTreeType()==MaxRank){
            return new MaxRankTree(rootLabel);
        }
        if(session.getTreeType()==Root){
            return new RootTree(rootLabel);
        }
    };
    //--max function--//
    void Tree::maxChild(Tree* tree,int *max,int *node){
        if(this->getChildren().size()>*max){
            *(max) = getChildren().size();
            *(node)=this->getNode();
        }
        if(tree->getChildren().size()>0){
            for(Tree* child:this->getChildren()){
                maxChild(child,max,node);
            }

        }
    }
 //------------inherent------------//
 //--cycle--//
   CycleTree::CycleTree(int rootLabel, int currCycle):Tree(rootLabel),currCycle(currCycle){} ;
    int CycleTree::traceTree(){
        int curr_node = this->getNode();
        Tree* curr_tree =this;
        int cycleCount=currCycle;
        while(cycleCount>=0&&curr_tree->getChildren().size()>0){
            curr_tree = curr_tree->getChildren()[0];
            curr_node = curr_tree->getNode();
            cycleCount--;
        }
        return curr_node;
    };
   Tree* CycleTree::clone()const{return new CycleTree(*this);};

//--max--//
    MaxRankTree::MaxRankTree(int rootLabel):Tree(rootLabel){};
     int MaxRankTree::traceTree(){
         int index =this->getNode();
         int* node = &index;
         int _max = this->getChildren().size();
         int *max = &_max;
         for(Tree* child:this->getChildren()){
            maxChild(child,max,node);
         }
        return *node;
     };

     Tree* MaxRankTree::clone()const{return new MaxRankTree(*this);};
//--root--//
  RootTree::RootTree(int rootLabel):Tree(rootLabel){};

    int RootTree::traceTree(){return this->getNode();};
    Tree* RootTree::clone()const{return new RootTree(*this);};

這是我的制作文件:

# define some Makefile variables for the compiler flags
# to use Makefile variables later in the Makefile: $()

CC = g++
CFLAGS = -g -Wall -Weffc++ -std=c++11



# All Targets
all:Assign1

# Tool invocations
#Executable

Assign1:bin/main.o bin/Agent.o bin/Graph.o bin/Session.o bin/Tree.o

    @echo 'Building target: Assign1'
    @echo 'Invoking: C++ Linker'
    $(CC) -o bin/main bin/Agent.o bin/Graph.o bin/Session.o bin/Tree.o
    @echo 'Finished building target: Assign1'
    @echo ' '

#Depends on the source and header files
bin/main.o: src/main.cpp
    $(CC) $(CFLAGS) -c -Iinclude -o bin/main.o src/main.cpp

#Depends on the source and header files
bin/Agent.o: src/Agent.cpp
    $(CC) $(CFLAGS) -c -Iinclude -o bin/Agent.o src/Agent.cpp

#Depends on the source and header files
bin/Graph.o: src/Graph.cpp
    $(CC) $(CFLAGS) -c -Iinclude -o bin/Graph.o src/Graph.cpp

#Depends on the source and header files
bin/Session.o: src/Session.cpp
    $(CC) $(CFLAGS) -c -Iinclude -o bin/Session.o src/Session.cpp

#Depends on the source and header files
bin/Tree.o: src/Tree.cpp
    $(CC) $(CFLAGS) -c -Iinclude -o bin/Tree.o src/Tree.cpp

#CLean the build directory
clean:
    rm -f bin/*

我明白了,你的方法:

static Tree* createTree(const Session& session, int rootLabel){

在你的cpp文件中是static。 因此它在其他文件中是不可見的。

您可能打算創建以下方法:

Tree* Tree::createTree(const Session& session, int rootLabel){

同一個正文,但簽名應該是第二個。

您將createTree定義為:

static Tree* createTree(...)

這定義了一個成員 function,它具有對其他翻譯單元不可見的內部鏈接。

如果它應該是 static成員function 然后將其定義為成員 function(但沒有static關鍵字):

Tree* Tree::createTree(...)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM