在自定义类中实例化自定义类时出现"expected type-specifier"错误



似乎此错误通常在您没有正确包含类时出现,但是在检查我的工作后,似乎一切都井井有条......

为了简洁起见,

我创建了一个test.h + test.cpp向您展示我的代码是如何中断的。出于某种原因,当我尝试在测试中实例化类 SetAsList 时.cpp它抛出了标题的错误。我也评论了代码行

感谢您的任何见解!

主要:

#include <iostream>
#include "test.h"
using namespace std;
int main(int argc, char **argv)
{
}

测试.h

#ifndef _test
#define _test
#include "SetAsOC.h"
#include "SetAsList.h"
using namespace std;
class test
{
public:
    test(){};
    void example();
};
#endif

测试.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

设置列表.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--;
}

正如错误消息所说,SetAsList::SetAsList不是类型说明符,请更改

SetAsList *trial = new SetAsList::SetAsList();

SetAsList *trial = new SetAsList();

将调用默认构造函数,无需指定它。

相关内容

最新更新