samedi 19 octobre 2019

what does it mean by Undefined reference?

I tried to compile all my cpp files in visual studio code but it's giving error message all the time!

I have tried to include all the header files.

c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\nabil\AppData\Local\Temp\cctJte6p.o:main.cpp:(.text+0x66): undefined reference to `ArrayBag::add(int const&)'

int main()
{
ArrayBag <int> obj1;`enter code here`
ArrayBag <int> obj2;
obj1.add(4);
obj2.add(7);

obj1+=obj2; // obj1 is invisible & its calling the obj2 of Array bag and 
trying to add it with obj1
obj1.display();
obj1-=obj2;
obj2.display();
return 0;`enter code here`
}

enter code here

#ifndef ARRAY_BAG_
#define ARRAY_BAG_

#include <iostream>
#include <vector>

template<class T>
class ArrayBag
{

public:
/** default constructor**/
ArrayBag();

bool add(const T& new_entry);
bool remove(const T& an_entry);
void operator+=(const ArrayBag<T>& a_bag);
void display() const;
void operator-=(const ArrayBag<T> & a_bag);
};
#endif

#include "ArrayBag.h"
template<class T>
bool ArrayBag<T>::add(const T& new_entry)
{
bool has_room = (item_count_ < DEFAULT_CAPACITY);

if (has_room && getFrequencyOf(new_entry)==0)
{
items_[item_count_] = new_entry;
item_count_++;
return true;
}  // end if

return false;
}  // end add

template<class T>
void ArrayBag<T>::display() const{
for(int i=0;i<item_count_;i++){
std::cout << items_[i] << std::endl;

}

enter code here

Aucun commentaire:

Enregistrer un commentaire