int missive::removeRequest(string name)
{
//variable to determine if the request was found in the list or not; initially set to false.
bool notFound = true;
//loop throught the list to find the ID
for(int i=0; i < missiveLimit;i++)
{
//if the ID is the same as name, dealocate memory
if(getList()[i]->getID() == name)
{
getList()[i]= 0;
delete getList()[i];
notFound=false;
}
}
if(notFound)
{
return -1;
}
return 0;
}
int missive::getMostUrgentRequestPriority()
{
int max, location =-1;
//loop through list to determine max value for urgency
for(int i=0; i < missiveLimit; i++)
{
if(getList()[i]->getUrgency() > max)
{
max = getList()[i]->getUrgency();
location=i;
}
}
if(location == -1)
{
return -1;
}
Repaste your code in code tags: [code]
your code goes here [/code]
Also post the header files and the driver (main) file so we can try running it.
Also tell us the error messages and how you compile it (or if you are using an IDE).
#include "missive.h"
#include "request.h"
missive::missive()
{
cout<<"missive class constructed"<<endl;
}
missive::missive(string cName, string cID, int sizeMissive)
{
commanderName = cName;
commanderID = cID;
missiveLimit = sizeMissive;
currentSize =0;
}
missive::missive(string cName, string cID, request** requests, int sizeMissive, int currSize)
{
commanderName= cName;
commanderID= cID;
missiveLimit= sizeMissive;
currentSize=currSize;
//makes an array of pointers to request objects, to store the pre-generated list
request* * array = new request*[sizeMissive];
//initialize the array
for(int i = 0; i< sizeMissive; i++)
{
array[i]=0;//initialise to NULL pointer
array[i]= new request;
array[i]=requests[i];
}
}
missive::~missive()
{
for(int i = 0; i <missiveLimit;i++)
{
if(getList()[i] !=0)
{
delete getList()[i];
}
}
delete [] getList();
}
int missive::addRequest(request* i)
{
if (currentSize = missiveLimit)
{
return -1;
}
//loop through missive list to find an open slot
int k=0;
bool flag =true;
while(flag)
{
if(getList()[k]->getUrgency() == 0)
{
flag = false;
getList()[k] =i;
}
k++;
}
return k;
}
int missive::removeRequest(string name)
{
//variable to determine if the request was found in the list or not; initially set to false.
bool notFound = true;
//loop throught the list to find the ID
for(int i=0; i < missiveLimit;i++)
{
//if the ID is the same as name, dealocate memory
if(getList()[i]->getID() == name)
{
getList()[i]= 0;
delete getList()[i];
notFound=false;
}
}
if(notFound)
{
return -1;
}
return 0;
}
int missive::getMostUrgentRequestPriority()
{
int max, location =-1;
//loop through list to determine max value for urgency
for(int i=0; i < missiveLimit; i++)
{
if(getList()[i]->getUrgency() > max)
{
max = getList()[i]->getUrgency();
location=i;
}
}
if(location == -1)
{
return -1;
}
return max;
}
request** missive::getList()
{
return list;
}
string missive::getName()
{
return commanderName;
}
string missive::getID()
{
return commanderID;
}
int missive::getCurrSize()
{
return currentSize;
}
int missive::getMissiveLimit()
{
return missiveLimit;
}
thanks for the help. new to c++ and this site, so not 100% sure how to post things. i don't use an IDE, I compile it using a makefile. here is the makefile:
main: main.o missive.o request.o
g++ -o main main.o missive.o request.o
make
g++ -o main main.o missive.o request.o
missive.o: In function `missive::missive()':
missive.cpp:(.text+0x0): multiple definition of `missive::missive()'
main.o:main.cpp:(.text+0x0): first defined here
missive.o: In function `missive::missive()':
missive.cpp:(.text+0x0): multiple definition of `missive::missive()'
main.o:main.cpp:(.text+0x0): first defined here
missive.o: In function `missive::missive(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, int)':
missive.cpp:(.text+0x8a): multiple definition of `missive::missive(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, int)'
main.o:main.cpp:(.text+0x8a): first defined here
missive.o: In function `missive::missive(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, int)':
missive.cpp:(.text+0x8a): multiple definition of `missive::missive(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, int)'
main.o:main.cpp:(.text+0x8a): first defined here
missive.o: In function `missive::missive(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, request**, int, int)':
missive.cpp:(.text+0x12a): multiple definition of `missive::missive(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, request**, int, int)'
main.o:main.cpp:(.text+0x12a): first defined here
missive.o: In function `missive::missive(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, request**, int, int)':
missive.cpp:(.text+0x12a): multiple definition of `missive::missive(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, request**, int, int)'
main.o:main.cpp:(.text+0x12a): first defined here
missive.o: In function `missive::~missive()':
missive.cpp:(.text+0x27e): multiple definition of `missive::~missive()'
main.o:main.cpp:(.text+0x27e): first defined here
missive.o: In function `missive::getList()':
missive.cpp:(.text+0x556): multiple definition of `missive::getList()'
main.o:main.cpp:(.text+0x538): first defined here
missive.o: In function `missive::~missive()':
missive.cpp:(.text+0x27e): multiple definition of `missive::~missive()'
main.o:main.cpp:(.text+0x27e): first defined here
missive.o: In function `missive::addRequest(request*)':
missive.cpp:(.text+0x340): multiple definition of `missive::addRequest(request*)'
main.o:main.cpp:(.text+0x322): first defined here
missive.o: In function `missive::removeRequest(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)':
missive.cpp:(.text+0x3d4): multiple definition of `missive::removeRequest(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
main.o:main.cpp:(.text+0x3b6): first defined here
missive.o: In function `missive::getMostUrgentRequestPriority()':
missive.cpp:(.text+0x4c0): multiple definition of `missive::getMostUrgentRequestPriority()'
main.o:main.cpp:(.text+0x4a2): first defined here
missive.o: In function `missive::getName[abi:cxx11]()':
missive.cpp:(.text+0x560): multiple definition of `missive::getName[abi:cxx11]()'
main.o:main.cpp:(.text+0x542): first defined here
missive.o: In function `missive::getID[abi:cxx11]()':
missive.cpp:(.text+0x582): multiple definition of `missive::getID[abi:cxx11]()'
main.o:main.cpp:(.text+0x564): first defined here
missive.o: In function `missive::getCurrSize()':
missive.cpp:(.text+0x5a4): multiple definition of `missive::getCurrSize()'
main.o:main.cpp:(.text+0x586): first defined here
missive.o: In function `missive::getMissiveLimit()':
missive.cpp:(.text+0x5b0): multiple definition of `missive::getMissiveLimit()'
main.o:main.cpp:(.text+0x592): first defined here
request.o: In function `request::request()':
request.cpp:(.text+0x0): multiple definition of `request::request()'
main.o:main.cpp:(.text+0x59e): first defined here
request.o: In function `request::request()':
request.cpp:(.text+0x0): multiple definition of `request::request()'
main.o:main.cpp:(.text+0x59e): first defined here
request.o: In function `request::request(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, int)':
request.cpp:(.text+0xce): multiple definition of `request::request(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, int)'
main.o:main.cpp:(.text+0x66c): first defined here
request.o: In function `request::request(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, int)':
request.cpp:(.text+0xce): multiple definition of `request::request(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, int)'
main.o:main.cpp:(.text+0x66c): first defined here
request.o: In function `request::~request()':
request.cpp:(.text+0x194): multiple definition of `request::~request()'
main.o:main.cpp:(.text+0x732): first defined here
request.o: In function `request::~request()':
request.cpp:(.text+0x194): multiple definition of `request::~request()'
main.o:main.cpp:(.text+0x732): first defined here
request.o: In function `request::getRequesterName[abi:cxx11]()':
request.cpp:(.text+0x1f6): multiple definition of `request::getRequesterName[abi:cxx11]()'
main.o:main.cpp:(.text+0x794): first defined here
request.o: In function `request::getID[abi:cxx11]()':
request.cpp:(.text+0x216): multiple definition of `request::getID[abi:cxx11]()'
main.o:main.cpp:(.text+0x7b4): first defined here
request.o: In function `request::getRequestType[abi:cxx11]()':
request.cpp:(.text+0x238): multiple definition of `request::getRequestType[abi:cxx11]()'
main.o:main.cpp:(.text+0x7d6): first defined here
request.o: In function `request::getUrgency()':
request.cpp:(.text+0x25a): multiple definition of `request::getUrgency()'
main.o:main.cpp:(.text+0x7f8): first defined here
request.o: In function `request::print()':
request.cpp:(.text+0x266): multiple definition of `request::print()'
main.o:main.cpp:(.text+0x804): first defined here
collect2: error: ld returned 1 exit status
make: *** [makefile:2: main] Error 1
That's what I suspected. You just need to remove these two lines from main.cpp:
1 2
#include "missive.cpp"
#include "request.cpp"
#include is really just for .h files, not .cpp files.
EDIT:
In request::request, you should assign 0 to urgency (not NULL).
And strings don't need to be initialized to ""; they are automatically initialized to an empty string.
In main.cpp, you would usually call the constructor like this:
request Order(name, ID, type, urgency);
(Note that name and type shouldn't be in quotes!)
Also, it's very bad form to have "using namespace std" in header files. It's best to just put std:: where it's needed (in front of string). You should also include only what is needed in the header itself.
request.h only needs <string>.
missive.h only needs <string> and request.h.
And in the cpp files you should include what's needed for each file; don't rely on the header having included it.
In missive.cpp include <iostream> and <string>
In request.cpp include <iostream> and <string>
Also put "using namespace std;" in the cpp files (if you want; it's actually best to always just say std:: where needed).
I've added some stuff to my previous post. I see a couple of other errors which I'll add to this post in a minute or two.
In missive::addRequest:
if (currentSize = missiveLimit) //// this should be ==
And once you set flag to false, you still increment k once more before the loop ends and you return it. That's probably not what you want. It's best to just get rid of the flag and use break (or just return k at that point).
Also, it seems you should be looking for a null pointer in the list instead of urgency being 0. So:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
int missive::addRequest(request* i)
{
if (currentSize == missiveLimit)
return -1;
int k = 0;
while (k < currentSize)
{
if (getList()[k] == 0)
{
getList()[k] = i;
break;
}
k++;
}
return k; // k will be currentSize if no null was found
}
In removeRequest you are setting the spot to 0 before deleting it. You need to do that in the opposite order. And again you don't need the flag.
1 2 3 4 5 6 7 8 9 10 11 12 13
int missive::removeRequest(string name)
{
for (int i = 0; i < missiveLimit; i++)
{
if (getList()[i]->getID() == name)
{
delete getList()[i];
getList()[i] = 0;
return 0;
}
}
return -1;
}