Hi all.
I'm trying to define a class in a header file, and implement it separately. However, my compiler is telling me that the class is being redefined. I recognize what the error is telling me, but I'm not sure what to do to fix it.
the header file:
and the source file:Code:#include <string> using namespace std; #ifndef VENDORFUNCS_HPP_ #define VENDORFUNCS_HPP_ template<class t> void copyArray(t* from, t** dest, unsigned int length, bool assignval=true); class Vendor{//<<<<<ERROR HERE>>>>> public: Vendor(string vendorname, string* productnames, int slength, double* prodprices, int dlength); bool purchase(string name, double withmoney); string getName(); double getPrice(string product); string* valuesUnder(double d, int &arrlength); }; #endif
I know what I could do if I was not using private variables in the class, but I'm hoping there is some way to do this without taking them out.Code:#include <string> #include "VendorFuncs.hpp" template <class t> void copyArray(t* from, t** dest, unsigned int length, bool assignval){ if(assignval){ *dest = new t[length]; } for(unsigned int i=0; i<length; i++){ (*dest)[i] = from[i]; } } class Vendor{//<<<<<<ERROR HERE>>>>>> string name; string* products; double* prices; int productNumber; int findProduct(String name){ for(int i=0; i<productNumber; i++){ if(products[i]==name) return i; } } public: Vendor(string vendorname, string* productnames, double* prodprices, int numProducts){ name=vendorname; copyArray(productnames, &products, slength); copyArray(prodprices, &prices, dlength); productNumber=numProducts; } bool purchase(string pname, double money){ int indx = findProduct(pname); if(prices[indx]>money) return false; else return true; } string getname(){ return name; } double getPrice(String pname){ return prices[findProduct(pname)]; } string* valuesUnder(double amt, int &lenindic){ lenindic=0; string* arr = new string[productNumber]; for(int i=0; i<productNumbet; i++){ if(prices[i]<=amt){ arr[lenindic]=products[i]; lenindic+=1; } } return arr; } };




Reply With Quote