0

The program is supposed to cout "hello world I am working".

It runs when I put the method definition "newNode" in the header file, but won't work when it is in the src file.

main.cpp

#include "BreadthFirstSearch.h"

int main() {
    BreadthFirstSearch<int> bfs;

    bfs.newNode(1, 2);
}

BreadthFirstSearch.h

#ifndef BFS_BREADTHFIRSTSEARCH_H
#define BFS_BREADTHFIRSTSEARCH_H

#include <list>
#include <map>
#include <iostream>

using namespace std;


template<typename T>
class BreadthFirstSearch {

    map<T, list<T>> mapOfNodes;

public:
    BreadthFirstSearch(const map<T, list<T>> &mapOfNodes);

    void newNode(T primary, T secondary);

};

BreadthFirstSearch.cpp

#include <iostream>
#include "BreadthFirstSearch.h"

template<typename T>
void BreadthFirstSearch<T>::newNode(T primary, T secondary) {
    std::cout << "hello world i am working" << std::endl;
}

Note I am using Cmake

cmake_minimum_required(VERSION 3.21)
project(BFS)

set(CMAKE_CXX_STANDARD 17)

add_executable(BFS main.cpp BreadthFirstSearch.cpp BreadthFirstSearch.h)

Terminal returns

Linking CXX executable BFS
FAILED: BFS 
: && /Library/Developer/CommandLineTools/usr/bin/c++ -g -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX11.3.sdk -Wl,-search_paths_first -Wl,-headerpad_max_install_names  CMakeFiles/BFS.dir/main.cpp.o CMakeFiles/BFS.dir/BreadthFirstSearch.cpp.o -o BFS   && :
Undefined symbols for architecture x86_64:
  "BreadthFirstSearch<int>::newNode(int, int)", referenced from:
      _main in main.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.
Ben
  • 11
  • 2

0 Answers0