C++中的非类类型错误
Non-class type error in C++
我需要一些关于我正在开发的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);
相关文章:
- 类没有命名C++代码中的类型错误
- 不完整类型错误(E0409、E0070、E0515)
- 使用具有结构不完整类型错误的模板
- 不命名构造函数和析构函数上的类型错误
- 返回派生类型时出现协变类型错误
- 为什么此构造函数没有给出不完整的类型错误?
- 定义模板化结构的特征时出现不完整的类型错误
- 使用重载构造函数时出现不完整的类型错误
- 为什么我得到表达式必须有类类型错误?
- 输出 [left]=input[i] 行中的 c++ 代码中存在无效的类型错误
- 枚举成员不是类型错误
- 如何修复"ctypes"。参数错误:参数 2:<键入"异常.类型错误">:RaspberryPi 中的错误类型"错误
- 外部 "C" Visual Studio 2015 中的显式类型错误,DLL 测试代码
- 非默认析构函数会导致不完整的类型错误
- 为什么按引用传入会导致绑定引用类型错误
- Objective-C C++ 包装器 类型错误的不完整定义
- 输入类型错误
- 当构造函数的参数类型错误时引发异常
- 命名空间中的'bad_cast' 'std' 未命名类型错误
- C UNWORKOWN类型错误