0

sorry, this might be a really stupid beginner question. I'm trying to compile a program, but i just keep getting errors. I've watched a few tutorials and read threads, but the method that works for others doesn't seem to work for me. I use windows with cygwin. To compile i use:

g++ -g -std=c++14 -c -IA:\some\lib\dir *.cpp
g++ -g -std=c++14 -IA:\some\lib\dir branch_and_bound.cpp tinyxml2.o vertex.o graph.o my_operators.o utils.o astar.o my_globals.o -o a.exe

I get an error:

/cygdrive/c/Users/juris/AppData/Local/Temp/ccc9S76y.o: In function `operator<<(std::ostream&, Node*)':
/cygdrive/a/git_repos/marsrutai/branch_and_bound.cpp:146: undefined reference to `std::ostream& operator<< <Vertex*, std::map<Vertex*, int, std::less<Vertex*>, std::allocator<std::pair<Vertex* const, int> > > >(std::ostream&, std::map<Vertex*, std::map<Vertex*, int, std::less<Vertex*>, std::allocator<std::pair<Vertex* const, int> > >, std::less<Vertex*>, std::allocator<std::pair<Vertex* const, std::map<Vertex*, int, std::less<Vertex*>, std::allocator<std::pair<Vertex* const, int> > > > > > const&)'
/cygdrive/a/git_repos/marsrutai/branch_and_bound.cpp:146:(.text+0xf98): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `std::ostream& operator<< <Vertex*, std::map<Vertex*, int, std::less<Vertex*>, std::allocator<std::pair<Vertex* const, int> > > >(std::ostream&, std::map<Vertex*, std::map<Vertex*, int, std::less<Vertex*>, std::allocator<std::pair<Vertex* const, int> > >, std::less<Vertex*>, std::allocator<std::pair<Vertex* const, std::map<Vertex*, int, std::less<Vertex*>, std::allocator<std::pair<Vertex* const, int> > > > > > const&)'
/cygdrive/c/Users/juris/AppData/Local/Temp/ccc9S76y.o: In function `Node::calcLowerBound()':
/cygdrive/a/git_repos/marsrutai/branch_and_bound.cpp:278: undefined reference to `std::map<Vertex*, bool, std::less<Vertex*>, std::allocator<std::pair<Vertex* const, bool> > > operator-<Vertex*, bool>(std::map<Vertex*, bool, std::less<Vertex*>, std::allocator<std::pair<Vertex* const, bool> > >, std::map<Vertex*, bool, std::less<Vertex*>, std::allocator<std::pair<Vertex* const, bool> > >)'
/cygdrive/a/git_repos/marsrutai/branch_and_bound.cpp:278:(.text+0x1c95): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `std::map<Vertex*, bool, std::less<Vertex*>, std::allocator<std::pair<Vertex* const, bool> > > operator-<Vertex*, bool>(std::map<Vertex*, bool, std::less<Vertex*>, std::allocator<std::pair<Vertex* const, bool> > >, std::map<Vertex*, bool, std::less<Vertex*>, std::allocator<std::pair<Vertex* const, bool> > >)'
collect2: error: ld returned 1 exit status

branch_and_bound.cpp (starting line 138)

...
std::map<Vertex*, std::map<Vertex*, int>> Node::getConstraints(){
    return constraints;
}

std::ostream& operator<<(std::ostream& os, Node *N){
    os << "\r\nNode:" << N -> getLB() << ":\r\n" << N -> getConstraints();
    if(N -> left() != nullptr){
        os << "\r\nlchild " << N -> left();
    }
    if(N -> right() != nullptr){
        os << "\r\nrchild " << N -> right();
    }
    return os;
}

So the error is on N -> getConstraings();, It's a map in a map, i'm trying to print it.

branch_and_bound.hpp

#ifndef _Branch_and_bound
#define _Branch_and_bound

#include "vertex.hpp"
#include "graph.hpp"
#include "utils.hpp"

#include "my_globals.hpp"
#include "my_operators.hpp"


