我试图在Visual Studio代码中编译我所有的cpp文件,但是它一直在显示错误消息!
我试图包括所有头文件。
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):未定义对`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