Database System Concepts
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
Bartleby Related Questions Icon

Related questions

Question

In C++

Write a class, named "TwoOrLess" with a header file (two_or_less.hpp) and an implementation file (two_or_less.cpp).

This is a class that acts much like a set, except it can hold 0, 1, or 2 duplicates of an int. You need to support the insert, count, and size methods with the same parameters and return types as the set<int> class (see test cases).

 

 

### Code Sample

```cpp
#include "two_or_less.hpp"

TwoOrLess thing;
ASSERT_EQ(thing.size(), 0);
thing.insert(0);
ASSERT_EQ(thing.size(), 1);
```

### Your Code's Output

```plaintext
from /usr/include/gmock/internal/gmock-internal-utils.h:47,
from /usr/include/gmock/gmock-actions.h:51,
from /usr/include/gmock/gmock.h:59,
from cpp_unit_test:0:
cpp_unit_test:3:11: error: ‘thing’ was not declared in this scope
ASSERT_EQ(thing.size(), 0);
^~~~~
cpp_unit_test:3:11: note: suggested alternative: ‘this’
cpp_unit_test:4:1: error: ‘thing’ was not declared in this scope
thing.insert(0);
^~~~~
cpp_unit_test:4:1: note: suggested alternative: ‘this’
CMakeFiles/runTests.dir/build.make:62: recipe for target 'CMakeFiles/runTests.dir/tests.cpp.o' failed
make[2]: *** [CMakeFiles/runTests.dir/tests.cpp.o] Error 1
```

### Explanation

The code attempts to include and use a class or header called `TwoOrLess` from the `two_or_less.hpp` file. This snippet is supposed to perform unit tests using Google Mock, indicated by the `ASSERT_EQ` checks.

**Errors Identified:**

1. **Undeclared Variable:**
   - The error message `‘thing’ was not declared in this scope` indicates a missing declaration or inclusion issue. The compiler suggests the alternative `‘this’`, which is not relevant in this context.

2. **Build Error:**
   - The build is terminated with an error in the CMake process, implying that the compilation of `tests.cpp.o` failed due to the unresolved issues in the above code.

### Recommendations for Resolution:

- **Ensure `TwoOrLess` is Defined:**
  - Verify that the `two_or_less.hpp` file is correctly included and that it defines the `TwoOrLess` class.

- **Check for Typos:**
  - Confirm there are no syntax errors or typographical mistakes around the `TwoOrLess` and `thing` declarations.

- **Linking with Google Mock:**
  - Ensure that Google Mock (GMock) is properly linked and installed in the environment since `ASSERT
expand button
Transcribed Image Text:### Code Sample ```cpp #include "two_or_less.hpp" TwoOrLess thing; ASSERT_EQ(thing.size(), 0); thing.insert(0); ASSERT_EQ(thing.size(), 1); ``` ### Your Code's Output ```plaintext from /usr/include/gmock/internal/gmock-internal-utils.h:47, from /usr/include/gmock/gmock-actions.h:51, from /usr/include/gmock/gmock.h:59, from cpp_unit_test:0: cpp_unit_test:3:11: error: ‘thing’ was not declared in this scope ASSERT_EQ(thing.size(), 0); ^~~~~ cpp_unit_test:3:11: note: suggested alternative: ‘this’ cpp_unit_test:4:1: error: ‘thing’ was not declared in this scope thing.insert(0); ^~~~~ cpp_unit_test:4:1: note: suggested alternative: ‘this’ CMakeFiles/runTests.dir/build.make:62: recipe for target 'CMakeFiles/runTests.dir/tests.cpp.o' failed make[2]: *** [CMakeFiles/runTests.dir/tests.cpp.o] Error 1 ``` ### Explanation The code attempts to include and use a class or header called `TwoOrLess` from the `two_or_less.hpp` file. This snippet is supposed to perform unit tests using Google Mock, indicated by the `ASSERT_EQ` checks. **Errors Identified:** 1. **Undeclared Variable:** - The error message `‘thing’ was not declared in this scope` indicates a missing declaration or inclusion issue. The compiler suggests the alternative `‘this’`, which is not relevant in this context. 2. **Build Error:** - The build is terminated with an error in the CMake process, implying that the compilation of `tests.cpp.o` failed due to the unresolved issues in the above code. ### Recommendations for Resolution: - **Ensure `TwoOrLess` is Defined:** - Verify that the `two_or_less.hpp` file is correctly included and that it defines the `TwoOrLess` class. - **Check for Typos:** - Confirm there are no syntax errors or typographical mistakes around the `TwoOrLess` and `thing` declarations. - **Linking with Google Mock:** - Ensure that Google Mock (GMock) is properly linked and installed in the environment since `ASSERT
### C++ Code Example: "TwoOrLess" Class Test

