【问题标题】:Absurd errors with Visual Studio regarding multiple classesVisual Studio 关于多个类的荒谬错误
【发布时间】:2011-11-20 22:39:39
【问题描述】:

我试图在我的一个项目中使用链接列表来实现一块表数据结构。我的项目有7个文件,如下:

  • LinkedList.cpp

  • LinkedList.h

  • Node.cpp

  • Node.h

  • PieceTable.h

  • PieceTable.cpp

  • Main.cpp

所以这里的问题是,在我的类PieceTable 中,我有一个LinkedList 类型的数据成员。直到昨天一切都很好。我已经多次构建了该项目,并且运行良好。今天早上,我在LinkedList 中添加了一个功能,在PieceTable 中添加了另一个功能。当我尝试构建它时,编译器说:

1>c:\users\devjeet\documents\visual studio 2010\projects\piece table\piece table\piecetable.h(33): error C2079: 'PieceTable::dList' uses undefined class 'LinkedList'

dList 是 LinkedList 类型的类成员的名称。我什至提出了一个前向类声明,编译器说了一些意思:

LinkedList 是一个未定义的类

这里是头文件:

拼图:

#ifndef PIECETABLE_H
#define PIECETABLE_H
#include <Windows.h>
#include <iostream>
#include "LinkedList.h"
#include "Node.h"

class LinkedList;

using namespace std;

class PieceTable
{
public:
    PieceTable(void);
    ~PieceTable(void);

    //buffer realated functions
    void setBuffer(char buffer[]);

    //printing functions
    void printBuffer();
    void printTable();


    //text insertion functions 
    void insertTextAfterPosition(char text, const int& position);
private:
    LinkedList dList;
    char* originalBuffer;
    char* editBuffer;
    int bufferLength;
    int editBufferCounter;

};
#endif

链表:

#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include "Node.h"
#include "PieceTable.h"

class Node;

class LinkedList
{
public:
    LinkedList();
    ~LinkedList();

    bool isEmpty() const;

    //functions that deal with getting nodes
    Node* getNodeAtPosition(const int& position) const;
    Node* getFront() const;
    Node* getBack() const;
    Node* getHead()const;
    Node* getTail()const;
    Node* getNodeFromOffset(const int& offset) const;

    //functions that deal with adding nodes
    void append(const int offset, const int& length,const bool descriptor);
    void add(Node* node, const int offset, const int& length,const bool descroptor);                                //adds a node after the given node
    void insertNodeAfterPosition(const int offset, const int& length,const bool descriptor, const int& position);

    //function concerned with deletion
    void removeNode(Node* node);
    void deleteNodeAtPosition(const int& position);
    void removeBack();
    void removeFront();
    void emptyList();

    //debugging functions
    void printNodes();
private:
    Node* head;
    Node* tail;
};

#endif

请注意,无论我使用#pragma once 还是#ifndef/#endif,都会出现问题

谢谢,

Devjeet

【问题讨论】:

  • 我不会在头文件中放置“使用命名空间”指令,尤其是 std(不是你似乎在使用它的任何东西)。另外为什么 LinkedList.h 需要#include PieceTable.h?他们都试图互相包容!
  • 我这样做(包括piecetable和包括linkedlist)作为解决这个问题的绝望尝试:P

标签: c++ visual-studio-2010


【解决方案1】:

这是一个相当直接的循环包含:piecetable.h 包含 linkedlist.h,而 linkedlist.h 错误地包含 piecetable.h。我相信你可以删除第二个包含,你可以从piecetable.h中删除class LinkedList的前向声明。

【讨论】:

  • 感谢您的回复。我只是这样做了,它并没有改变任何事情
  • 检查node.h 也有虚假夹杂物。
猜你喜欢
  • 2015-01-04
  • 1970-01-01
  • 2015-11-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多