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

标签 c++ c++11 g++

似乎这个错误通常是在你没有正确地包含类时出现的,但在检查我的工作之后似乎一切正常......

为了简洁起见,我创建了一个 test.h + test.cpp 来向您展示我的代码是如何被破坏的。出于某种原因,当我尝试在 test.cpp 中实例化类 SetAsList 时,它会抛出标题错误。我也注释了这行代码

感谢您的任何见解!

主要内容:

#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
}

设置为列表.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();

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

关于c++ - 在自定义类中实例化自定义类时出现 "expected type-specifier"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27242955/

相关文章:

c++ - 成员初始化列表错误中的模板基构造函数调用

c++ - 使用范围解析运算符传递函数指针arduino

c++ - 在 GLFW 中单击无需鼠标拖动

c++ - 无法推导 std::function 模板参数

c++ - GCC 因可变参数模板和指向成员函数的指针而失败

c++ - 带有转发的延迟初始化

macos - C++、OS X 的 clang 与 GCC 中的 Lambda 表达式

c++ - g++ "does not name a type"错误

c++ - dynarrays 的状态是什么?

c++ - 无法通过 VIM 运行链接脚本