似乎这个错误通常在您没有正确包含类时出现,但在检查我的工作之后似乎一切都井井有条......
为了简洁起见,我创建了一个test.h + test.cpp来向您展示我的代码是如何破解的。出于某种原因,当我尝试在test.cpp中实例化类SetAsList时,它会抛出标题的错误。我评论了代码行
感谢您的任何见解!
主:
#include <iostream>
#include "test.h"
using namespace std;
int main(int argc, char **argv)
{
}
test.h
#ifndef _test
#define _test
#include "SetAsOC.h"
#include "SetAsList.h"
using namespace std;
class test
{
public:
test(){};
void example();
};
#endif
TEST.CPP:
#include "test.h"
void test::example()
{
SetAsList *trial = new SetAsList::SetAsList(); // <-- test.cpp:6:25: error: expected type-specifier
}
SetAsList.h
#ifndef _SETLIST
#define _SETLIST
#include <iostream>
#include <stdlib.h>
#include "Set.h"
using namespace std;
//node for DLL
typedef struct node{
node *previous;
node *next;
int value;
} node;
class SetAsList: Set
{
private:
node *head;
node *tail;
int count;
public:
~SetAsList();
//method=0 is a pure virtual method for abstract classes
int size();
SetAsList();
int& operator[](const int& Index);
void add(int value);
void removeObject(int value);
void removeAt(int index);
int indexOf(int value);
void remove(node *obj); //removes node
};
#endif
SetAsList.cpp
#include "SetAsList.h"
int SetAsList::size()
{
return count;
}
SetAsList::SetAsList()
{
head = NULL;
tail = NULL;
count =0;
}
SetAsList::~SetAsList()
{
node *temp = head;
node *freeNode;
for(int a=0; a< count; a++)
{
freeNode = temp;
temp = temp->next;
delete freeNode;
}
head = NULL;
tail = NULL;
count =0;
}
int& SetAsList::operator[](const int& Index)
{
node *temp = head;
for(int a=0; a< count; a++)
{
if(Index == a)
return temp->value;
temp = temp->next;
}
throw 321;
}
void SetAsList::add(int value)
{
node *newNode = new node();
newNode->value = value;
if(count ==0)
{
head= newNode;
tail = newNode;
}
else
{
tail->next = newNode;
newNode->previous = tail;
tail = newNode;
}
count ++;
}
void SetAsList::removeAt(int index)
{
node *temp = head;
for(int a=0; a< count; a++)
{
if(index == a)
{
return;
}
temp = temp->next;
}
}
void SetAsList::removeObject(int value)
{
node *temp = head;
for(int a=0; a< count; a++)
{
if(value == temp->value)
{
remove(temp);
return;
}
temp = temp->next;
}
}
int SetAsList::indexOf(int value)
{
node *temp = head;
for(int a=0; a< count; a++)
{
if(temp->value == value)
{
return a;
}
temp = temp->next;
}
return -1;
}
void SetAsList::remove(node *obj)
{
if(count ==1)
{ delete head;
head = NULL;
tail = NULL;
}
else
{
node *prev = obj->previous;
node *next = obj->next;
prev->next = next;
next->previous = prev;
delete obj;
}
count--;
}
答案 0 :(得分:3)
正如错误消息所述,SetAsList::SetAsList
不是类型说明符,更改
SetAsList *trial = new SetAsList::SetAsList();
到
SetAsList *trial = new SetAsList();
将调用默认构造函数,您无需指定它。