简体   繁体   English

C++ 指针 function class 在 class 中的实现

[英]C++ pointer function implementation for a class within class

My header file "DoublyLinkedLists" looks like this我的 header 文件“DoublyLinkedLists”看起来像这样

class DoublyLinkedLists{
private:
    struct element{
        int key;
        struct element *prev, *next;
    };
    element* head = NULL;
    element* tail = NULL;
public:
    void insertToHead(int insert);
    element *find(int insert);
 
};

In the cpp file that includes "DoublyLinkedLists" header, I implemented my function(insert to head) like this:在包含“DoublyLinkedLists”header 的 cpp 文件中,我实现了我的函数(插入到头部),如下所示:

void DoublyLinkedLists::insertToHead(int insert){
  //some code
}

I've tried, but doesnt work我试过了,但没用

DoublyLinkedLists::element* find(int insert){
//code
}

But how can I implement a function "element *find(int insert)" in a cpp file that returns a pointer?但是我如何在返回指针的cpp文件中实现function“元素*查找(int插入)”? Where must I write "DoublyLinkedLists::" that specifies from where I am taking a function, or must I write something else.我必须在哪里写“DoublyLinkedLists::”来指定我从哪里获取 function,或者我必须写其他东西。

I have absolutely no idea what I'm doing.我完全不知道我在做什么。 I'm new to object oriented C++ programming and any help would be really appreciated.我是 object 面向 C++ 编程的新手,非常感谢任何帮助。

Here is a sample implementation what you are supposed to do.这是您应该执行的示例实现。 I don't know if tail->next == head so I check tail seperately.我不知道是否 tail->next == head 所以我单独检查 tail 。

DoublyLinkedLists::element* DoublyLinkedLists::find(int insert)
{
    element* current = head; // Our current node we are checking.
    while(current != tail) // as long as we aren't at the end do
    {
        if (current->key == insert) // check if the current is the one we are looking for
            return current; // return it 
        current = current->next; // else go to the next element and repeat
    }
    if (tail->key == insert) // at least we check the tail
        return tail;
   return NULL; // if "insert" is not in our list we return NULL
}

You wrote element *find(int insert) I want to point out that the * is linked to element you return a pointer of element.你写了element *find(int insert)我想指出*链接到你返回element指针的元素。 It is more clear if you change the function signature to element* find(int insert)如果把function签名改成element* find(int insert)就更清楚了

Edit: Some pointer explanation.编辑:一些指针解释。 What is a pointer?什么是指针? Every pointer is either 4-bytes long (32 bit) or 8-bytes long.每个指针都是 4 字节长(32 位)或 8 字节长。 A pointer holds a memory address.一个指针持有一个 memory 地址。 The type of pointer (int*, double*, element*, ...) tells the compiler 1) how many bytes to read on the given address and 2) how to interpret them.指针的类型(int*、double*、element*、...)告诉编译器 1)要在给定地址上读取多少字节以及 2)如何解释它们。

Example:例子:

int i = 100; // Let's say i is on adress 0x00000004
int* pI = &i; // pI = 0x00000004 (!) pI is exacly only the address not the value

std::cout << pI; // output: "0x00000004"
std::cout << *pI; // output: 100;

*pI simply tells the compiler "go to adress 0x00000004, take sizeof(int) (4) bytes and trat them as integer." *pI只是告诉编译器“转到地址 0x00000004,获取sizeof(int) (4) 字节并将它们作为 integer。” So the compiler takes bytes 0x00000004 to 0x00000008.所以编译器采用字节 0x00000004 到 0x00000008。

My question was poorly written, but all I've been looking was for a syntax of the function find.我的问题写得不好,但我一直在寻找 function 查找的语法。

DoublyLinkedLists::element *DoublyLinkedLists::find(int insert){
 //here is original code, but its quite long
  return nullptr;
}

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

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