I am trying to make a first fit memory management code but everytime I try to run it I keep getting incomplete results in the output and these errors
error:expected primary-expression before ‘,’ token
I don't know what to put in the code to resolve this error
#include"stdafx.h"
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
#include<iostream>
#include<memory.h>
#include <cstdlib>
using namespace std;
struct allocList
{
char* startAlloc;
char* endAlloc;
int fk_pid;
allocList *nxt;
};
struct procList
{
int pid;
int jobstatus;
char *startProc;
char *endProc;
procList *nxt;
};
bool initProc(procList*&, int*, int);
bool initAlloc(allocList*&, int*, int);
int doFirstFit(procList*, allocList*);
int cmptFragmentation(procList*, allocList*);
int search(procList*, int);
bool reset(procList*, allocList*);
int main()
{
int arrMemory[] = { 100, 500, 200, 300, 600 };
int arrJobs[] = { 212, 17, 112, 426, 500 };
allocList *ptrAllocStart = NULL;
procList *ptrProcStart = NULL;
initAlloc(ptrAllocStart, arrMemory, (sizeof(arrMemory) / sizeof(int)));
initProc(ptrProcStart, arrJobs, (sizeof(arrJobs) / sizeof(int)));
cout << "Memory Block: " << endl << "Block\tSpace" << endl;
for (int i = 0; i < sizeof(arrMemory) / sizeof(int); i++)
{
cout << i + 1 << "\t" << arrMemory[i] << endl;
}
cout << "\nJobs:" << endl << "Job\tSize" << endl;
for (int i = 0; i < sizeof(arrJobs) / sizeof(int); i++)
{
cout << i + 1 << "\t" << arrJobs[i] << endl;
}
int jobLoaded = 0;
int fragmentation = 0;
jobLoaded = doFirstFit(ptrProcStart, ptrAllocStart);
fragmentation = cmptFragmentation(ptrProcStart, ptrAllocStart);
allocList* memtrav = ptrAllocStart;
getch();
return 0;
}
bool initProc(procList*& ptrProcStart, int* ptrArrProc, int length){
int i;
procList *ptrProc=ptrProcStart;
for(i=0; i<length; i++){
if(ptrProc != NULL){
ptrProc->nxt=new procList;
ptrProc = ptrProc->nxt; ptrProc->startProc=(char*)malloc(*(ptrArrProc+i));
ptrProc->endProc=ptrProc->startProc + *(ptrArrProc+i);
memset(ptrProc->startProc,'a'+i,*(ptrArrProc+i));
ptrProc->jobstatus=0;
ptrProc->pid=i;
ptrProc->nxt=NULL;
}
else
{
ptrProc=new procList;
ptrProc->startProc=(char*)malloc(*(ptrArrProc+i));
ptrProc->endProc=ptrProc->startProc + *(ptrArrProc+i);
memset(ptrProc->startProc, 'a'+i, *(ptrArrProc+i));
ptrProc->jobstatus=0;
ptrProc->pid=i;
ptrProc->nxt=NULL;
ptrProcStart = ptrProc;}}
return true; }
bool initAlloc(allocList*& ptrAllocstart, int* ptrArrAlloc, int length)
{
//cout << "loading function initAlloc"<< "\t" << length << endl;
int i;
allocList* ptrAlloc = ptrAllocstart;
for (i = 0; i < length; i++)
{
//cout << "running loop 1st"<< "\t" << i << endl;
if (ptrAlloc != NULL)
{
ptrAlloc -> nxt = new allocList;
ptrAlloc = ptrAlloc->nxt;
//cout << "after new ptrAlloc" << endl;
ptrAlloc->startAlloc=(char*)malloc(*(ptrArrAlloc+i));
ptrAlloc->endAlloc=ptrAlloc->startAlloc + *(ptrArrAlloc+i);
memset(ptrAlloc->startAlloc,'a'+i,*(ptrArrAlloc+i));
ptrAlloc->nxt=NULL;
}
else
{ //cout << "inside else"<< "\t" << i << endl;
ptrAlloc= new allocList;
ptrAlloc->startAlloc-(char*)malloc(*(ptrArrAlloc+i));
ptrAlloc->endAlloc-ptrAlloc->startAlloc + *(ptrArrAlloc+i);
memset(ptrAlloc->startAlloc, 'a'+i, *(ptrArrAlloc+i));
ptrAlloc->nxt=NULL;
ptrAllocstart=ptrAlloc;
}
}
return true;
}
int doFirstFit(procList*, allocList*){
//cout lang ng UI
cout << "\n\nFirst Fit:\nMemory Block\tSize\tJob\tInternal " << " Fragmentation\n" << endl;
//declaration ng variable
int i = 0;
allocList* memory;
//mag do while sa memory n walang laman?
while (memory != NULL)
i++;
cout << "\t" << i << "\t" << memory->endAlloc - memory->startAlloc << "\t"
<< memory->fk_pid << "\t"
//<< memory->endAlloc - memory->startAlloc - search(procList,memory->fk_pid - 1)
<< endl;
memory = memory->nxt;
return 0;
}
int search(procList* job, int id)
{
int size = 0;
while (job != NULL)
{
if (job->pid == id)
{
size =atoi(job->endProc) - atoi(job->startProc);
break;
}
job = job->nxt;
}
return size;
}
int cmptFragmentation(procList * jobs, allocList * mem)
{
allocList* memtrav, * temp;
procList* jobtrav;
jobtrav = jobs;
memtrav = mem;
int freespace = 0, memsize, jobsize;
int i = 0;
while (memtrav->nxt != NULL)
{
if (memtrav->nxt->fk_pid == 0)
{
freespace += (memtrav->nxt->endAlloc - memtrav->nxt->startAlloc);
temp = memtrav->nxt;
memtrav->nxt = memtrav->nxt->nxt; delete temp;
}
memtrav = memtrav->nxt;
}
if (memtrav->fk_pid == 0)
{
freespace += (memtrav->endAlloc - memtrav->startAlloc);
memtrav = memtrav->nxt;
}
memtrav = mem;
while (memtrav != NULL)
{
jobsize = search(jobs, memtrav->fk_pid - 1);
memsize = memtrav->endAlloc - memtrav->startAlloc;
if (memtrav->fk_pid != 0)
{
memtrav->endAlloc = memtrav->startAlloc + jobsize;
}
freespace += (memsize - jobsize);
memtrav = memtrav->nxt;
}
memtrav = mem;
while (memtrav != NULL)
{
if (memtrav->nxt == NULL)
{
memtrav->nxt = new allocList;
memtrav = memtrav->nxt;
memtrav->startAlloc = (char*)malloc(freespace);
memtrav->endAlloc = memtrav->startAlloc + freespace;
memset(memtrav->startAlloc, 0, freespace);
memtrav->fk_pid = 0;
memtrav->nxt = NULL;
break;
}
memtrav = memtrav->nxt;
}
memtrav = mem;
cout << endl << endl << "Defragmentation\nMemory " << "Block\tSize\tJob\tFreeSpace\n";
while (memtrav != NULL)
{
i++;
cout << "\t" << i << "\t" << memtrav->endAlloc - memtrav->startAlloc << "\t" << memtrav->fk_pid
<< "\t" << memtrav->endAlloc - memtrav->startAlloc - search(jobs, memtrav->fk_pid - 1) << endl;
memtrav = memtrav->nxt;
}
while (jobtrav != NULL)
{
if (jobtrav->jobstatus == 0)
{
doFirstFit(jobs, mem);
cmptFragmentation(jobs, mem);
}
jobtrav = jobtrav->nxt;
}
return 0;
}
bool reset(procList* jobs, allocList* mem)
{
procList* tempj = jobs;
allocList* tempa = mem;
while (jobs->nxt != NULL)
{
jobs = jobs->nxt;
free(tempj);
tempj = jobs;
}
free(tempj);
while (mem->nxt != NULL)
{
mem = mem->nxt;
free(tempa);
tempa = mem;
}
free(tempa);
return true;
}
This is the part where I commented where the error has an issue with and I don't know what I'm doing wrong
<< memory->endAlloc - memory->startAlloc - search(procList,memory->fk_pid - 1)
Please help, thank you so much!
In this function call
search(procList,memory->fk_pid - 1)
there is used the type specifier procList instead of an expression.
Pay attention to that such a function definition where its parameters are not used
int doFirstFit(procList*, allocList*){
//cout lang ng UI
cout << "\n\nFirst Fit:\nMemory Block\tSize\tJob\tInternal " << " Fragmentation\n" << endl;
//declaration ng variable
int i = 0;
allocList* memory;
//..
does not make a sense.
Moreover there is used uninitialized pointer
allocList* memory;
in expressions like for example this
memory = memory->nxt;
that invokes undefined behavior.
The function is called with arguments as
doFirstFit(jobs, mem);
So you need use the passed arguments within the function instead of declaring local variables like this
allocList* memory;
In the analysis I've done the error is this
memory->endAlloc - memory->startAlloc - search(procList,memory->fk_pid - 1)
otherwise direct passing of proclist, you must declare its pointer variable just like you did for Alloclist memory; by same way you'll resolve this
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
#include<iostream>
#include<memory.h>
#include <cstdlib>
using namespace std;
struct allocList
{
char* startAlloc;
char* endAlloc;
int fk_pid;
allocList *nxt;
};
struct procList
{
int pid;
int jobstatus;
char *startProc;
char *endProc;
procList *nxt;
};
bool initProc(procList*&, int*, int);
bool initAlloc(allocList*&, int*, int);
int doFirstFit(procList*, allocList*);
int cmptFragmentation(procList*, allocList*);
int search(procList*, int);
bool reset(procList*, allocList*);
int main()
{
int arrMemory[] = { 100, 500, 200, 300, 600 };
int arrJobs[] = { 212, 17, 112, 426, 500 };
allocList *ptrAllocStart = NULL;
procList *ptrProcStart = NULL;
initAlloc(ptrAllocStart, arrMemory, (sizeof(arrMemory) / sizeof(int)));
initProc(ptrProcStart, arrJobs, (sizeof(arrJobs) / sizeof(int)));
cout << "Memory Block: " << endl << "Block\tSpace" << endl;
for (int i = 0; i < sizeof(arrMemory) / sizeof(int); i++)
{
cout << i + 1 << "\t" << arrMemory[i] << endl;
}
cout << "\nJobs:" << endl << "Job\tSize" << endl;
for (int i = 0; i < sizeof(arrJobs) / sizeof(int); i++)
{
cout << i + 1 << "\t" << arrJobs[i] << endl;
}
int jobLoaded = 0;
int fragmentation = 0;
jobLoaded = doFirstFit(ptrProcStart, ptrAllocStart);
fragmentation = cmptFragmentation(ptrProcStart, ptrAllocStart);
allocList* memtrav = ptrAllocStart;
getch();
return 0;
}
bool initProc(procList*& ptrProcStart, int* ptrArrProc, int length){
int i;
procList *ptrProc=ptrProcStart;
for(i=0; i<length; i++){
if(ptrProc != NULL){
ptrProc->nxt=new procList;
ptrProc = ptrProc->nxt; ptrProc->startProc=(char*)malloc(*(ptrArrProc+i));
ptrProc->endProc=ptrProc->startProc + *(ptrArrProc+i);
memset(ptrProc->startProc,'a'+i,*(ptrArrProc+i));
ptrProc->jobstatus=0;
ptrProc->pid=i;
ptrProc->nxt=NULL;
}
else
{
ptrProc=new procList;
ptrProc->startProc=(char*)malloc(*(ptrArrProc+i));
ptrProc->endProc=ptrProc->startProc + *(ptrArrProc+i);
memset(ptrProc->startProc, 'a'+i, *(ptrArrProc+i));
ptrProc->jobstatus=0;
ptrProc->pid=i;
ptrProc->nxt=NULL;
ptrProcStart = ptrProc;}}
return true; }
bool initAlloc(allocList*& ptrAllocstart, int* ptrArrAlloc, int length)
{
//cout << "loading function initAlloc"<< "\t" << length << endl;
int i;
allocList* ptrAlloc = ptrAllocstart;
for (i = 0; i < length; i++)
{
//cout << "running loop 1st"<< "\t" << i << endl;
if (ptrAlloc != NULL)
{
ptrAlloc -> nxt = new allocList;
ptrAlloc = ptrAlloc->nxt;
//cout << "after new ptrAlloc" << endl;
ptrAlloc->startAlloc=(char*)malloc(*(ptrArrAlloc+i));
ptrAlloc->endAlloc=ptrAlloc->startAlloc + *(ptrArrAlloc+i);
memset(ptrAlloc->startAlloc,'a'+i,*(ptrArrAlloc+i));
ptrAlloc->nxt=NULL;
}
else
{ //cout << "inside else"<< "\t" << i << endl;
ptrAlloc= new allocList;
ptrAlloc->startAlloc-(char*)malloc(*(ptrArrAlloc+i));
ptrAlloc->endAlloc-ptrAlloc->startAlloc + *(ptrArrAlloc+i);
memset(ptrAlloc->startAlloc, 'a'+i, *(ptrArrAlloc+i));
ptrAlloc->nxt=NULL;
ptrAllocstart=ptrAlloc;
}
}
return true;
}
int doFirstFit(procList*, allocList*){
//cout lang ng UI
cout << "\n\nFirst Fit:\nMemory Block\tSize\tJob\tInternal " << " Fragmentation\n" << endl;
//declaration ng variable
int i = 0;
allocList* memory;
procList*ab;
//mag do while sa memory n walang laman?
while (memory != NULL)
i++;
int mem=memory->fk_pid - 1;
cout << "\t" << i << "\t" << memory->endAlloc - memory->startAlloc << "\t"<< memory->fk_pid << "\t"<< memory->endAlloc - memory->startAlloc - search(ab,mem)<< endl;
memory = memory->nxt;
return 0;
}
int search(procList* job, int id)
{
int size = 0;
while (job != NULL)
{
if (job->pid == id)
{
size =atoi(job->endProc) - atoi(job->startProc);
break;
}
job = job->nxt;
}
return size;
}
int cmptFragmentation(procList * jobs, allocList * mem)
{
allocList* memtrav, * temp;
procList* jobtrav;
jobtrav = jobs;
memtrav = mem;
int freespace = 0, memsize, jobsize;
int i = 0;
while (memtrav->nxt != NULL)
{
if (memtrav->nxt->fk_pid == 0)
{
freespace += (memtrav->nxt->endAlloc - memtrav->nxt->startAlloc);
temp = memtrav->nxt;
memtrav->nxt = memtrav->nxt->nxt; delete temp;
}
memtrav = memtrav->nxt;
}
if (memtrav->fk_pid == 0)
{
freespace += (memtrav->endAlloc - memtrav->startAlloc);
memtrav = memtrav->nxt;
}
memtrav = mem;
while (memtrav != NULL)
{
jobsize = search(jobs, memtrav->fk_pid - 1);
memsize = memtrav->endAlloc - memtrav->startAlloc;
if (memtrav->fk_pid != 0)
{
memtrav->endAlloc = memtrav->startAlloc + jobsize;
}
freespace += (memsize - jobsize);
memtrav = memtrav->nxt;
}
memtrav = mem;
while (memtrav != NULL)
{
if (memtrav->nxt == NULL)
{
memtrav->nxt = new allocList;
memtrav = memtrav->nxt;
memtrav->startAlloc = (char*)malloc(freespace);
memtrav->endAlloc = memtrav->startAlloc + freespace;
memset(memtrav->startAlloc, 0, freespace);
memtrav->fk_pid = 0;
memtrav->nxt = NULL;
break;
}
memtrav = memtrav->nxt;
}
memtrav = mem;
cout << endl << endl << "Defragmentation\nMemory " << "Block\tSize\tJob\tFreeSpace\n";
while (memtrav != NULL)
{
i++;
cout << "\t" << i << "\t" << memtrav->endAlloc - memtrav->startAlloc << "\t" << memtrav->fk_pid
<< "\t" << memtrav->endAlloc - memtrav->startAlloc - search(jobs, memtrav->fk_pid - 1) << endl;
memtrav = memtrav->nxt;
}
while (jobtrav != NULL)
{
if (jobtrav->jobstatus == 0)
{
doFirstFit(jobs, mem);
cmptFragmentation(jobs, mem);
}
jobtrav = jobtrav->nxt;
}
return 0;
}
bool reset(procList* jobs, allocList* mem)
{
procList* tempj = jobs;
allocList* tempa = mem;
while (jobs->nxt != NULL)
{
jobs = jobs->nxt;
free(tempj);
tempj = jobs;
}
free(tempj);
while (mem->nxt != NULL)
{
mem = mem->nxt;
free(tempa);
tempa = mem;
}
free(tempa);
return true;
}
The above working just fine.
Related
I am writing a program to implement queue using vectors. I am using the class as template. And in main function am trying to create both string vector and int vector based on template data type. However am getting compilation error from vector assign method.
template <class T>
class queueWithArray {
private:
vector<T> queueArray;
int numberOfElements;
int size;
int head;
int tail;
public:
queueWithArray(int n) {
numberOfElements = 0;
head = -1;
tail = -1;
size = n;
if(is_same<T,string>::value) {
cout << "data type is string" << endl;
queueArray.assign(n,"");
} else {
queueArray.assign(n,0);
}
}
...
int main() {
string InputArray[] = {"to", "be", "or", "not", "to", "-", "be", "-", "-", "that", "-", "-", "-", "is"};
queueWithArray<string> obj(2);
for(auto i=0; i < (signed int)(sizeof(InputArray)/sizeof(InputArray[0])); ++i) {
if(InputArray[i] == "-") {
string item = obj.dequeue();
cout << "dequeue->" << item << endl;
} else {
obj.enqueue(InputArray[i]);
cout << "enqueue->" << InputArray[i] << endl;
}
obj.printQueue();
}
int InputArray_int[] = {10,20,30,40,50,-1,20,-1,-1,60,-1,-1,-1,70};
queueWithArray<int> obj_int(2);
for(auto i=0; i < (signed int)(sizeof(InputArray_int)/sizeof(InputArray_int[0])); ++i) {
if(InputArray_int[i] == -1) {
int item = obj_int.dequeue();
cout << "dequeue->" << item << endl;
} else {
obj_int.enqueue(InputArray_int[i]);
cout << "enquque->" << InputArray_int[i] << endl;
}
obj.printQueue();
}
return 0;
}
..\QueueUsingTemplate.cpp:135:31: required from here
..\QueueUsingTemplate.cpp:45:4: error: no matching function for call to >'std::vector::assign(int&, const char [1])'
queueArray.assign(n,"");
^~~~~~~~~~
In file included from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\vector:64:0,
from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\queue:61,
from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\mingw32\bits\stdc++.h:86,
from ..\QueueUsingTemplate.cpp:18:
c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\stl_vector.h:489:7: note: candidate: void >std::vector<_Tp, _Alloc>::assign(std::vector<_Tp, _Alloc>::size_type, const value_type&) [with >_Tp = int; _Alloc = std::allocator; std::vector<_Tp, _Alloc>::size_type = unsigned int; >std::vector<_Tp, _Alloc>::value_type = int]
assign(size_type __n, const value_type& __val)
^~~~~~
Thank you so much VainMan. queryArray.assign(n, T{}); solves the issue. Hi Louis, please find the complete program with include statements here.
/*
* queueUsingArray.cpp
*
* Created on: 02-Nov-2021
* Author: Admin
*/
#include <iostream>
#include <ctype.h>
#include <bits/stdc++.h>
#include <vector>
#include <typeinfo>
#include <type_traits>
using namespace std;
template <class T>
class queueWithArray {
private:
vector<T> queueArray;
int numberOfElements;
int size;
int head;
int tail;
public:
queueWithArray(int n) {
numberOfElements = 0;
head = -1;
tail = -1;
size = n;
queueArray.assign(n,T{});
}
void enqueue(T item) {
if(numberOfElements == size) {
resize(size * 2);
}
if((head == -1) && (tail == -1)) {
head = tail = 0;
} else {
tail = (tail + 1) % size;
}
queueArray[tail] = item;
numberOfElements++;
}
T dequeue() {
T item;
if(numberOfElements == 0) {
cout << "No elements to dequeue" << endl;
} else {
if(numberOfElements == size/4) {
resize(size/2);
}
item = queueArray[head];
if(head == tail) {
head = tail = -1;
} else {
head = (head + 1) % size;
}
numberOfElements--;
}
return item;
}
bool isEmpty() {
return ((head == -1) && (tail == -1));
}
void resize(int newSize) {
if(newSize > 0) {
size = newSize;
int newIndex = 0;
for(int i=head; i<=tail; ++i){
queueArray[newIndex] = queueArray[i];
newIndex++;
}
queueArray.resize(newSize);
head=0;
tail=newIndex-1;
} else {
return;
}
}
void printQueue() {
if(!isEmpty()) {
for(auto i=head; i<tail; i++) {
cout << queueArray[i] << "->";
}
cout << queueArray[tail] << endl;
} else {
cout << "Queue is Empty" << endl;
}
}
};
int main() {
string InputArray[] = {"to", "be", "or", "not", "to", "-", "be", "-", "-", "that", "-", "-", "-", "is"};
queueWithArray<string> obj(2);
for(auto i=0; i < (signed int)(sizeof(InputArray)/sizeof(InputArray[0])); ++i) {
if(InputArray[i] == "-") {
string item = obj.dequeue();
cout << "dequeue->" << item << endl;
} else {
obj.enqueue(InputArray[i]);
cout << "enqueue->" << InputArray[i] << endl;
}
obj.printQueue();
}
int InputArray_int[] = {10,20,30,40,50,-1,20,-1,-1,60,-1,-1,-1,70};
queueWithArray<int> obj_int(2);
for(auto i=0; i < (signed int)(sizeof(InputArray_int)/sizeof(InputArray_int[0])); ++i) {
if(InputArray_int[i] == -1) {
int item = obj_int.dequeue();
cout << "dequeue->" << item << endl;
} else {
obj_int.enqueue(InputArray_int[i]);
cout << "enquque->" << InputArray_int[i] << endl;
}
obj_int.printQueue();
}
return 0;
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
Could you please tell me where is the memory leak and explain why i did wrong? I can't find the problem and i don't find the answer on google.
#include <iostream>
using namespace std;
class Avion{
private:
static int autoincrementare;
char* nume;
int randuri;
int* locuri ;
const double pret = 100.00;
int nrPersonal;
bool servire;
public:
// setteri si getteri
int getRanduri(){
return this->randuri;
}
int getNrPersonal(){
return this->nrPersonal;
}
bool getServire(){
return this->servire;
}
char* getNume(){
return this->nume;
}
int* getLocuri(){
return this->locuri;
}
void setNume(char* nume){
if (nume != NULL) delete this->nume;
this->nume = new char[strlen(nume) + 1];
strcpy(this->nume, nume);
}
void setLocuri(int* locuri){
if (randuri != NULL) {
this->locuri = new int[this->randuri];
for (int i = 0; i < randuri; i++)
this->locuri[i] = locuri[i];
}
}
void setRanduri(int randuri){
this->randuri = randuri;
}
void setNrPersonal(int nrPersonal){
this->nrPersonal = nrPersonal;
}
void setServire(bool servire){
this->servire = servire;
}
//constructor fara parametrii
Avion() :pret(autoincrementare++){
this->randuri = 3;
this->servire = true;
this->nrPersonal = 10;
this->nume = new char[strlen("Luft") + 1];
strcpy(this->nume, "Luft");
this->locuri = new int[this->randuri];
for (int i = 0; i < this->randuri; i++){
this->locuri[0] = 30;
this->locuri[1] = 40;
this->locuri[2] = 50;
}
}
Avion(int randuri, bool servire, int nrPersonal, char* nume, int* locuri) :pret(autoincrementare++){
this->randuri = randuri;
this->servire = servire;
this->nrPersonal = nrPersonal;
//if (nume != NULL) delete this->nume;
this->nume = new char[strlen(nume) + 1];
strcpy(this->nume, nume);
// if (locuri != NULL)delete this->locuri;
this->locuri = new int(this->randuri);
for (int i = 0; i < randuri; i++)
{
this->locuri[i] = locuri[i];
}
}
friend ostream & operator<<(ostream & out, Avion & a){
out << "Avionul are " << a.randuri << " randuri" << endl;
out << "Avionul are deschis bufetul : " << a.servire << endl;
out << "Avionul are numarul de personal de: " << a.nrPersonal << endl;
out << "Avionul are numele : " << a.nume << endl;
out << " Avionul are: " << a.randuri << " randuri cu " << endl;
for (int i = 0; i < a.getRanduri(); i++){
cout << a.getLocuri()[i] << " locuri " << endl;
}
return out;
}
friend istream & operator >>(istream & in, Avion &a){
char aux[50];
cout << "Nume avion : "; in >> aux;
if (a.nume != NULL) delete[] a.nume;
a.nume = new char[strlen(aux) + 1];
strcpy(a.nume, aux);
return in;
}
Avion& operator=(const Avion& a){
this->randuri = a.randuri;
this->servire = a.servire;
this->nrPersonal = a.nrPersonal;
this->nume = new char[strlen(a.nume) + 1];
strcpy(this->nume, a.nume);
this->locuri = new int(this->randuri);
for (int i = 0; i < randuri; i++)
{
this->locuri[i] = a.locuri[i];
}
return *this;
}
~Avion(){
if (nume != NULL) delete[] this->nume;
if (locuri != NULL) delete[] this->locuri;
}
};
int Avion::autoincrementare = 1;
void main(){
Avion Luft;
cin >> Luft;
cout << Luft << endl;
cout << "================================"<<endl;
cout << "==========================" << endl;
int a[3]{10, 20, 30};
Avion BlueAir(3, true, 10, "Blue Air", a);
cout << BlueAir << endl;
/*
Avion G6;
G6 = Luft;
cout << G6 << endl;
cout << "==================";
cout << Luft << endl;
*/
}
In this code, the first line of the function is doing a delete instead of a delete[]. So that's at least one memory leak.
void setNume(char* nume){
if (nume != NULL) delete this->nume;
this->nume = new char[strlen(nume) + 1];
strcpy(this->nume, nume);
}
You have to match new with delete and new[] with delete[].
So after coding this I got an error : C++ none of the 3 overloads could convert all the argument types line 39 1 in w5.cpp
do you know where is the problem? and could you help me to fix it? I actually dont know why it is showing this because I got the default constructor for this code.
//w5.h
#define MAX_LINE_LENGTH 256
#define MAX_PURCHASES 5
// w5.cpp
#include <iostream>
#include <cstring>
#include "w5.h"
#include "CreditStatement.h"
using namespace std;
void sort(CreditStatement* statement, int n);
int main()
{
double price;
int n = 0;
CreditStatement statement[MAX_PURCHASES];
cout << "Credit Statement Processor\n";
cout << "==========================\n";
do
{
cout << "Item price (0 to quit): ";
cin >> price;
if (cin.fail() || (cin.get() != '\n'))
{
cin.ignore(2000, '\n');
cerr << "Bad character. Try again." << endl;
cin.clear();
}
else if ((int)price != 0)
{
cout << "Statement item: ";
char item[MAX_LINE_LENGTH];
cin.getline(item, MAX_LINE_LENGTH);
if (strlen(item) > 0)
{
statement[n] = CreditStatement(item, price);
n++;
}
}
} while ((int)price != 0 && n < MAX_PURCHASES);
cout << endl;
sort(statement, n);
cout << " Credit Statement\n\n";
cout << " Item Price\n";
cout << "----------------------------------\n";
for (int i = 0; i < n; i++)
{
statement[i].display();
}
cout << endl;
return 0;
}
// sort sorts the elements of Credit Card Statement[n] in ascending order
//
void sort(CreditStatement* s, int n)
{
int i, j;
CreditStatement temp;
for (i = n - 1; i > 0; i--)
{
for (j = 0; j < i; j++)
{
if (s[j].isGreaterThan(s[j + 1]))
{
temp = s[j];
s[j] = s[j + 1];
s[j + 1] = temp;
}
}
}
}
//CreditStatement.h
class CreditStatement{
bool _valid;
double* _price;
char* _item;
public:
CreditStatement();
CreditStatement(char*, double*);
CreditStatement(const CreditStatement&);
CreditStatement& operator=(const CreditStatement&);
//output
void display() const;
//mutators
bool isGreaterThan(const CreditStatement&) const;
};
//CreditStatement.cpp
#include <iostream>
#include <new>
#include "CreditStatement.h"
using namespace std;
void CreditStatement::display() const{
cout << " Something" << _price << _item;
}
bool CreditStatement::isGreaterThan(const CreditStatement&) const{
return _valid;
}
CreditStatement::CreditStatement(){
_item = NULL;
_price = NULL;
}
CreditStatement::CreditStatement(char* iP, double* pP){
_price = NULL;
_item = NULL;
if (pP != NULL){
int sizepP = sizeof(pP) / sizeof(pP[0]);
_price = new (nothrow) double[sizepP];
if (_price){
for (int i = 0; i <sizepP; i++){
_price[i] = pP[i];
};
}
if (iP != NULL){
int sizeiP = sizeof(iP) / sizeof(iP[0]);
_item = new (nothrow) char [sizeiP];
if (_item){
for (int i = 0; i < sizeiP; i++){
_item[i] = iP[i];
};
}
}
}
}
CreditStatement::CreditStatement(const CreditStatement& otherCS){
*this = CreditStatement(otherCS._item, otherCS._price);
}
CreditStatement& CreditStatement::operator=(const CreditStatement& otherCS){
if (this != &otherCS)
{
if (_item){
delete[] _item;
_item = NULL;
}
if (_price){
delete[] _price;
_price = NULL;
}
else{
if (otherCS._price != NULL){
int sizepP = sizeof(otherCS._price) / sizeof(otherCS._price[0]);
_price = new (nothrow) double[sizepP];
if (_price){
for (int i = 0; i < sizepP; i++){
_price[i] = otherCS._price[i];
};
}
if (otherCS._item != NULL){
int sizeiP = sizeof(otherCS._item) / sizeof(otherCS._item[0]);
_item = new (nothrow) char[sizeiP];
if (_item){
for (int i = 0; i < sizeiP; i++){
_item[i] = otherCS._item[i];
};
}
}
}
}
}
return *this;
}
I also got this error
"no instance of constructor "CreditStatement::CreditStatement" matches the argument list
argument types are: (char [256], double) c:*\Project1\w5.cpp 38 20.
I think the problem is your call statement[n] = CreditStatement(item, price);
Here, price is a double, but there's a constructor CreditStatement(char*, double*); but none with signature CreditStatement(char*, double);
You might want to fix that.
i have a c2280 error in c++ and i don't know how to solve it.
here is the code:
#include <iostream>
#include <queue>
#include <deque>
#include "State.h"
#include <assert.h>
#define MAXIMUM_NUMBER_OF_STATES 1000
#define DELTA_Q 0.1
using namespace std;
class RRT
{
private:
float inc_dist;
State start;
State goal;
deque<State> states = deque<State>();
bool goal_is_reached = false;
float RRT::random(float min, float max){
// check if min is less than max , if not then exception is thrown
assert(max >= min);
float range = max - min;
float random;
random = rand() / RAND_MAX;
return (random * (range)) + min;
}
State RRT::randomState(State current_state){
State state = State();
srand(time(0));
while (inStates(state))
{
state.x = random(min(current_state.x, goal.x), max(current_state.x, goal.x));
state.y = random(min(current_state.y, goal.y), max(current_state.y, goal.y));
state.z = random(min(current_state.z, goal.z), max(current_state.z, goal.z));
}
return state;
}
bool RRT::inStates(State state){
for (int i = 0; i < states.size(); i++){
if (states.at(i).x == state.x && states.at(i).y == state.y && states.at(i).z==state.z)
return true;
}
return false;
}
bool RRT::goalTest(State state){
if (state.x == goal.x && state.y == goal.y && state.z == goal.z){
return true;
}
else
return false;
}
void RRT::Successor(State state){
State temp3;
State temp2;
cout <<endl<< "was"<<endl;
if (goalTest(state) || states.size() == MAXIMUM_NUMBER_OF_STATES){
return;
}
getNearistNeighbor(temp3=randomState(state));
cout << "random x: " << temp3.x << " random y: " << temp3.y << " random z: " << temp3.z << endl;
temp2 = State(temp3);
temp2.setFather(state);
states.push_back(temp2);
states.back().setFather(state);
Successor(states.back());
return;
}
State RRT::getNearistNeighbor(State sub_goal_state){
float min_dist = 0, temp;
State desired_state;
for (int i = 0; i < states.size(); i++){
temp = distanceBetweenTwoStates(states.at(i), sub_goal_state);
if (temp < min_dist){
min_dist = temp;
desired_state = State(states.at(i));
}
}
return desired_state;
}
void RRT::generatePath(State state){
cout << endl << "1" << endl;
if (!state.checkIfNull()){
cout << endl << "end" << endl;
return;
cout << endl << "2" << endl;
generatePath(*state.getFather().get());
path.push_back(state);
}
cout << endl << "2" << endl;
generatePath(*state.getFather().get());
path.push_back(state);
return;
}
float RRT::distanceBetweenTwoStates(State state1, State state2){
float x_distance, y_distance, z_distance;
x_distance = sqrt(pow((state1.x - state2.x), 2));
y_distance = sqrt(pow((state1.y - state2.y), 2));
z_distance = sqrt(pow((state1.z - state2.z), 2));
return x_distance + y_distance + z_distance;
}
public:
deque<State> path = deque<State>();
deque<float*> xyzs = deque<float*>();
RRT::RRT(){
}
RRT::RRT(float* starting_point, float* goal_point)
{
start = State(starting_point);
goal= State(goal_point);
states.push_back(start);
}
deque<float*> RRT::getPath(){
Successor(start);
for (int i = 0; i < states.size();i++)
{
if (goalTest(states.at(i))){
goal_is_reached = true;
path.push_back(states.at(i));
break;
}
}
if (goal_is_reached==false){
path.push_back(getNearistNeighbor(goal));
}
State state;
state=State(path.at(0));
path.pop_front();
cout << endl << "x: " << state.x;
cout << endl << "y: " << state.y;
cout << endl << "z: " << state.z << endl;
generatePath(state);
convertToXYZ();
return xyzs;
}
void convertToXYZ(){
State temp;
float* xyz = new float[3];
for (int i = 0; i < path.size(); i++){
temp = path.at(i);
xyz[0] = temp.x;
xyz[1] = temp.y;
xyz[3] = temp.z;
xyzs.push_back(xyz);
}
}
};
here is the code for State.h:
#include <memory>
using namespace std;
class State
{
private:
unique_ptr<State> father;
public:
float x;
float y;
float z;
State(float *xyz){
x = 0;
y = 0;
z = 0;
father = NULL;
}
State(){
x = 0;
y = 0;
z = 0;
father = NULL;
}
State(State& state) : father(new State(*state.father)), x(state.x), y(state.y), z(state.z) {}
State(unique_ptr<State>& state) : father(new State(*state.get())){}
void setFather(State& state){
father.reset(new State(state));
}
unique_ptr<State> getFather(){
unique_ptr<State> temp(new State(*this));
return temp;
}
bool checkIfNull(){
if (father){
return true;
}
else
return false;
}
};
i have been trying to solve this problem but i have not succeed so i need your help guys please help me in solving this.
thanks in advance.
here is the error:
Error 8 error C2280: 'std::unique_ptr<State,std::default_delete<_Ty>> &std::unique_ptr<_Ty,std::default_delete<_Ty>>::operator =(const std::unique_ptr<_Ty,std::default_delete<_Ty>> &)' : attempting to reference a deleted function c:\users\userr\documents\visual studio 2013\projects\devo controller\devo controller\rrt.h 186 1 Devo Controller
again thanks in advance.
unique_ptr<State> getFather(){
unique_ptr<State> temp(new State(*this));
return temp;
}
you can't return unique_ptr . returning a value from function means using the copy constructor. the copy constructor is disabled for unique_ptr. why? because you can't copy a unique_ptr , it's unique! you need to use shared_ptr if many pointers are to point to a specific object. the same goes for the assignment operator ( = ) .
PS. I think it's deprecated to explicitly assign object memory addres to unique_ptr (with new) , the new standard forces you to use std::make_unique in order to assign something to unique_ptr.
also, try googling your error first, you'll be surprised how many answers there are out there
I am getting compile errors in my C++ project. On my friend's computer I get no errors but on mine I get the following:
On line 129 - "Function Definition does not declare parameters "
On line 137 - "'seats' was not declared in this scope"
I am running Code::blocks 10.05 on Windows 7.
Here is my code (with line numbers indicated by comments):
#include <iostream>
#include "Seat.h"
using namespace std;
class Plane
{
public:
int viewSeat(int sNum) {
if (seats[sNum].isFree()) {
return 0;
} else {
//cout << "Debug (Plane::viewSeat(sNum) called)";
string seatMess = "";
switch (seats[sNum].getState()) {
case 1:
seatMess = " is reserved for ";
break;
case 2:
seatMess = " has been checked in by ";
break;
}
cout << "(X) Seat ";
cout << seats[sNum].getCode();
cout << seatMess;
cout << seats[sNum].getFName() << " " << seats[sNum].getLName() << "\n";
return 1;
}
}
void viewAll() {
for (int x = 0; x < numOfSeats; x++) {
if ((seats[x].getState() == 0)) {
cout << "Seat " << seats[x].getCode() << " is free\n";
} else {
viewSeat(x);
}
}
}
void viewFree() {
int freeSeats = 0;
for (int x = 0; x < numOfSeats; x++) {
if ((seats[x].getState() == 0)) {
cout << "Seat " << seats[x].getCode() << " is free\n";
freeSeats++;
}
}
cout << "Found " << freeSeats << " free seats\n";
}
void freeSeat(int sNum) {
seats[sNum].emptySeat();
}
string getCode(int sNum) {
return seats[sNum].getCode();
}
int reserveSeat(int sNum, string fName, string lName, int age, int cType, string business = "") {
if (seats[sNum].isFree()) {
seats[sNum].setFName(fName);
seats[sNum].setLName(lName);
seats[sNum].setAge(age);
seats[sNum].setType(cType);
seats[sNum].setState(1);
seats[sNum].setBusiness(business);
return 1;
} else {
return 0;
}
}
int checkSeat(string lName, string seatCode) {
int found = 0;
for (int x = 0; x < (sizeof(seats) / sizeof(Seat)); x++) {
if ((seats[x].getCode() == seatCode) && (seats[x].getLName() == lName)) {
found = 1;
seats[x].setState(2);
return 1;
}
}
return 0;
}
int calcTake() {
int seatPrice = 5;
int totalTake, totalSeats = 0, totalWestern = 0, totalBusiness = 0, totalStandard = 0;
for (int x = 0; x < numOfSeats; x++) {
if ((seats[x].getState() != 0)) {
int cType = seats[x].getType();
int thisSeat;
if (cType == 1) {
// discount for western`
thisSeat = 0.75 * seatPrice;
totalWestern++;
} else if (cType == 2) {
// discount for business
thisSeat = 0.8 * seatPrice;
totalBusiness++;
} else {
thisSeat = 0.95 * seatPrice;
totalStandard++;
}
totalTake = totalTake + thisSeat;
totalSeats++;
}
}
return totalTake;
}
int isFree(int sNum) {
if (seats[sNum].isFree()) {
return 1;
} else {
return 0;
}
}
private:
// Line 129 ("Function Definition does not declare parameters"):
Seat seats[32] { {"1A"}, {"1B"}, {"1C"}, {"1D"},
{"2A"}, {"2B"}, {"2C"}, {"2D"},
{"3A"}, {"3B"}, {"3C"}, {"3D"},
{"4A"}, {"4B"}, {"4C"}, {"4D"},
{"5A"}, {"5B"}, {"5C"}, {"5D"},
{"6A"}, {"6B"}, {"6C"}, {"6D"},
{"7A"}, {"7B"}, {"7C"}, {"7D"},
{"8A"}, {"8B"}, {"8C"}, {"8D"}
};
// Line 137 ("'seats' was not declared in this scope"):
int numOfSeats = sizeof(seats) / sizeof(Seat);
};
I think you have missed =
Seat seats[32] = { {"1A"}, {"1B"}, {"1C"}, {"1D"},..