Expression Must have Class Type?

2019-06-06 17:10发布

So I've been playing around with Nodes and keep running into this error when I try to test it. If I use Parentheses I get this Error on list. - "Expression must have class type!"

If I don't use Parentheses I get this Error on list, insert and display - "this is inaccessible."

This happens when Declaring my LList in Main(). What's going on and why is this?

My Driver

#include "LList.h"
#include <iostream>
using namespace std;

int main()
{
    LList<int> list;
    bool test = list.insert(5);
    list.display();

    return 0;
}

Class LList

#include "Nodes.h"
#ifndef LLIST_H
#define LLIST_H

template<typename TYPE>
class LList
{
    Node<TYPE>* front;
    LList();
    ~LList();
    bool insert(const TYPE& dataIn);
    void display() const;
};

template<typename TYPE>
LList<TYPE>::LList()
{
    front = null;
};

template<typename TYPE>
LList<TYPE>::~LList()
{
    Node<TYPE>* temp;
    while(front)
    {
        temp = front;
        front = fornt -> next;
        delete temp;
    }
};

template<typename TYPE>
bool LList<TYPE>::insert(const TYPE& dataIn)
{
    bool success = false;
    Node<TYPE> pBefore = null;
    Node<TYPE> pAfter = front;

    while(pAfter && PAfter->data < dataIn)
    {
        pBefore = pAfter;
        pAfter = pAfter->next;
    }

    if(Node<TYPE>* store = new Node<TYPE>)
        store->data = dataIn

    return success;
};

template<typename TYPE>
void LList<TYPE>::display() const
{
    TYPE* temp = front;
    while(front && temp->next != null)
    {
        cout << temp->data << endl;
    }
};

#endif

Class Nodes

#ifndef NODES_H
#define NODES_H

template<typename TYPE>
struct Node
{
    Node<TYPE>* next;
    TYPE data;
    Node();
    Node(TYPE d, Node<TYPE> n);
};
template<typename TYPE>
Node<TYPE>::Node()
{
    data = 0;
    next = null;
};
template<typename TYPE>
Node<TYPE>::Node(TYPE d, Node<TYPE> n)
{
    data = d;
    next = n;
};

#endif

1条回答
来,给爷笑一个
2楼-- · 2019-06-06 17:35

Your errors are a result of your class declaration:

template<typename TYPE>
class LList
{
    Node<TYPE>* front;
    LList();
    ~LList();
    bool insert(const TYPE& dataIn);
    void display() const;
};

The clue is in the error "This is inaccesible." Because you have not given any access modifiers, all of the members of this class default to private. To fix this, you just need to label the public and private sections of your class:

template<typename TYPE>
class LList
{
    public:
        LList();
        ~LList();
        bool insert(const TYPE& dataIn);
        void display() const;

    private:
        Node<TYPE>* front;
};

With this change, your code should work with or without parentheses at the end of your variable declaration for list.

查看更多
登录 后发表回答