I am desperate for help with these last few errors. Error1 error C2801: \'Lab3::
ID: 3639583 • Letter: I
Question
I am desperate for help with these last few errors.Error1 error C2801: 'Lab3::operator =' must be a non-static member c:userssuperbeedydesktophashtable1hashtablehashtable.cpp 34 1 HashTable
Error2 error C2780: '_OutTy *std::copy(_InIt,_InIt,_OutTy (&)[_OutSize])' : expects 3 arguments - 1 provided c:userssuperbeedydesktophashtable1hashtablehashtable.cpp 35 1 HashTable
Error3 error C2780: '_OutIt std::copy(_InIt,_InIt,_OutIt)' : expects 3 arguments - 1 provided c:userssuperbeedydesktophashtable1hashtablehashtable.cpp 35 1 HashTable
Here is my code.
Thanks
#################################################################################################
//main.cpp
#define CRTDBG_MAP_ALLOC
#include "hashtable.h"
#include "stock.h"
#include <stdlib.h>
#include <crtdbg.h>
#include <iostream>
namespace Lab3
{
void printHeader(char delimiter)
{
std::cout << "symbol" << delimiter << "name"<<delimiter<<"date"<<delimiter<<"net assert value" << delimiter << "Year to Date Return" << std::endl;
std::cout << "------" << delimiter << "----"<<delimiter<<"----"<<delimiter<<"----------------" << delimiter << "-------------------" << std::endl;
}
hashTable *myMap;
}
void PrintMenu()
{
std::cout << "Please input your select" << std::endl;
std::cout << "1 - display" << std::endl;
std::cout << "2 - load file" << std::endl;
std::cout << "3 - save file" << std::endl;
std::cout << "4 - add security" << std::endl;
std::cout << "5 - remove security" << std::endl;
std::cout << "6 - retrieve security" << std::endl;
std::cout << "7 - modify security" << std::endl;
std::cout << "8 - monitor, list chain length" << std::endl;
std::cout << "9 - exit" << std::endl;
}
void ServeAddSecurity()
{
std::cout << "please input Ticker Symbol: ";
char symbol[128];
std::cin.ignore(std::cin.rdbuf()->in_avail());
std::cin.getline(symbol,128);
std::cout << "please input Stock or Fund Name:";
char name[256];
std::cin.ignore(std::cin.rdbuf()->in_avail());
std::cin.getline(name,256);
std::cout << "please input Net Asset Value:";
double netAssetValue;
std::cin.ignore(std::cin.rdbuf()->in_avail());
std::cin >> netAssetValue;
std::cout << "please input Date of that Value:";
char date[128];
std::cin.ignore(std::cin.rdbuf()->in_avail());
std::cin.getline(date,128);
std::cout << "please input Year to Date Return:";
double yearDateReturn;
std::cin.ignore(std::cin.rdbuf()->in_avail());
std::cin >> yearDateReturn;
Lab3::myMap->add(Lab3::stock(symbol,name,netAssetValue,date,yearDateReturn));
}
void ServeRemoveSecurity()
{
std::cout << "please input Ticker Symbol: " <<std::endl;
char symbol[128];
std::cin.ignore(std::cin.rdbuf()->in_avail());
std::cin.getline(symbol,128);
Lab3::myMap->remove(symbol);
}
void ServeRetrieveSecurity()
{
std::cout << "please input Ticker Symbol: " <<std::endl;
char symbol[128];
std::cin.ignore(std::cin.rdbuf()->in_avail());
std::cin.getline(symbol,128);
Lab3::stock result;
Lab3::myMap->retrieve(symbol,result);
std::cout << "retrive result " << std::endl;
Lab3::printHeader(' ');
result.print(std::cout,' ');
}
void ServeModifySecurity()
{
std::cout << "please input Ticker Symbol: ";
char symbol[128];
std::cin.ignore(std::cin.rdbuf()->in_avail());
std::cin.getline(symbol,128);
std::cout << "please input Net Asset Value:";
float netAssetValue;
std::cin.ignore(std::cin.rdbuf()->in_avail());
std::cin >> netAssetValue;
std::cout << "please input Date of that Value:";
char date[128];
std::cin.ignore(std::cin.rdbuf()->in_avail());
std::cin.getline(date,128);
std::cout << "please input Year to Date Return:";
float yearDateReturn;
std::cin.ignore(std::cin.rdbuf()->in_avail());
std::cin >> yearDateReturn;
Lab3::myMap->modify(symbol,netAssetValue,date,yearDateReturn);
}
void main()
{
Lab3::myMap=new Lab3::hashTable(5);
bool running=true;
while(running)
{
PrintMenu();
std::cin.ignore(std::cin.rdbuf()->in_avail());
switch(int choose=getchar())
{
case '1':
std::cout << "Here print all securities " << std::endl;
Lab3::printHeader(' ');
Lab3::myMap->display();
break;
case '2':
std::cout << "Load securities from securities.txt" << std::endl;
Lab3::myMap->loadFile("securities.txt");
break;
case '3':
std::cout << "Save securities to securities.txt" << std::endl;
Lab3::myMap->saveFile("securities.txt");
break;
case '4':
ServeAddSecurity();
break;
case '5':
ServeRemoveSecurity();
break;
case '6':
ServeRetrieveSecurity();
break;
case '7':
ServeModifySecurity();
break;
case '8':
Lab3::myMap->monitor();
break;
case '9':
running=false;
break;
default:
std::cout << "select " <<choose<<" is invalid" << std::endl;
}
}
delete Lab3::myMap;
_CrtDumpMemoryLeaks();
}
##################################################################################################
//hashTable.cpp
#include "hashtable.h"
#include "data.h"
#include "stock.h"
#include "stock.cpp"
#include <iostream>
#include <cstring>
#include <fstream>
#include <sstream>
namespace Lab3
{
//class hashTable;
hashTable::hashTable():m_mapSize(5),m_hashTable(NULL)
{
m_hashTable=new linkedList[m_mapSize];
}
hashTable::hashTable(unsigned int mapSize): m_mapSize(mapSize),m_hashTable(NULL)
{
m_hashTable=new linkedList[mapSize];
}
hashTable::hashTable(hashTable const &other)
{
copy(other);
}
hashTable::~hashTable()
{
delete [] m_hashTable;
}
hashTable &operator=(hashTable const &other)
{
copy(other);
return other;
}
void hashTable::clean()
{
delete [] m_hashTable;
m_hashTable=new linkedList[m_mapSize];
}
void hashTable::add(const stock &aStock)
{
m_hashTable[HashValue(aStock.getSymbol())].add(aStock);
}
bool hashTable::remove(char * tickerSymbol)
{
return m_hashTable[HashValue(tickerSymbol)].remove(tickerSymbol);
}
bool hashTable::modify(char * tickerSymbol, float newNetAssetVal, char * newDate, float newDateReturn)
{
return m_hashTable[HashValue(tickerSymbol)].modify(tickerSymbol,newNetAssetVal,newDate,newDateReturn);
}
void hashTable::display()
{
print(std::cout,' ');
}
void hashTable::print(std::ostream &output,char delimiter)
{
for(unsigned int index=0;index<m_mapSize;index++)
{
m_hashTable[index].print(std::cout,delimiter);
}
}
void hashTable::saveFile(char *path)
{
std::ofstream file(path);
for(unsigned int index=0;index<m_mapSize;index++)
{
m_hashTable[index].print(file,';');
}
}
void hashTable::loadFile(char *path)
{
hashTable clear(m_mapSize);
std::ifstream file(path);
char line[1024];
while(true)
{
char symbol[128];
char name[256];
char netAssetValueStr[128];
char date[128];
char yearDateReturnStr[128];
file.getline(line,1024);
if(!file.is_open()||file.eof()) break;
char * pch= strtok (line,";");
if(pch==NULL) continue;
strcpy(symbol,pch);
pch = strtok (NULL, ";");
if(pch==NULL) continue;
strcpy(name,pch);
pch = strtok (NULL, ";");
if(pch==NULL) continue;
strcpy(date,pch);
pch = strtok (NULL, ";");
if(pch==NULL) continue;
strcpy(netAssetValueStr,pch);
double netAssetValue;
{
std::stringstream parser(netAssetValueStr);
parser >> netAssetValue;
}
pch = strtok (NULL, ";");
if(pch==NULL) continue;
strcpy(yearDateReturnStr,pch);
double yearDateReturn;
{
std::stringstream parser(yearDateReturnStr);
parser >> yearDateReturn;
}
add(stock::stock(symbol,name,netAssetValue,date,yearDateReturn));
}
}
bool hashTable::retrieve(char * tickerSymbol, stock& aStock)
{
return m_hashTable[HashValue(tickerSymbol)].retrieve(tickerSymbol,aStock);
}
void hashTable::monitor()
{
for(unsigned int index=0;index<m_mapSize;index++)
{
std::cout << "Chain "<< index <<" have length " << m_hashTable[index].length() << std::endl;
}
}
unsigned int hashTable::getMapSize() const
{
return m_mapSize;
}
}
##################################################################################################
//hashtable.h
#ifndef HASHTABLE_H
#define HASHTABLE_H
#include "stock.h"
#include "data.h"
#include <fstream>
#include <sstream>
namespace Lab3
{
class hashTable
{
friend class hashTable;
public:
hashTable();
hashTable(unsigned int mapSize);
hashTable(hashTable const &other);
~hashTable();
void clean();
void add(const stock &aStock);
bool remove(char * tickerSymbol);
bool modify(char * tickerSymbol, float newNetAssetVal, char * newDate, float newDateReturn);
void display();
void print(std::ostream &output,char delimiter);
void saveFile(char *path);
void loadFile(char *path);
bool retrieve(char * tickerSymbol, stock& aStock);
void monitor();
unsigned int getMapSize() const;
hashTable &operator=(hashTable const &other);
private:
struct node
{
node():m_next(NULL),m_prev(NULL){}
stock m_data;
node *m_next,*m_prev;
};
void copy(hashTable const &other);
unsigned char HashValue(char const *value);
linkedList *m_hashTable;
unsigned int m_mapSize;
};
}
#endif
##################################################################################################
//data.cpp
#include "data.h"
namespace Lab3
{
linkedList::linkedList():m_root(NULL){}
linkedList::~linkedList()
{
clean();
}
void linkedList::clean()
{
node *curNode=m_root;
while(curNode!=NULL)
{
node *nextNode=curNode->m_next;
delete curNode;
curNode=nextNode;
}
}
unsigned int linkedList::length() const
{
unsigned int result=0;
node *curNode=m_root;
while(curNode!=NULL)
{
++result;
curNode=curNode->m_next;
}
return result;
}
void linkedList::add(const stock &aStock)
{
node *prepareNode=new node();
prepareNode->m_data=aStock;
if(m_root==NULL)
{
m_root=prepareNode;
return;
}
node *node=m_root;
if(strcmp(node->m_data.getSymbol(),aStock.getSymbol())>0)
{
Insert(NULL,prepareNode);
return;
}
while(node!=NULL)
{
if(node->m_next==NULL)
{
Insert(node,prepareNode);
return;
}
if(strcmp(node->m_next->m_data.getSymbol(),aStock.getSymbol())>0)
{
Insert(node,prepareNode);
return;
}
node=node->m_next;
}
}
bool linkedList::remove(char * tickerSymbol)
{
node *removeNode=FindNode(tickerSymbol);
if(removeNode==NULL) return false;
if(removeNode->m_next!=NULL) removeNode->m_next->m_prev= removeNode->m_prev;
if(removeNode->m_prev!=NULL) removeNode->m_prev->m_next= removeNode->m_next;
if(m_root==removeNode) m_root=removeNode->m_next;
delete removeNode;
return true;
}
bool linkedList::modify(char * tickerSymbol, float newNetAssetVal, char * newDate, float newDateReturn)
{
node *targetNode=FindNode(tickerSymbol);
if(targetNode==NULL) return false;
targetNode->m_data.setDate(newDate);
targetNode->m_data.setNetAssertValue(newNetAssetVal);
targetNode->m_data.setYearDateReturn(newDateReturn);
return true;
}
bool linkedList::retrieve(char * tickerSymbol, stock& aStock)
{
node *targetNode=FindNode(tickerSymbol);
if(targetNode==NULL) return false;
aStock=targetNode->m_data;
return true;
}
/*void display()
{
print(std::cout);
}
*/
void linkedList::print(std::ostream &output,char delimiter)
{
node *curNode=m_root;
while(curNode!=NULL)
{
curNode->m_data.print(output,delimiter);
curNode=curNode->m_next;
}
}
void linkedList::Insert(node *preNode,node *insertNode)
{
if(preNode==NULL)
{
insertNode->m_next=m_root;
m_root->m_prev=insertNode;
m_root=insertNode;
}
else
{
insertNode->m_next=preNode->m_next;
if(preNode->m_next!=NULL) preNode->m_next->m_prev=insertNode;
insertNode->m_prev=preNode;
preNode->m_next=insertNode;
}
}
}
##################################################################################################
//data.h
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
using namespace std;
#include "stock.h"
namespace Lab3
{
class linkedList
{
private:
struct node
{
node():m_next(NULL),m_prev(NULL){}
stock m_data;
node *m_next,*m_prev;
};
public:
linkedList();
~linkedList();
void clean();
unsigned int length() const;
void add(const stock &aStock);
bool remove(char * tickerSymbol);
bool modify(char * tickerSymbol, float newNetAssetVal, char * newDate, float newDateReturn);
bool retrieve(char * tickerSymbol, stock& aStock);
void print(std::ostream &output,char delimiter);
private:
node *FindNode(char *tickerSymbol)
{
node *curNode=m_root;
while(curNode!=NULL)
{
if(!strcmp(curNode->m_data.getSymbol(),tickerSymbol)) return curNode;
curNode=curNode->m_next;
}
return NULL;
}
void Insert(node *preNode,node *insertNode);
node *m_root;
};
}
#endif
##################################################################################################
//stock.cpp
#include "stock.h"
#include <iostream>
namespace Lab3
{
stock::stock():
m_symbol(NULL),
m_name(NULL),
m_netAssetValue(0.0),
m_date(NULL),
m_yearDateReturn(0.0)
{
}
stock::stock(char *symbol,char *name,double netAssetValue,char *date,double yearDateReturn)
{
m_symbol=(char*)malloc(strlen(symbol)+1);
strcpy(m_symbol,symbol);
m_name=(char*)malloc(strlen(name)+1);
strcpy(m_name,name);
m_date=(char*)malloc(strlen(date)+1);
strcpy(m_date,date);
m_netAssetValue=netAssetValue;
m_yearDateReturn=yearDateReturn;
}
/// copy constructor
stock::stock(stock const &src)
{
copy(src);
}
/// assignment operator
const stock& stock::operator=(stock const &src)
{
copy(src);
return src;
}
stock::~stock()
{
clean();
}
/// All copy functionality (assignment operation, copy constructor) are instead use this function,
void stock::copy(stock const &src)
{
clean();
m_symbol=(char*)malloc(strlen(src.getSymbol())+1);
strcpy(m_symbol,src.getSymbol());
m_name=(char*)malloc(strlen(src.getName())+1);
strcpy(m_name,src.getName());
m_date=(char*)malloc(strlen(src.getDate())+1);
strcpy(m_date,src.getDate());
m_netAssetValue=src.m_netAssetValue;
m_yearDateReturn=src.m_yearDateReturn;
}
double stock::getNetAssertValue() const
{
return m_netAssetValue;
}
double stock::getYearDateReturn() const
{
return m_yearDateReturn;
}
void stock::setName(char *name)
{
if(m_name!=NULL) free(m_name);
m_name=(char*)malloc(strlen(name)+1);
strcpy(m_name,name);
}
/// set the date of this security
void stock::setDate(char *date)
{
if(m_date!=NULL) free(m_date);
m_date=(char*)malloc(strlen(date)+1);
strcpy(m_date,date);
}
void stock::setNetAssertValue(double netAssetValue)
{
m_netAssetValue=netAssetValue;
}
void stock::setYearDateReturn(double yearDateReturn)
{
m_yearDateReturn=yearDateReturn;
}
/// reset this security
void stock::clean()
{
if(m_symbol!=NULL) free(m_symbol);
if(m_name!=NULL) free(m_name);
if(m_date!=NULL) free(m_date);
m_symbol=NULL;
m_name=NULL;
m_date=NULL;
m_netAssetValue=0;
m_yearDateReturn=0;
}
void stock::print(std::ostream &output,char delimiter)
{
output << m_symbol << delimiter << m_name << delimiter << m_date << delimiter << m_netAssetValue << delimiter << m_yearDateReturn << delimiter << std::endl;
}
}
#################################################################################################
//stock.h
#ifndef STOCK_H_
#define STOCK_H_
#include <iostream>
namespace Lab3
{
struct stock
{
public:
stock();
stock(char *symbol,char *name,double netAssetValue,char *date,double yearDateReturn);
stock(stock const &src);
stock const &operator=(stock const &src);
~stock();
void copy(stock const &src); /// All copy functionality (assignment operation, copy constructor) are instead use this function,
char const *getSymbol() const
{
return m_symbol;
}
char const *getName() const
{
return m_name;
}
char const *getDate() const
{
return m_date;
}
double getNetAssertValue() const; ///< Get Net Assert Value
double getYearDateReturn() const; ///< Get Year Date of Return
void setName(char *name);
void setDate(char *date);
void setNetAssertValue(double netAssetValue);
void setYearDateReturn(double yearDateReturn);
void clean();
void print(std::ostream &output,char delimiter);
private:
char *m_symbol;
char *m_name;
double m_netAssetValue;
char *m_date;
double m_yearDateReturn;
};
}
#endif
Explanation / Answer
So in hashTable.cpp you're not declaring the operator= function as part of the hastTable class, which it is. It should read const hashTable& hashTable::operator=(hashTable const &other) { copy(other); return other; } You're also going to want to edit hashTable.h and change the declaration of your operator= function to return a const (Either that or remove const from the parameter)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.