vendredi 4 septembre 2020

I am trying to understand how template classes work, but I keep getting compile issues [duplicate]

I am trying to better understand how template classes work. I thought that once you declare a templated class object in main, you pick the type that you want. Then when you call the function, you pass in the same value and the compiler will recognize what type you want. When I create the class I get ld: symbol(s) not found for architecture x86_64. I know there is something I am missing but I cant figure it out. If anyone could help that would be greatly appreciated.

main.cpp

#include "dynamicStack.h"

int main()
{

dynamicStack<string> str;

str.push();

return 0;
}

dynamicStack.cpp

#include "dynamicStack.h"

template <class T>
void dynamicStack<T>::push()
{
   cout << "Hello ";
}

template <class T>
dynamicStack<T>::dynamicStack()
{
}

template <class T>
dynamicStack<T>::~dynamicStack()
{
}

dynamicStack.h

#ifndef __DYNAMICSTACK_H_INCLUDED__
#define __DYNAMICSTACK_H_INCLUDED__
#include <iostream>
#include <string>
using namespace std;

template <class T>
class dynamicStack
{
public:
  dynamicStack();
  ~dynamicStack();

  void push();

private: 
  T data;
};
#endif

ERROR:

Undefined symbols for architecture x86_64: "dynamicStack<std::__cxx11::basic_string<char, std::char_traits, std::allocator > >::push()", referenced from: _main in cccQUr0w.o "dynamicStack<std::__cxx11::basic_string<char, std::char_traits, std::allocator > >::dynamicStack()", referenced from: _main in cccQUr0w.o "dynamicStack<std::__cxx11::basic_string<char, std::char_traits, std::allocator > >::~dynamicStack()", referenced from: _main in cccQUr0w.o ld: symbol(s) not found for architecture x86_64 collect2: error: ld returned 1 exit status

Aucun commentaire:

Enregistrer un commentaire