【问题标题】:Including .h File for 2 Classes包括 2 个类的 .h 文件
【发布时间】:2015-08-09 21:05:31
【问题描述】:

我想将这个单一的 .cpp 文件转换成多个文件,但我在这样做时遇到了麻烦。我收到 undeclared identifier'children' : is not a member of 'Node' 编译器错误。

这是我原来的完整代码:

#include <iostream>
#include <vector>
using namespace std;

class Node {
public:
    Node();
    ~Node() {}
    char content();
    void setContent(char c);
    bool wordMarker();
    void setWordMarker();
    Node* findChild(char c);
    void appendChild(Node* child);
    vector<Node*> children();

private:
    char m_Content;
    bool m_Marker;
    vector<Node*> m_Children;
};

class Trie {
public:
    Trie();
    ~Trie();
    void addWord(string s);
    bool searchWord(string s);
    void deleteWord(string s);
private:
    Node* root;
};

Node::Node() 
{ 
    m_Content = ' '; m_Marker = false; 
}

char Node::content() 
{ 
    return m_Content; 
}
void Node::setContent(char c) 
{ 
    m_Content = c; 
}
bool Node::wordMarker() 
{ 
    return m_Marker; 
}
void Node::setWordMarker() 
{ 
    m_Marker = true; 
}
void Node::appendChild(Node* child) 
{ 
    m_Children.push_back(child); 
}
vector<Node*> Node::children() 
{ 
    return m_Children; 
}

Node* Node::findChild(char c)
{
    for (int i = 0; i < m_Children.size(); i++)
    {
        Node* tmp = m_Children.at(i);
        if (tmp->content() == c)
        {
            return tmp;
        }
    }

    return NULL;
}

Trie::Trie()
{
    root = new Node();
}

Trie::~Trie()
{
    // Free memory
}

void Trie::addWord(string s)
{
    Node* current = root;

    if (s.length() == 0)
    {
        current->setWordMarker(); // an empty word
        return;
    }

    for (int i = 0; i < s.length(); i++)
    {
        Node* child = current->findChild(s[i]);
        if (child != NULL)
        {
            current = child;
        }
        else
        {
            Node* tmp = new Node();
            tmp->setContent(s[i]);
            current->appendChild(tmp);
            current = tmp;
        }
        if (i == s.length() - 1)
            current->setWordMarker();
    }
}


bool Trie::searchWord(string s)
{
    Node* current = root;

    while (current != NULL)
    {
        for (int i = 0; i < s.length(); i++)
        {
            Node* tmp = current->findChild(s[i]);
            if (tmp == NULL)
                return false;
            current = tmp;
        }

        if (current->wordMarker())
            return true;
        else
            return false;
    }

    return false;
}

// Test program
int main()
{
    Trie* trie = new Trie();
    trie->addWord("Hello");
    trie->addWord("Balloon");
    trie->addWord("Ball");

    if (trie->searchWord("Hell"))
        cout << "Found Hell" << endl;

    if (trie->searchWord("Hello"))
        cout << "Found Hello" << endl;

    if (trie->searchWord("Helloo"))
        cout << "Found Helloo" << endl;

    if (trie->searchWord("Ball"))
        cout << "Found Ball" << endl;

    if (trie->searchWord("Balloon"))
        cout << "Found Balloon" << endl;

    delete trie;
}

我的尝试:

节点.h

#ifndef NODE_INCLUDED
#define NODE_INCLUDED

class Node {
public:
    Node();
    ~Node() {}
    char content();
    void setContent(char c);
    bool wordMarker();
    void setWordMarker();
    Node* findChild(char c);
    void appendChild(Node* child);
    vector<Node*> children();

private:
    char m_Content;
    bool m_Marker;
    vector<Node*> m_Children;
};

#endif // NODE_INCLUDED

trie.h

#ifndef TRIE_INCLUDED
#define TRIE_INCLUDED
class Trie {
public:
    Trie();
    ~Trie();
    void addWord(string s);
    bool searchWord(string s);
    void deleteWord(string s);
private:
    Node* root;
};
#endif //TRIE_INCLUDED