**Code:**
```cpp
#include "two_or_less.hpp"

TwoOrLess thing;
ASSERT_EQ(thing.size(), 0);
ASSERT_EQ(thing.count(0), 0);

thing.insert(0);
ASSERT_EQ(thing.size(), 1);
ASSERT_EQ(thing.count(0), 1);

thing.insert(1);
ASSERT_EQ(thing.size(), 2);
ASSERT_EQ(thing.count(1), 1);
ASSERT_EQ(thing.count(0), 1);

thing.insert(0);
ASSERT_EQ(thing.size(), 2);
```

**Output:**
```
cpp_unit_test:3:11: error: ‘thing’ was not declared in this scope
    ASSERT_EQ(thing.size(), 0);
          ^~~~
cpp_unit_test:3:11: note: suggested alternative: ‘this’
cpp_unit_test:4:11: error: ‘thing’ was not declared in this scope
    ASSERT_EQ(thing.count(0), 0);
          ^~~~
cpp_unit_test:4:11: note: suggested alternative: ‘this’
cpp_unit_test:6:1: error: ‘thing’ was not declared in this scope
    thing.insert(0);
    ^~~~
cpp_unit_test:6:1: note: suggested alternative: ‘this’
```

### Explanation

This code attempts to utilize a class or struct named `TwoOrLess`, performing several operations to test its functionality:

1. **Initialization and Assertion:**
   - The `TwoOrLess` object, `thing`, is created but not defined in the scope, leading to compilation errors.
   - `ASSERT_EQ` is used to check if the `size()` and `count()` functions return expected values.

2. **Insertion and Verification:**
   - The code attempts to insert elements into `thing` and verify its `size` and `count` after each operation.

3. **Errors Encountered:**
   - The main issue is the undeclared identifier `thing`, suggesting a lack of appropriate definition or inclusion of necessary header files.
   - The error messages suggest replacing `thing` with `this`, which is inappropriate in this context but hints at scope issues.

### Troubleshooting Steps

To resolve these errors, consider the following steps:

- **Include Proper Headers:** Ensure that the `two_or_less.hpp` file is correctly included and
expand button
Transcribed Image Text:### C++ Code Example: "TwoOrLess" Class Test **Code:** ```cpp #include "two_or_less.hpp" TwoOrLess thing; ASSERT_EQ(thing.size(), 0); ASSERT_EQ(thing.count(0), 0); thing.insert(0); ASSERT_EQ(thing.size(), 1); ASSERT_EQ(thing.count(0), 1); thing.insert(1); ASSERT_EQ(thing.size(), 2); ASSERT_EQ(thing.count(1), 1); ASSERT_EQ(thing.count(0), 1); thing.insert(0); ASSERT_EQ(thing.size(), 2); ``` **Output:** ``` cpp_unit_test:3:11: error: ‘thing’ was not declared in this scope ASSERT_EQ(thing.size(), 0); ^~~~ cpp_unit_test:3:11: note: suggested alternative: ‘this’ cpp_unit_test:4:11: error: ‘thing’ was not declared in this scope ASSERT_EQ(thing.count(0), 0); ^~~~ cpp_unit_test:4:11: note: suggested alternative: ‘this’ cpp_unit_test:6:1: error: ‘thing’ was not declared in this scope thing.insert(0); ^~~~ cpp_unit_test:6:1: note: suggested alternative: ‘this’ ``` ### Explanation This code attempts to utilize a class or struct named `TwoOrLess`, performing several operations to test its functionality: 1. **Initialization and Assertion:** - The `TwoOrLess` object, `thing`, is created but not defined in the scope, leading to compilation errors. - `ASSERT_EQ` is used to check if the `size()` and `count()` functions return expected values. 2. **Insertion and Verification:** - The code attempts to insert elements into `thing` and verify its `size` and `count` after each operation. 3. **Errors Encountered:** - The main issue is the undeclared identifier `thing`, suggesting a lack of appropriate definition or inclusion of necessary header files. - The error messages suggest replacing `thing` with `this`, which is inappropriate in this context but hints at scope issues. ### Troubleshooting Steps To resolve these errors, consider the following steps: - **Include Proper Headers:** Ensure that the `two_or_less.hpp` file is correctly included and
Expert Solution
Check Mark
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education