Sorry if it's a duplicate question, but I've been searching for solution since yesterday and I haven't found out yet..
I'm using CLion on mac, and my compiler is from xcode so i think it's clang. Any help would be appreciated!! Do I need to use gcc? or do I need to fix my cmake?
here's an error message.
[ 33%] Linking CXX executable bag
Undefined symbols for architecture x86_64:
"Bag<int>::add(int)", referenced from:
_main in main.cpp.o
"Bag<int>::print()", referenced from:
_main in main.cpp.o
"Bag<int>::Bag()", 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)
make[3]: *** [bag] Error 1
make[2]: *** [CMakeFiles/bag.dir/all] Error 2
make[1]: *** [CMakeFiles/bag.dir/rule] Error 2
make: *** [bag] Error 2
Here's main.cpp
#include <iostream>
#include "Bag.h"
int main() {
Bag<int> temp;
temp.add(1);
temp.print();
cout << endl;
return 0;
}
Here's Bag.h.
#ifndef BAG_BAG_H
#define BAG_BAG_H
#include <vector>
#include <cstdlib>
#include <iostream>
using namespace std;
static const size_t CAPACITY = 100;
template <class T>
class Bag{
public:
Bag();
size_t size() const;
bool empty();
bool check(const T& item);
void resize(size_t new_size);
void clear();
void remove(const T& item);
void add(T item);
void print();
private:
T* data;
size_t _size;
};
#endif //BAG_BAG_H
Here's Bag.cpp.
#include "Bag.h"
template <class T>
Bag<T>::Bag() : data{new T[CAPACITY]}, _size{0} {}
template <class T>
size_t Bag<T>::size() const {return _size;}
template <class T>
bool Bag<T>::empty() {
}
template <class T>
bool Bag<T>::check(const T &item) {
}
template <class T>
void Bag<T>::resize(size_t new_size) {
T* temp = new T[new_size];
for(int i = 0; i == _size; i++)
*data++ = *temp++;
delete [] data;
this->data = &temp;
}
template <class T>
void Bag<T>::add(T item) {
data[_size + 1] = item;
_size++;
}
template <class T>
void Bag<T>::print() {
for(int i = 0; i == _size; i++)
cout << data[i] << " ";
}
here's CMakeLists.txt
cmake_minimum_required(VERSION 3.15)
project(bag)
set(CMAKE_CXX_STANDARD 14)
add_executable(bag main.cpp Bag.cpp Bag.h)