Struggling to make this code work

I have these 2 classes that are supposed to work together, but it won't even compile.

missive.cpp file:
#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 [] array; NOT USED
}

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;
}




request.cpp file:
#include "request.h"
#include <iostream>
#include <string>
#include <cstring>


request::request()
{
requesterName="";
requestID="";
requestType="";
urgency=0;
}

request::request(string name, string ID, string type, int urgent)
{
requesterName=name;
requestID=ID;
requestType=type;
urgency=urgent;
}

request::~request()
{
cout<<"request deleted"<<endl;
}

string request::getRequesterName()
{
return requesterName;
}

string request::getID()
{
return requestID;
}

string request::getRequestType()
{
return requestType;
}

int request::getUrgency()
{
return urgency;
}

void request::print()
{
cout<<"Name : "<< getRequesterName() <<endl;
cout<<"ID : "<< getID() <<endl;
cout<<"Type : "<< getRequestType() <<endl;
cout<<"Urgency : "<< getUrgency() <<endl;
}

what's wrong with the code?
1) Please use code tags when posting code, to make it readable:

http://www.cplusplus.com/articles/z13hAqkS/

2)
it won't even compile

Which means your compiler will have given you specific errors, relating to the specific lines of code that are causing those errors.

For my own curiosity, I have to ask: why did you decide to withhold those details from us?
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).
Last edited on
missive.cpp file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#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;
	}
	


request.cpp file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include "request.h"
#include <iostream>
#include <string>
#include <cstring>


	request::request()
	{
		requesterName="";
		requestID="";
		requestType="";
		urgency=NULL;
	}
	
	request::request(string name, string ID, string type, int urgent)
	{
		requesterName=name;
		requestID=ID;
		requestType=type;
		urgency=urgent;
	}
	
	request::~request()
	{
		cout<<"request deleted"<<endl;
	}
	
	string request::getRequesterName()
	{
		return requesterName;
	}
	
	string request::getID()
	{
		return requestID;
	}
	
	string request::getRequestType()
	{
		return requestType;
	}
	
	int request::getUrgency()
	{
		return urgency;
	}
	
	void request::print()
	{
		cout<<"Name : "<<	getRequesterName()	<<endl;
		cout<<"ID : "<<	getID()				<<endl;
		cout<<"Type : "<<	getRequestType()		<<endl;
		cout<<"Urgency : "<<	getUrgency()		<<endl;
	}


missive.h file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#ifndef MISSIVE_H
#define MISSIVE_H

#include <iostream>
#include <string>
#include <cstring>
#include "request.h"
using namespace std;
class missive
{
	
	private:
		request** list;
		string commanderName;
		string commanderID;
		int missiveLimit;
		int currentSize;
	
	public:
		missive(); //default constructor
		missive(string, string, int);	
		missive(string, string, request**, int, int);
		~missive();
		int addRequest(request*);
		int removeRequest(string);
		int getMostUrgentRequestPriority();
		request** getList();
		string getName();
		string getID();
		int getCurrSize();
		int getMissiveLimit();
	



	
};

	


#endif 



request.h file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#ifndef REQUEST_H
#define REQUEST_H

#include <iostream>
#include <string>
#include <cstring>
using namespace std;
class request
{
	
	private:
		string requesterName;
		string requestID;
		string requestType;
		int urgency;
	
	public:
		request();
		request(string, string, string, int);
		~request();
		string getRequesterName();
		string getID();
		string getRequestType();
		int getUrgency();
		void print();
	
	


	
};

	


#endif 


main file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include "request.h"
#include "missive.h"
#include "missive.cpp"
#include "request.cpp"
#include <iostream>
#include <cstring>
#include <string>

using namespace std;

// main function

int main() 
{ 	
	string name="hein";
	string ID="215";
	string type="supplies";
	int urgency=17;
	
	request Order =request("name", ID, "type",urgency);
	Order.print();
	
	
	return 0; 
}


couldn't compile to test more with the main file
Last edited on
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

main.o: main.cpp missive.h request.h
g++ -c main.cpp

missive.o: missive.h missive.cpp
g++ -c missive.cpp

request.o: request.h request.cpp
g++ -c request.cpp
run:
./main

clean:
rm *.o main



errors I get are
here are the errors:

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
Where's main.cpp?

And you've lost the indentation of your code files. You should've re-copied/pasted them from the original code. You can edit the post.
Last edited on
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).
Last edited on
is that is?

does the code seem proper to you in other regards?
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;
}

Last edited on
Topic archived. No new replies allowed.