class Node{
...

Important line here is the inclusion of utils.hpp

utils.hpp

#ifndef _utils
#define _utils

#include <stdlib.h>
#include <map>
#include <string>
#include <iostream>
#include <chrono>
#include <fstream>

#include "coord_visual.cpp"
#include "vertex.hpp"
#include "graph.hpp"

#include "my_globals.hpp"

void printPath(Tour ll, std::string name = "", bool showSize = true);
void printMap(std::map<Vertex*, auto> map, std::string name = "");
void printVector(std::vector<auto> v);
void timestamp(bool renew = false);
Graph* generateRandomGraph(int v_count = 0, int link_count = 10);
void exportGraphToCSV(Graph &gg, std::string name = "");
void swap(int &a, int &b);
std::ostream& recursiveMapPrint(std::ostream& os, const auto not_map, int recurse = 1);
std::ostream& recursiveMapPrint(std::ostream& os, const std::map<auto, auto> &M, int recurse = 0);
template <class T> std::vector<T> operator-(std::vector<T> A, std::vector<T> B);
template <class K, class V> std::map<K, V> operator-(std::map<K, V>A, std::map<K, V>B);
std::ostream& operator<<(std::ostream& os, const std::map<auto, auto> &M);
std::ostream& operator<<(std::ostream& os, const std::vector<auto> &V);
std::ostream& operator<<(std::ostream& os, Vertex *V);
template <class T> std::vector<T> operator-(std::vector<T> A, std::vector<T> B);
template <class K, class V> std::map<K, V> operator-(std::map<K, V>A, std::map<K, V>B);
template <class K, class V> std::ostream& operator<<(std::ostream& os, const std::map<K, V> &M);
template <class T> std::ostream& operator<<(std::ostream& os, const std::vector<T> &V);
Tour twoOptSwap(Tour T, int from, int to);

#endif

utils.cpp

...
template <class K, class V> std::ostream& operator<<(std::ostream& os, const std::map<K, V> &M){
    recursiveMapPrint(os, M);
    return os;
}

So should fall into the shown utils.cpp function, but instead get's a undefined refference. If i copy that function to branch_and_bound, then error simply moves up 1 step:

/cygdrive/c/Users/juris/AppData/Local/Temp/ccB8CmGa.o: In function `std::ostream& operator<< <Vertex*, std::map<Vertex*, int, std::less<Vertex*>, std::allocator<std::pair<Vertex* const, int> > > >(std::ostream&, std::map<Vertex*, std::map<Vertex*, int, std::less<Vertex*>, std::allocator<std::pair<Vertex* const, int> > >, std::less<Vertex*>, std::allocator<std::pair<Vertex* const, std::map<Vertex*, int, std::less<Vertex*>, std::allocator<std::pair<Vertex* const, int> > > > > > const&)':
/cygdrive/a/git_repos/marsrutai/branch_and_bound.cpp:170: undefined reference to `std::ostream& recursiveMapPrint<Vertex*, std::map<Vertex*, int, std::less<Vertex*>, std::allocator<std::pair<Vertex* const, int> > > >(std::ostream&, std::map<Vertex*, std::map<Vertex*, int, std::less<Vertex*>, std::allocator<std::pair<Vertex* const, int> > >, std::less<Vertex*>, std::allocator<std::pair<Vertex* const, std::map<Vertex*, int, std::less<Vertex*>, std::allocator<std::pair<Vertex* const, int> > > > > > const&, int)'
/cygdrive/a/git_repos/marsrutai/branch_and_bound.cpp:170:(.text$_ZlsIP6VertexSt3mapIS1_iSt4lessIS1_ESaISt4pairIKS1_iEEEERSoSA_RKS2_IT_T0_S3_ISB_ESaIS5_IKSB_SC_EEE[_ZlsIP6VertexSt3mapIS1_iSt4lessIS1_ESaISt4pairIKS1_iEEEERSoSA_RKS2_IT_T0_S3_ISB_ESaIS5_IKSB_SC_EEE]+0x22): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `std::ostream& recursiveMapPrint<Vertex*, std::map<Vertex*, int, std::less<Vertex*>, std::allocator<std::pair<Vertex* const, int> > > >(std::ostream&, std::map<Vertex*, std::map<Vertex*, int, std::less<Vertex*>, std::allocator<std::pair<Vertex* const, int> > >, std::less<Vertex*>, std::allocator<std::pair<Vertex* const, std::map<Vertex*, int, std::less<Vertex*>, std::allocator<std::pair<Vertex* const, int> > > > > > const&, int)'
collect2: error: ld returned 1 exit status

It seems as if branch_and_bound is not linking with utils, it also feels like i'm being retarded and missing something obvious. What am i doing wrong here?

  • template must be implemented in header – Danh Jan 09 '17 at 09:39
  • `Each name that ... begins with an underscore followed by an uppercase letter is reserved to the implementation for any use`. You may not define the macro `_Branch_and_bound`. Pick another identifier, that follows the rules instead. – eerorika Jan 09 '17 at 09:39

0 Answers0