Need to create an Entry.cpp file

I need to create a entry.cpp file that will follow the functions listed in the entry.h file listed below. Could anyone help me create one?

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
  /** A class of entries to add to an array based implementation of the ADT dictionary*/

#pragma once

#ifndef ENTRY_
#define ENTRY_

template <class KeyType, class ValueType>

class Entry
{

private:
	KeyType key;
	ValueType value;

protected:
	void setKey(const KeyType& searchKey);

public:
	Entry();
	Entry(const KeyType& searchKey, const ValueType& newValue);
	ValueType getValue() const;
	KeyType getKey() const;
	void setValue(const ValueType& newValue);

	bool operator ==(const Entry<KeyType, ValueType>& rightHandValue) const;
	bool operator >(const Entry<KeyType, ValueType>& leftHandValue) const;

}; // end Entry

#include Entry.cpp

#endif  
Last edited on
What part(s) are you having difficulty with? What part(s) have you done/tried?
Why do you need a .cpp file?
Code with templates is normally all in the header file.
I'm confused on how I do the two bool operator and the getValue/getKey.
So post what you have now. thmm is quite right. The function bodies for a templated class need to be in the same file as the class definition - but the .h file has an #include Entry.cpp. This would work, but isn't usually how it's done.


Last edited on
I'm just following the code that is listed in my textbook. I need to use to this h file to create a entry.cpp file.
Last edited on
Hello Lacy9265,

I realize that your book may say "Entry.cpp", but as a ".cpp" file it should be compiled as any ".cpp" would be as part of the whole solution/project.

Try using the extension of ".inl" or ".ipp". As a header file extension this should not compile as part of the project until you include it somewhere.

This would give you the ability to split the templated class into 2 files, yet make them as 1 at compile time.

I believe this should, but have not had a chance to test it yet.

Andy
Hello Andy,

I have to follow the instructions that the textbook gives. It seems like I have to have a Entry.cpp file. Thanks for your suggestion. If I don't use an Entry.cpp file I will lose points.
Last edited on
As three files (including main.cpp for testing):

entry.h
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
#pragma once

template <class KeyType, class ValueType>
class Entry {
private:
	KeyType key {};
	ValueType value {};

protected:
	void setKey(const KeyType& searchKey);

public:
	Entry();
	Entry(const KeyType& searchKey, const ValueType& newValue);
	ValueType getValue() const;
	KeyType getKey() const;
	void setValue(const ValueType& newValue);

	bool operator ==(const Entry<KeyType, ValueType>& rightHandValue) const;
	bool operator >(const Entry<KeyType, ValueType>& leftHandValue) const;
};

template <class KeyType, class ValueType>
class Entry1 : public Entry<KeyType, ValueType> {
public:
	Entry1() : Entry() {}
	Entry1(const KeyType& searchKey, const ValueType& newValue) : Entry<KeyType, ValueType>(searchKey, newValue) {}
	void setKey(const KeyType& searchKey) { Entry<KeyType, ValueType> ::setKey(searchKey); }
};

#include "entry.cpp" 


entry.cpp
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

template <class KeyType, class ValueType>
inline Entry<KeyType, ValueType>::Entry() {}

template <class KeyType, class ValueType>
inline Entry<KeyType, ValueType>::Entry(const KeyType& searchKey, const ValueType& newValue) : key(searchKey), value(newValue) {}

template <class KeyType, class ValueType>
inline ValueType Entry<KeyType, ValueType>::getValue() const { return value; }

template <class KeyType, class ValueType>
inline KeyType Entry<KeyType, ValueType>::getKey() const { return key; }

template <class KeyType, class ValueType>
inline void Entry<KeyType, ValueType>::setValue(const ValueType& newValue) {
	value = newValue;
}

template <class KeyType, class ValueType>
inline void Entry<KeyType, ValueType>::setKey(const KeyType& newKey) {
	key = newKey;
}

template <class KeyType, class ValueType>
inline bool Entry<KeyType, ValueType>::operator==(const Entry<KeyType, ValueType>& rightHandValue) const {
	return rightHandValue.key == key;	// Might want to also compare values as well
}

template <class KeyType, class ValueType>
inline bool Entry<KeyType, ValueType>::operator>(const Entry<KeyType, ValueType>& rightHandValue) const {
	return rightHandValue.key > key;	// Might want to also compare values as well
}


main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <iomanip>
#include "entry.h"

int main()
{
	Entry e(1, 2);
	Entry1 e1(5, 6);

	std::cout << e.getKey() << "  " << e.getValue() << '\n';
	std::cout << e1.getKey() << "  " << e1.getValue() << '\n';

	e1.setValue(4);
	e1.setKey(3);

	std::cout << e1.getKey() << "  " << e1.getValue() << '\n';

	Entry e2(1, 5);

	std::cout << std::boolalpha << (e2 == e) << '\n';
	std::cout << std::boolalpha << (e2 > e) << '\n';
}

I have figured out the bool operators.
I'm having trouble with these functions:
protected:
void setKey(const KeyType& searchKey);

public:
Entry();
Entry(const KeyType& searchKey, const ValueType& newValue);
ValueType getValue() const;
KeyType getKey() const;
void setValue(const ValueType& newValue);
Last edited on
I right now I'm getting issues where I get errors saying function already defined. I'm using the same entry.h file I have posted before.

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
 
template <class KeyType, class ValueType>
inline Entry<KeyType, ValueType>::Entry() {}

template <class KeyType, class ValueType>
inline Entry<KeyType, ValueType>::Entry(const KeyType& searchKey, const ValueType& newValue) : key(searchKey), value(newValue) {}

template <class KeyType, class ValueType>
inline ValueType Entry<KeyType, ValueType>::getValue() const { return value; }

template <class KeyType, class ValueType>
inline KeyType Entry<KeyType, ValueType>::getKey() const { return key; }

template <class KeyType, class ValueType>
inline void Entry<KeyType, ValueType>::setValue(const ValueType& newValue) {
	value = newValue;
}

template <class KeyType, class ValueType>
inline void Entry<KeyType, ValueType>::setKey(const KeyType& newKey) {
	key = newKey;
}


template < class KeyType, class ValueType>
bool Entry<KeyType, ValueType>::operator==(const Entry<KeyType, ValueType>&rightHandItem) const
{
return (key == rightHandItem.getKey());
}  


template < class KeyType, class ValueType>
bool Entry<KeyType, ValueType>::operator>(const Entry<KeyType, ValueType>& leftHandItem) const
{
	return (key > leftHandItem.getKey());
}
Last edited on
As Handy Andy already pointed out using including a .cpp is going to cause problems.
You're getting duplicate errors because entry.cpp is being compiled twice.
Once as a result of the include.
And again when your IDE sees the .cpp file and compiles entry.cpp a second time.
Since you can't change the name, your best best is to go into your IDE's property page for entry.cpp and mark it as non-compileable. This way your IDE will not try to compile it because it is a .cpp file. Your include will not be affected.

In Visual Studio, right click on entry.cpp; Select properties. The property page is displayed. Under General, Exclude from Build; select YES and save the property page. Other IDE's may vary.
Last edited on
Or see my code above and mark the function definitions as inline.

Or to not compile entry.cpp, right click it and select Remove and then Remove (NOT DELETE).

If I would follow what AbstractionAnon said. When I zip the project and open the project on another machine the build for entry.cpp would be the same as it was on my computer.
When I zip the project and open the project on another machine the build for entry.cpp would be the same as it was on my computer.

That's correct.
Topic archived. No new replies allowed.