简体   繁体   中英

Getting a “expected type-specifier” error when I instantiate a custom class in a custom class

It seems that this error usually arises when you don't properly include classes, but after checking my work it seems all is in order...

For the sake of brevity i created a test.h + test.cpp to show you how my code is breaking. For some reason when I attempt to instantiate the class SetAsList in test.cpp it throws me the title's error. I commented the line of code as well

Thank you for any insight!

main:

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

As the error message said, SetAsList::SetAsList is not a type-specifier, change

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

to

SetAsList *trial = new SetAsList();

The default constructor will be called, you don't need to specify it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM