Question: Fix Nyble.h and Nyble.cpp Nyble.h: #ifndef NYBLE _ H #define NYBLE _ H class Nyble { public: Nyble ( ) ; Nyble ( const Nyble&

Fix Nyble.h and Nyble.cpp
Nyble.h:
#ifndef NYBLE_H
#define NYBLE_H
class Nyble
{
public:
Nyble();
Nyble(const Nyble&);
Nyble& operator=(const Nyble&);
~Nyble();
Nyble(unsigned int value);
Nyble operator+(const Nyble& other) const;
friend Nyble operator+(const Nyble& nyble, unsigned int num);
friend Nyble operator+(unsigned int num, const Nyble& nyble);
Nyble& operator+=(const Nyble& other);
Nyble operator++();
Nyble operator++(int);
Nyble operator<<(int shift) const;
Nyble operator~() const;
Nyble operator+() const;
operator unsigned int() const;
unsigned char getData() const;
private:
// Do not change this data
unsigned char data;
};
#endif
//--- End of File ---
Nyble.cpp:
#include "Nyble.h"
Nyble::Nyble()
{
data =0;
}
Nyble::Nyble(const Nyble& other)
{
data = other.data;
}
Nyble& Nyble::operator=(const Nyble& other)
{
if (this != &other){
data = other.data;
}
return *this;
}
Nyble::~Nyble(){}
Nyble::Nyble(unsigned int value)
{
data = static_cast(value & 0x0F);
}
Nyble Nyble::operator+(const Nyble& other) const {
Nyble result;
result.data = static_cast((data + other.data) & 0x0F);
return result;
}
Nyble operator+(const Nyble& nyble, unsigned int num){
Nyble result;
result.data =(nyble.data +(num & 0x0F)) & 0x0F;
return result;
}
Nyble operator+(unsigned int num, const Nyble& nyble){
return nyble + num;
}
Nyble& Nyble::operator+=(const Nyble& other){
data = static_cast((data + other.data) & 0x0F);
return *this;
}
Nyble Nyble::operator++(){
data = static_cast((data +1) & 0x0F);
return *this;
}
Nyble Nyble::operator++(int){
Nyble temp =*this;
++(*this);
return temp;
}
Nyble Nyble::operator<<(int shift) const {
Nyble result;
shift %=4;
result.data = static_cast(((data << shift)|(data >>(4- shift))) & 0x0F);
return result;
}
Nyble Nyble::operator~() const {
Nyble result;
result.data = static_cast(~data & 0x0F);
return result;
}
Nyble Nyble::operator+() const {
Nyble result;
result.data = static_cast((data +3) & 0x0F);
return result;
}
Nyble::operator unsigned int() const {
return static_cast(data -3);
}
unsigned char Nyble::getData() const
{
return this->data;
}
//--- End of File ---
An error will occur if the test code below is executed simultaneously using the above two codes.
Nyble_AddOp_test.cpp:
#include "_UnitTestConfiguration.h"
#include "Nyble.h"
TEST(Nyble_AddOp, TestConfig::ALL)
{
#if Nyble_AddOp
Nyble A(3);
Nyble B(4);
Nyble C;
CHECK( A.getData()==3);
CHECK( B.getData()==4);
CHECK( C.getData()==0);
C = A + B;
CHECK( A.getData()==3);
CHECK( B.getData()==4);
CHECK( C.getData()==7);
Nyble D(5);
Nyble E(13);
D += E;
CHECK( D.getData()==2);
CHECK( E.getData()==13);
Nyble F(8);
Nyble G;
G = F +3;
CHECK( F.getData()==8);
CHECK( G.getData()==11);
Nyble H(14);
Nyble I;
I =7+ H;
CHECK( H.getData()==14);
CHECK( I.getData()==5);
#endif
} TEST_END
//--- End of File ---
Nyble_SpecialOps3_test.cpp:
#include "_UnitTestConfiguration.h"
#include "Nyble.h"
TEST(Nyble_SpecialOps3, TestConfig::ALL)
{
#if Nyble_SpecialOps3
Nyble E(13);
unsigned int MonkeyVal(0);
MonkeyVal = E;
CHECK(MonkeyVal ==10);
CHECK(E.getData()==13);
#endif
} TEST_END
//--- End of File ---
Output:
10_Nyble_AddOp_test.cpp(41,6): error C2666: 'operator +': overloaded functions have similar conversions
10_Nyble_AddOp_test.cpp(49,6): error C2666: '+': overloaded functions have similar conversions
The problem has not been resolved at all.
The code given as the correct answer is the same as the existing code.
Is there a way to fix Nyble.h and Nyble.cpp to get both tests through at the same time without causing that error?

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!