简体   繁体   English

在C ++中实现octets类的Set

[英]Implementing Set of octets class in C++

I have an OOP assignment that says: 我有一个OOP作业,内容是:

You are asked to implement a C++ class to model a set of octets (unsigned 8 bit numbers often referred to as bytes). 要求您实现一个C ++类来对一组八位字节(通常称为字节的无符号8位数字)进行建模。 The class is to be used in an embedded application that cannot assume the presence of the STL and must also use the smallest amount of storage possible (for the worst case of a full set) while being as efficient as possible during execution. 该类将在不能假定STL存在的嵌入式应用程序中使用,并且还必须使用可能的最小存储量(对于最坏的完整存储集),同时在执行期间应尽可能地高效。 Your class should have the appropriate constructors and destructor; 您的类应具有适当的构造函数和析构函数; in addition to implementing methods that allow the class user to add and remove numbers to/from the set, and check if a number belongs to the set. 除了实现允许类用户向集合添加数字或从集合中删除数字的方法之外,还检查数字是否属于集合。 Once more, your set class should be self contained and should not use the C++ STL library but should be based on built-in C++ types.You are also asked to implement a main program that uses your class to demonstrate the above interfaces. 再一次,您的set类应该是自包含的,不应使用C ++ STL库,而应基于内置的C ++类型。还要求您实现一个使用该类的主程序来演示上述接口的主程序。

Here is my set.h file: 这是我的set.h文件:

// File: Set.h

#ifndef SET_H
#define SET_H

// Specification of the class

class Set
{
private:
    int memory[8];

public:
    Set(); // Constructor 
    ~Set(); // Destructor 

    void add(int i); // Add number to a set
    bool find(int i); // Checks if a number belongs to a set
    void remove(int i);// Remove number from a set
};


#endif

and my set.cpp file 和我的set.cpp文件

// File: Set.cpp


#include <iostream>
#include "Set.h"

Set::Set(){ // Constructor 
    for (int x = 0; x <8; x++)
        memory[x] = 0;
};

void Set::add(int i) // Add integer i to a set
{
    if (find(i)){

        int index = i / 32;
        int bit = i % 32;
        memory[index] +=  1<< bit ;
    }
}

bool Set::find(int i) // Checks if integer i belongs to a set. Returns true if yes
{

    int index = i / 32;
    int bit = i % 32;
    int temp = (memory[index] >> bit)%2;
    return temp;

}

void Set::remove(int i) // Remove integer i from a set
{

    if (find(i)){

        int index = i / 32;
        int bit = i % 32;
        memory[index] -= 1 << bit;

    }
}

I tried to implement a simple main file to test the class as follows: 我试图实现一个简单的主文件来测试该类,如下所示:

// File: Setmain.cpp
// Test file for the Set class

#include <iostream>
#include "Set.cpp"

using namespace std;

int main(){

    Set test;
    test.add(200);

    return 0;

}

And I got these errors and I don't know how to solve them, any help would be appreciated 而且我遇到了这些错误,而且我不知道如何解决它们,我们将不胜感激

error LNK2005: "public: __thiscall Set::Set(void)" (??0Set@@QAE@XZ) already defined in Set.obj  

error LNK2005: "public: void __thiscall Set::add(int)" (?add@Set@@QAEXH@Z) already defined in Set.obj

error LNK2005: "public: bool __thiscall Set::find(int)" (?find@Set@@QAE_NH@Z) already defined in Set.obj

error LNK2005: "public: void __thiscall Set::remove(int)" (?remove@Set@@QAEXH@Z) already defined in Set.obj

error LNK2019: unresolved external symbol "public: __thiscall Set::~Set(void)" (??1Set@@QAE@XZ) referenced in function _main    

error LNK1120: 1 unresolved externals   

Thanks in advance 提前致谢

改变你的主要

#include "Set.h"

When I duplicated your code, I only got ONE error, similar to the last one in your list above (I am running g++ under Linux/Fedora). 复制代码时,只会出现一个错误,类似于上面列表中的最后一个错误(我在Linux / Fedora下运行g ++)。 Once I define the Destructor function Set::~Set(void) (as an empty function), the compile worked. 一旦我定义了Destructor函数Set ::〜Set(void)(作为一个空函数),编译就起作用了。

Set::~Set(void) { // Destructor 
};

Aside from that, in terms of best programming practices, I would recommend some range-checking, and in terms of code efficiency, I would recommend using a shift operator (i >> 5) instead of division (i / 32), and using a mask operation (i & 0x1F) instead of modulo (i % 32). 除此之外,就最佳编程实践而言,我建议进行一些范围检查,就代码效率而言,我建议使用移位运算符(i >> 5)代替除法(i / 32),并使用掩码操作(i&0x1F)而不是模(i%32)。 One would hope that a good optimizing compiler would produce the same code either way, but why chance it? 有人希望一个好的优化编译器会以任何一种方式产生相同的代码,但是为什么会这样呢?

  1. In setmain.cpp, change 在setmain.cpp中,更改

     #include "Set.cpp" 

    to

     #include "Set.h" 
  2. In set.cpp, implement a destructor 在set.cpp中,实现一个析构函数

     Set::~Set() { } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM