我需要一些关于我正在开发的 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. %d\n",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*’
我完全被这个难住了,所以你们能给我的任何帮助将不胜感激。