node.cpp

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

Node::Node() 
{ 
    m_Content = ' '; m_Marker = false; 
}

char Node::content() 
{ 
    return m_Content; 
}
void Node::setContent(char c) 
{ 
    m_Content = c; 
}
bool Node::wordMarker() 
{ 
    return m_Marker; 
}
void Node::setWordMarker() 
{ 
    m_Marker = true; 
}
void Node::appendChild(Node* child) 
{ 
    m_Children.push_back(child); 
}
vector<Node*> Node::children() 
{ 
    return m_Children; 
}

Node* Node::findChild(char c)
{
    for (int i = 0; i < m_Children.size(); i++)
    {
        Node* tmp = m_Children.at(i);
        if (tmp->content() == c)
        {
            return tmp;
        }
    }

    return NULL;
}

trie.cpp

Trie::Trie()
{
    root = new Node();
}

Trie::~Trie()
{
    // Free memory
}

void Trie::addWord(string s)
{
    Node* current = root;

    if (s.length() == 0)
    {
        current->setWordMarker(); // an empty word
        return;
    }

    for (int i = 0; i < s.length(); i++)
    {
        Node* child = current->findChild(s[i]);
        if (child != NULL)
        {
            current = child;
        }
        else
        {
            Node* tmp = new Node();
            tmp->setContent(s[i]);
            current->appendChild(tmp);
            current = tmp;
        }
        if (i == s.length() - 1)
            current->setWordMarker();
    }
}


bool Trie::searchWord(string s)
{
    Node* current = root;

    while (current != NULL)
    {
        for (int i = 0; i < s.length(); i++)
        {
            Node* tmp = current->findChild(s[i]);
            if (tmp == NULL)
                return false;
            current = tmp;
        }

        if (current->wordMarker())
            return true;
        else
            return false;
    }

    return false;
}

main.cpp

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

// Test program

#include "node.h" //is this needed?
#include "trie.h" //is this needed?
int main()
{
    Trie* trie = new Trie();
    trie->addWord("Hello");
    trie->addWord("Balloon");
    trie->addWord("Ball");

    if (trie->searchWord("Hell"))
        cout << "Found Hell" << endl;

    if (trie->searchWord("Hello"))
        cout << "Found Hello" << endl;

    if (trie->searchWord("Helloo"))
        cout << "Found Helloo" << endl;

    if (trie->searchWord("Ball"))
        cout << "Found Ball" << endl;

    if (trie->searchWord("Balloon"))
        cout << "Found Balloon" << endl;

    delete trie;
}

【问题讨论】:

  • 那是错误信息的准确副本吗?它出现在哪一行?
  • 由于“main.cpp”不使用Node,你不应该在其中包含“node.h”。由于“trie.cpp”确实使用Node,因此您应该在其中包含它。你应该在“trie.h”的顶部添加一个前向声明class Node;
  • @user2079303 错误信息在这里:imgur.com/zVmPBTk
  • @molbdnilo 谢谢我已经这样做了,但遗憾的是我仍然收到错误消息:imgur.com/zVmPBTk
  • 是的,您将能够做到!我们只需要摆脱所有错误。我尝试编译您的代码,并注意到您在 node.h 上使用了向量,因此应该包含:#include &lt;vector&gt;using namespace std; 以使这些向量起作用。

标签: c++ include


【解决方案1】:

这就是我的工作,我对我添加或删除的内容发表评论。

节点.h

#ifndef NODE_INCLUDED
#define NODE_INCLUDED

#include <vector> //Class Node uses vector so it should be included
#include <iostream> //Class Node uses iostream so it should be included

class Node {
public:
    Node();
    ~Node() {}
    char content();
    void setContent(char c);
    bool wordMarker();
    void setWordMarker();
    Node* findChild(char c);
    void appendChild(Node* child);
    std::vector<Node*> children();

private:
    char m_Content;
    bool m_Marker;
    std::vector<Node*> m_Children;
};

