- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 C++ 进行负载平衡。但是出现错误。我已经从 youtube 复制了代码,它在那里工作正常。代码是
//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();
}
}
错误日志如下:
- 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
我做了与此存储库中给出的完全相同的代码拷贝,但无法运行: https://github.com/mistapotta/C-Code/tree/master/C%2B%2B%20Stuff/Unit%206%20-%20arrayqueue/Day%203%264%20-%20Complicated%20Problem
请帮我找到解决方案。
最佳答案
问题是您将 arrayqueue.cpp
构建为单独的翻译单元。
当您这样做时,arrayqueue.cpp
将#include "arrayqueue.h"
#include "arrayqueue.cpp"
。
这意味着源文件 arrayqueue.cpp
source 中的所有函数将被定义两次。
不要构建arrayqueue.cpp
。
关于c++ - 获取错误 : redifinition of bool queue<T> previously declared here,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59069332/
这个问题在这里已经有了答案: The behavior of a C compiler with old-styled functions without prototypes (3 个答案) 关闭
我正在尝试使用 C++ 进行负载平衡。但是出现错误。我已经从 youtube 复制了代码,它在那里工作正常。代码是 //arrayqueue.h #define QUEUE_H #pragma onc
我是一名优秀的程序员,十分优秀!