Expression Must have Class Type?

老子叫甜甜 提交于 2019-12-10 11:54:07

问题


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:


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.



来源:https://stackoverflow.com/questions/12557655/expression-must-have-class-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!