C++中的非类类型错误

Non-class type error in C++

本文关键字:类型 错误 C++      更新时间:2023-10-16

我需要一些关于我正在开发的C++程序的帮助。

我正在上操作系统的课,所以我们的前几周是C编程的速成班,但现在我们应该把C程序升级到C++。我的教授向我们展示了一些示例代码,并向我们展示一些教程,但他们只让我了解了这一点。

我们使用一个头文件、一个.cpp文件来实现这些功能,以及一个测试文件(这就是我的错误来源。

//dll.h
#ifndef _DLL_H
#define _DLL_H
using namespace std;
#include <iostream>
#include <string>
class dll{
public:
typedef struct _Node
{
    struct _Node *pNode;
    struct _Node *nNode;
    int nodeValue;
}sNode;
typedef struct
{
    sNode first; 
}DLList;
dll();
~dll();
void init(DLList *DLL,int d);
void sort(DLList *DLL);
void print(DLList *DLL);
};
#endif

主.cpp文件:

//dll.cpp
using namespace std; 
#include <iostream>
#include <string>
#include <stdio.h>
#include <cstdlib>
#include "dll.h"
dll::dll(){
    cout<<"Constructor called"<<endl;
}
dll::~dll(){
    cout<<"Destructor called"<<endl;
}
void dll::init(DLList *DLL, int d)
{
    sNode *node;    
    sNode *pNode;
    pNode = &(DLL-> first);
    pNode->pNode = NULL;
    int i;
    for(i=0; i<d; i++)
    {
            node = (sNode*) malloc(sizeof(node));
            node-> nodeValue = rand();
            node-> pNode = pNode;
            node-> nNode = NULL;
            pNode-> nNode = node;
            pNode = node;
    }
}
void dll::print(DLList *DLL)
{
    int i= 1;
    sNode *nNode = DLL-> first.nNode;   
    while(nNode != NULL)
    {
            cout<<("%d.  %dn",i,nNode-> nodeValue);
            cout<<("");
            nNode = nNode-> nNode;
            i++;
    }
}
void dll::sort(DLList *DLL)
{
    int change = 1;
    while(change== 1)
    {
            change = 0;     
            sNode *current = DLL-> first.nNode;
            while(current-> nNode != NULL)
            {
                    if(current-> nodeValue > current-> nNode-> nodeValue)
                    {
                            int temp = current-> nodeValue;
                            current-> nodeValue = current-> nNode-> nodeValue;
                            current-> nNode-> nodeValue = temp;
                            change = 1;
                    }
                    current = current-> nNode;
            }
    }
    }

现在是测试文件,这就是我的错误不断弹出的地方:

//testDLL.cpp

using namespace std; 
#include <iostream>
#include <string>
#include <stdio.h>
#include <cstdlib>
#include "dll.h"
int main()
{
    cout<<("Doubly Linked List before sorting: n");    
    dll::DLList DLL;
    dll *test =  new dll();
    test.dll::init(&DLL,5);
    test.dll::print(&DLL);
    test.dll::sort(&DLL);
    cout<<("nDoubly Linked List after sorting: n");
    test.dll::print(&DLL);
    return 0;
} 

随着程序的编写,每次我试图编译(在linux命令行上使用g++)时,我都会不断地遇到这个问题:

testDLL.cpp: In function ‘int main()’:
testDLL.cpp:17:7: error: request for member ‘init’ in ‘test’, which is of non-class type ‘dll*’
testDLL.cpp:19:12: error: request for member ‘dll:: print’ in ‘test’, which is of non-class type ‘dll*’
testDLL.cpp:21:12: error: request for member ‘dll:: sort’ in ‘test’, which is of non-class type ‘dll*’
testDLL.cpp:25:12: error: request for member ‘dll:: print’ in ‘test’, which is of non-class type ‘dll*’

我完全被这件事难住了,所以你们能给我的任何帮助都将不胜感激。

如果使用dll作为指针,则声明是正确的。

dll* test = new dll();

如果使用dll作为对象,则声明应为:

dll test;

这将导致构造函数和析构函数自动调用。

如果您使用dll作为指针,则对init的调用应该是(您不必检查NULL,但这将有助于防止程序崩溃):

if (dll != NULL) {
    dll->init(&DLL, 5);
}

如果使用dll作为对象,则对init的调用应为:

 dll.init(&DLL, 5);