【问题标题】:(c++) Syntax errors for node header [duplicate](c ++)节点标头的语法错误[重复]
【发布时间】:2017-04-24 08:59:00
【问题描述】:

我收到此标头的所有语法错误和令牌错误。

#pragma once
#include "Book.h"
#include <string>
using namespace std;
/**
* This is a node class, they will hold object
* data and nodes will be utilized in List class
*/
class Node
{
public:
    /**
    * Purpose: Default constructor
    * Parameters: none
    * Returns: none
    * Pre-conditions: none
    * Post-conditions: NULL node is added to Linked List
    */
    Node();

    /**
    * Purpose: De-constructor
    * Parameters: none
    * Returns: none
    * Pre-conditions: none
    * Post-conditions: Node is deleted
    */
    ~Node();

    /**
    * Purpose: Parameterized constructor
    * Parameters: Book
    * Returns: none
    * Pre-conditions: none
    * Post-conditions: node is added to Linked List
    *                  with data
    */
    Node(Book* B);

    /**
    * Purpose: Name getter
    * Parameters: none
    * Returns: Book
    * Pre-conditions: Item name is not NULL
    * Post-conditions: none
    */
    Book* getBook();

    /**
    * Purpose: Next node getter
    * Parameters: none
    * Returns: Pointer for next node in list
    * Pre-conditions: Must have another node in list ahead of this
    * Post-conditions: none
    */
    Node* getNextNode();

    /**
    * Purpose: Next node setter
    * Parameters: pointer for next node in list
    * Returns: none
    * Pre-conditions: none
    * Post-conditions: List has new node ahead of this
    */
    void setNextNode(Node* a);

private:
    Book* thisBook;
    string itemName;
    string itemType;
    int itemNum;
    Node* nextNode;
};

一切似乎都井然有序,但我收到了这些错误

Error   C2061   syntax error: identifier 'Book'     38  

Error   C2535   'Node::Node(void)': member function already defined or declared     38  

Error   C2143   syntax error: missing ';' before '*'    47  

Error   C4430   missing type specifier - int assumed. Note: C++ does not support default-int    47  

Error   C2238   unexpected token(s) preceding ';'   47  

Error   C2143   syntax error: missing ';' before '*'    68  

Error   C4430   missing type specifier - int assumed. Note: C++ does not support default-int    68  

Error   C2238   unexpected token(s) preceding ';'   68  

我正在创建一个链接列表,这些类型的错误也出现在我的 List.h 文件中。我错过了某种减速吗?至于第二个错误,它指向了参数化的构造函数。

编辑:书籍定义代码

#pragma once
#include "Node.h"
#include "List.h"
#include <string>
using namespace std;

/**
* Class designed to store information
* of a book. This class is the BASE.
*/
class Book
{
private:
    string author;
    string title;
    double price;

public:

    /**
    * Purpose: Default constructor
    * Parameters: none
    * Returns: none
    * Pre-conditions: none
    * Post-conditions: none
    */
    Book();

    /**
    * Purpose: Constructor
    * Parameters: author, address, price
    * Returns: none
    * Pre-conditions: none
    * Post-conditions: variables saved
    */
    Book(string, string, double);

    /**
    * Purpose: Getter for Author class
    * Parameters: none
    * Returns: Author class
    * Pre-conditions: none
    * Post-conditions: none
    */
    string getAuthor();

    /**
    * Purpose: Getter for title
    * Parameters: none
    * Returns: title
    * Pre-conditions: title must have been saved
    * Post-conditions: none
    */
    string getTitle();

    /**
    * Purpose: Getter for price
    * Parameters: none
    * Returns: price
    * Pre-conditions: price must have been saved
    * Post-conditions: none
    */
    double getPrice();
};

【问题讨论】:

  • 编译器日志已经是你的好朋友了——仔细阅读并尝试理解每个错误是什么。 missing ; 之类的东西应该很明显
  • 我不认为 Book.h 包含 Node.h?
  • @aschepler 我相信你是对的。:)
  • 它确实包含 Node.h
  • 问题似乎出在您对Book 对象的定义中。也许你可以给我们看一下代码?

标签: c++ list syntax token nodes


【解决方案1】:

如果这两个标头都包含在同一个文件中,根据顺序,它将尝试在Book 之前定义Node

为了解决这种依赖关系,要么确保它们包含在正确的顺序中,要么声明Book而不在class Node之前定义它

class Book;

class Node
{
    ...
};

【讨论】:

  • 这些是单独的头文件。不过感谢您提供的信息。
  • 试过这个并结合List和Node。有效!谢谢!
猜你喜欢
  • 2020-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-07
  • 2018-06-15
  • 1970-01-01
  • 1970-01-01
  • 2015-09-17
相关资源
最近更新 更多