I am trying to do load balancing in c++. But getting error. I have copied the code from youtube and it works fine there. code is
//arrayqueue.h
#define QUEUE_H
#pragma once
template <class T>
class queue
{
public:
queue()
{
size = 5;
data = new T[size];
head = 0;
tail = 0;
}
void enqueue (T data);
T dequeue ();
T peek ();
bool isEmpty();
private:
T* data;
int head, tail, size;
bool needToResize();
void resize();
};
#include "arrayqueue.cpp"
//arrayqueue.cpp
#ifndef QUEUE_H
#include "arrayqueue.h"
#endif
#include <stdexcept>
using namespace std;
template <class T>
bool queue<T>::needToResize()
{
return (tail == size);
}
template <class T>
void queue<T>::resize()
{
T* temp = new T[2*size];
for (int i = 0; i < tail - head; i ++)
temp[i] = data[i+head];
data = temp;
tail = tail - head;
head = 0;
size *= 2;
}
template <class T>
void queue<T>::enqueue(T obj)
{
if (needToResize())
resize();
data[tail++] = obj;
}
template <class T>
T queue<T>::dequeue()
{
if (head == tail)
throw std::out_of_range("Attempt to dequeue from empty queue");
return data[head++];
}
template <class T>
T queue<T>::peek()
{
if (head == tail)
throw std::out_of_range("Attempt to peek from empty queue");
return data[head];
}
template <class T>
bool queue<T>::isEmpty()
{
return head == tail;
}
//loadbalancer.h
#define LOADBALANCER_H
#ifndef REQUEST_CPP
#include "request.cpp"
#endif
#include "arrayqueue.h"
class loadbalancer
{
public:
loadbalancer()
{
systemTime = 0;
}
int getTime();
void incTime();
void addRequest (request r);
request getRequest();
bool isRequestQueueEmpty();
private:
int systemTime;
queue <request> requestQueue;
};
#include "loadbalancer.cpp"
//loadbalancer.cpp
#ifndef LOADBALANCER_H
#include "loadbalancer.h"
#endif
int loadbalancer::getTime()
{
return systemTime;
}
void loadbalancer::incTime()
{
systemTime ++;
}
void loadbalancer::addRequest(request r)
{
requestQueue.enqueue(r);
incTime();
}
request loadbalancer::getRequest()
{
incTime();
if (!requestQueue.isEmpty())
{
request r = requestQueue.dequeue();
return r;
}
}
bool loadbalancer::isRequestQueueEmpty()
{
return requestQueue.isEmpty();
}
//request.cpp
#include <string>
#define REQUEST_CPP
using namespace std;
struct request
{
string source;
string destination;
int processTime;
};
//webserver.cpp
#ifndef REQUEST_CPP
#include "request.cpp"
#endif
class webserver
{
public:
webserver()
{
requestStartTime = 0;
servername = ' ';
}
webserver (char c)
{
requestStartTime = 0;
servername = c;
}
void addRequest(request req, int currTime)
{
r = req;
requestStartTime = currTime;
}
request getRequest()
{
return r;
}
char getName()
{
return servername;
}
bool isRequestDone(int currTime)
{
return (currTime >= (requestStartTime + r.processTime));
}
private:
request r;
int requestStartTime;
char servername;
};
//loadbalancermain.cpp
#include "request.cpp"
#include "webserver.cpp"
#include "loadbalancer.h"
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <sstream>
using namespace std;
const int NUMWEBSERVERS = 8;
//create a request
request createRandomRequest()
{
stringstream ips, ipd;
request r;
ips << (rand() % 256) << "." << (rand() % 256) << "." << (rand() % 256) << "." << (rand() % 256);
ipd << (rand() % 256) << "." << (rand() % 256) << "." << (rand() % 256) << "." << (rand() % 256);
r.source = ips.str();
r.destination = ipd.str();
r.processTime = rand()%500;
return r;
}
int main()
{
//random number generator
srand(time(0));
//create a loadbalancer
loadbalancer lb;
//start off with a "full" queue
for (int i = 0; i < 10; i ++)
{
request r = (createRandomRequest());
lb.addRequest(r);
}
//an array of webservers
webserver webarray[NUMWEBSERVERS];
for (int i = 0; i < NUMWEBSERVERS; i ++)
{
webserver w((char)(i + 65));
webarray[i] = w;
webarray[i].addRequest(lb.getRequest(), lb.getTime());
}
//loop
while (lb.getTime() < 10000)
{
int currTime = lb.getTime();
//check each webserver if it's done
if (webarray[currTime % NUMWEBSERVERS].isRequestDone(currTime))
{
request r = webarray[currTime % NUMWEBSERVERS].getRequest();
cout << "At " << currTime << " " << webarray[currTime % NUMWEBSERVERS].getName() << " processed request from "<< r.source << " to " << r.destination << endl;
//then give it a new request
webarray[currTime % NUMWEBSERVERS].addRequest(lb.getRequest(),currTime);
}
//every random amt of time, we get a new request
if (rand() % 20 == 0)
{
request r = (createRandomRequest());
lb.addRequest(r);
}
lb.incTime();
}
}
The error log is as follows :
- Filename: C:\Users\Manish Anhal\Desktop\Makefile.win
Processing makefile...
--------
- Makefile Processor: C:\Program Files (x86)\Dev-Cpp\MinGW64\bin\mingw32-make.exe
- Command: mingw32-make.exe -f "C:\Users\Manish Anhal\Desktop\Makefile.win" all
g++.exe -c Pdc/arrayqueue.cpp -o Pdc/arrayqueue.o -I"C:/Program Files (x86)/Dev-Cpp/MinGW64/include" -I"C:/Program Files (x86)/Dev-Cpp/MinGW64/x86_64-w64-mingw32/include" -I"C:/Program Files (x86)/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include" -I"C:/Program Files (x86)/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++"
Pdc/arrayqueue.cpp:11:6: error: redefinition of 'bool queue<T>::needToResize()'
bool queue<T>::needToResize()
^
In file included from C:\Users\Manish Anhal\Desktop\Pdc\arrayqueue.h:28:0:
C:\Users\Manish Anhal\Desktop\Pdc\arrayqueue.cpp:11:6: note: 'bool queue<T>::needToResize()' previously declared here
bool queue<T>::needToResize()
^
Pdc/arrayqueue.cpp:17:6: error: redefinition of 'void queue<T>::resize()'
void queue<T>::resize()
^
In file included from C:\Users\Manish Anhal\Desktop\Pdc\arrayqueue.h:28:0:
C:\Users\Manish Anhal\Desktop\Pdc\arrayqueue.cpp:17:6: note: 'void queue<T>::resize()' previously declared here
void queue<T>::resize()
^
Pdc/arrayqueue.cpp:29:6: error: redefinition of 'void queue<T>::enqueue(T)'
void queue<T>::enqueue(T obj)
^
In file included from C:\Users\Manish Anhal\Desktop\Pdc\arrayqueue.h:28:0:
C:\Users\Manish Anhal\Desktop\Pdc\arrayqueue.cpp:29:6: note: 'void queue<T>::enqueue(T)' previously declared here
void queue<T>::enqueue(T obj)
^
Pdc/arrayqueue.cpp:37:3: error: redefinition of 'T queue<T>::dequeue()'
T queue<T>::dequeue()
^
In file included from C:\Users\Manish Anhal\Desktop\Pdc\arrayqueue.h:28:0:
C:\Users\Manish Anhal\Desktop\Pdc\arrayqueue.cpp:37:3: note: 'T queue<T>::dequeue()' previously declared here
T queue<T>::dequeue()
^
Pdc/arrayqueue.cpp:45:3: error: redefinition of 'T queue<T>::peek()'
T queue<T>::peek()
^
In file included from C:\Users\Manish Anhal\Desktop\Pdc\arrayqueue.h:28:0:
C:\Users\Manish Anhal\Desktop\Pdc\arrayqueue.cpp:45:3: note: 'T queue<T>::peek()' previously declared here
T queue<T>::peek()
^
Pdc/arrayqueue.cpp:53:6: error: redefinition of 'bool queue<T>::isEmpty()'
bool queue<T>::isEmpty()
^
In file included from C:\Users\Manish Anhal\Desktop\Pdc\arrayqueue.h:28:0:
C:\Users\Manish Anhal\Desktop\Pdc\arrayqueue.cpp:53:6: note: 'bool queue<T>::isEmpty()' previously declared here
bool queue<T>::isEmpty()
^
C:\Users\Manish Anhal\Desktop\Makefile.win:40: recipe for target 'Pdc/arrayqueue.o' failed
mingw32-make.exe: *** [Pdc/arrayqueue.o] Error 1
I did the exact same copy of code as given in this repository but am unable to run: https://github.com/mistapotta/C-Code/tree/master/C%2B%2B%20Stuff/Unit%206%20-%20arrayqueue/Day%203%264%20-%20Complicated%20Problem
Please help me find the solution.
Aucun commentaire:
Enregistrer un commentaire