表达式必须具有类类型? [英] Expression Must have Class Type?

查看:642
本文介绍了表达式必须具有类类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我一直在尝试使用Nodes并在尝试对其进行测试时不断遇到该错误。如果使用括号,则会在列表中收到此错误。-表达式必须具有类类型!

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!"

如果我不要使用括号我在 list insert display -无法访问。

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

在Main()中声明我的LList时会发生这种情况。这是怎么回事,为什么呢?

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

我的驱动程序

#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

类节点

#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


推荐答案

您的错误是由于您的类声明导致的:

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

线索出在错误这是不可行的中。因为您没有给定任何访问修饰符,所以此类的所有成员默认为private。要解决此问题,您只需要标记课程的公共部分和私人部分:

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

通过此更改,您的代码应在变量声明的结尾处带或不带括号code>列表。

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

这篇关于表达式必须具有类类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