c++makefileundefined-referenceundefined-function

undefined reference to "function name" in "other function name" C++


as a part of a project iam trying to call a function from Tree cpp file to Graph cpp but the compailer throw me an error saying the function iam callin to is not define for 2 days i have been trying solving this without success

the error i get is:

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)'

this is my graph cpp : who implements BFS algorithem which calls createTree(where the problem is)

#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;
};

and this is my Tree cpp which implements 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);};

and this is my make file:

# 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/*

Solution

  • I see, your method:

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

    is static in your cpp file. Hence it's not visible from other files.

    Probably you meant to create the following method:

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

    Same body, but the signature should be the second one.