#endif // NODE_INCLUDED

trie.h

#ifndef TRIE_INCLUDED
#define TRIE_INCLUDED

#include "node.h" //Class Trie uses Node so it should include it
#include <string> //Class Node uses string so it should include it

class Trie {
public:
    Trie();
    ~Trie();
    void addWord(string s);
    bool searchWord(string s);
    void deleteWord(string s);
private:
    Node* root;
};
#endif //TRIE_INCLUDED

node.cpp

#include "node.h"
//#include <iostream>
//#include <vector>
using namespace std;
//Here you got all includes right but they should be in the header file!

Node::Node() 
{ 
    m_Content = ' '; m_Marker = false; 
}

char Node::content() 
{ 
    return m_Content; 
}
void Node::setContent(char c) 
{ 
    m_Content = c; 
}
bool Node::wordMarker() 
{ 
    return m_Marker; 
}
void Node::setWordMarker() 
{ 
    m_Marker = true; 
}
void Node::appendChild(Node* child) 
{ 
    m_Children.push_back(child); 
}
vector<Node*> Node::children() 
{ 
    return m_Children; 
}

Node* Node::findChild(char c)
{
    for (int i = 0; i < m_Children.size(); i++)
    {
        Node* tmp = m_Children.at(i);
        if (tmp->content() == c)
        {
            return tmp;
        }
    }

    return NULL;
}

trie.cpp

#include "trie.h" //Trie.cpp should at least contain its header file

Trie::Trie()
{
    root = new Node();
}

Trie::~Trie()
{
    // Free memory
}

void Trie::addWord(string s)
{
    Node* current = root;

    if (s.length() == 0)
    {
        current->setWordMarker(); // an empty word
        return;
    }

    for (int i = 0; i < s.length(); i++)
    {
        Node* child = current->findChild(s[i]);
        if (child != NULL)
        {
            current = child;
        }
        else
        {
            Node* tmp = new Node();
            tmp->setContent(s[i]);
            current->appendChild(tmp);
            current = tmp;
        }
        if (i == s.length() - 1)
            current->setWordMarker();
    }
}


bool Trie::searchWord(string s)
{
    Node* current = root;

    while (current != NULL)
    {
        for (int i = 0; i < s.length(); i++)
        {
            Node* tmp = current->findChild(s[i]);
            if (tmp == NULL)
                return false;
            current = tmp;
        }

        if (current->wordMarker())
            return true;
        else
            return false;
    }

    return false;
}

main.cpp

//#include "node.h" //You don't use Node here, so don't include it!
                    //Keep your code simple
#include <iostream>
#include <vector>
using namespace std;

// Test program

//#include "node.h" //is this needed? Not needed because you don't use 
                    //its declarations directly here and
                    //you had it declared before
#include "trie.h" //is this needed? Yes, it is. You use that class 
                  //directly here, so it should include it
int main()
{
    Trie* trie = new Trie();
    trie->addWord("Hello");
    trie->addWord("Balloon");
    trie->addWord("Ball");

    if (trie->searchWord("Hell"))
        cout << "Found Hell" << endl;

    if (trie->searchWord("Hello"))
        cout << "Found Hello" << endl;

    if (trie->searchWord("Helloo"))
        cout << "Found Helloo" << endl;

    if (trie->searchWord("Ball"))
        cout << "Found Ball" << endl;

    if (trie->searchWord("Balloon"))
        cout << "Found Balloon" << endl;

    delete trie;
}

顺便说一句,你不应该在头文件中使用using namespace,因为它是bad practice

【讨论】:

  • 不要将using namespace放在头文件中;它只会导致悲伤。 stackoverflow.com/questions/5849457/…
  • @Albert 非常感谢!因此,对于良好的做法,1.“库包含”应该只出现在标题中 2.如果需要,使用命名空间应该在 .cpp 中
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多