简体   繁体   English

#ifndef 不允许我的文件查看 header (C++) 中的内容

[英]#ifndef not letting my files see what's in the header (C++)

So I created a Binary tree class and I want to separate the class definition from its member function definition by putting the class def in the.h file and the function definitions in the.cpp file. So I created a Binary tree class and I want to separate the class definition from its member function definition by putting the class def in the.h file and the function definitions in the.cpp file. Then I would include the.h file into the.cpp file.然后我会将.h 文件包含到.cpp 文件中。 Now I got this to all work and it works fine.现在我得到了所有的工作,它工作正常。 the problem is when I want to put the name including guards.问题是当我想输入包括警卫在内的名字时。 Because I need to include the.h file in the member function.cpp file and in another.cpp that uses the binary tree class, I have to include these guards.因为我需要在成员 function.cpp 文件和另一个使用二叉树 class 的.cpp 文件中包含.h 文件,所以我必须包含这些保护。 But for some reason, once I include them both.cpp files don't seem to "see" the contents of the header file.但是由于某种原因,一旦我将它们都包含在内,这两个.cpp 文件似乎就不会“看到” header 文件的内容。 I'm new to using to ifndef so I'm so sure what I'm doing wrong.我是使用 ifndef 的新手,所以我很确定我做错了什么。 thank you谢谢你

Here's the header file called node.h:这是名为 node.h 的 header 文件:

#ifndef NODE_H
#define NODE_H

#include <iostream>
#include <string>
#include <vector>
#include <stdbool.h>
using std::cout;
using std::endl;
typedef std::vector<std::string> vectorString;

class BST  
{ 
    vectorString data; 
    BST *left, *right; 
  
public: 
    // Default constructor. 
    BST(); 
  
    // Parameterized constructor. 
    BST(std::string); 
  
    // Insert function. 
    BST* Insert(BST*, std::string); 
  
    // Inorder traversal. 
    void Inorder(BST*); 

    // PreOrder Traversal.
    void PreOrder(BST*);

    // PostOrder Traversal
    void PostOrder(BST*);

    // string slicer
    std::string strSlice(std::string);

    // boolean isDuplicate
    bool isDuplicate(std::string, vectorString);

    // print vector
    void printVector(vectorString);
};

#endif

and here's the member definition file called node.cpp这是名为 node.cpp 的成员定义文件



#include <iostream>
//#include "node.h" 
#include <string>
#include <vector>
using std::cout;
using std::endl;
  
  
// Default Constructor definition. 
BST ::BST()
    : data(0) 
    , left(NULL) 
    , right(NULL) 
{ 
} 
  
// Parameterized Constructor definition. 
BST ::BST(std::string value) 
{ 
    if(data.empty()){
        data.push_back(strSlice(value));
    }
        data.push_back(value); 
    
    left = right = NULL; 
} 
  
// String slicing function definition
std::string BST ::strSlice(std::string word){
    std::string word2 = "";
    word2 += word[0];
    word2 += word[1];
    return word2;
}


// print vector function definition
void BST ::printVector(std::vector<std::string> dataVector){
    for(int i = 0; i < dataVector.size(); i ++){
        cout << dataVector.at(i) << " ";
    }
}

// Insert function definition. 
BST* BST ::Insert(BST* root, std::string value) 
{ 
    if (!root)  
    { 
        // Insert the first node, if root is NULL. 

        return new BST(value); 
    } 
  
    // Insert data. 
    if (strSlice(value).compare(root->data.at(0)) > 0)  
    { 
        // Insert right node data, if the 'value' 
        // to be inserted is greater than 'root' node data. 
        cout << value << " is being put in the right node " << value << " > " << root->data.at(0) << endl;
        // Process right nodes. 
        root->right = Insert(root->right, value); 
        
    } else if (strSlice(value).compare(root->data.at(0)) == 0) {

        cout << value << " is being put in the same node " << value << " = " << root->data.at(0)  << endl;
        root->data.push_back(value);
    }
    else 
    { 
        // Insert left node data, if the 'value' 
        // to be inserted is greater than 'root' node data. 
        cout << value << " is being put in the left node " << value << " < " << root->data.at(0) << endl;
        // Process left nodes. 
        root->left = Insert(root->left, value); 
    } 
  
    // Return 'root' node, after insertion. 
    // cout << "after insert root is " << root << endl;
    return root; 
} 
  
// Inorder traversal function. 
// This gives data in sorted order. 
void BST ::Inorder(BST* root) 
{ 
    if (!root) { 
        return; 
    } 
    Inorder(root->left); 
    printVector(root->data);
    cout << endl; 
    Inorder(root->right); 
}
void BST::PreOrder(BST* root){
    if(!root){
        return;
    }
    root->printVector(root->data);
    cout << endl;
    PreOrder(root->left);
    PreOrder(root->right);
} 
void BST::PostOrder(BST* root){
    if(!root){
        return;
    }
    PostOrder(root->left);
    PostOrder(root->right);
    root->printVector(root->data);
    cout << endl;
} 

errors:错误:

C:\Users\14jjo\C++ Projects\Project 0\node.cpp:12:1: error: 'BST' does not name a type
 BST ::BST()
 ^~~
C:\Users\14jjo\C++ Projects\Project 0\node.cpp:20:1: error: 'BST' does not name a type
 BST ::BST(std::string value)
 ^~~

and here's the class that is trying to implement the binary tree class called P0.cpp:这是试图实现名为 P0.cpp 的二叉树 class 的 class:

#include <iostream>
//#include "tree.h"
#include "cleanString.h"
#include "node.h"
#include <fstream>
#include <string>
using std::cout;

using std::endl;

int main(int argc, char** argv) {
    std::ifstream fileRead;
    std::string word;

    fileRead.open(argv[1]);
    if(!fileRead){
        cout << "this is not a file\n";
    } else { cout << "this is a file\n"; }

    fileRead >> word;
    word = cleanString(word);
    BST tree, *root = nullptr;
    root = tree.Insert(root, word);

    while (fileRead >> word) {
        word = cleanString(word);
        tree.Insert(root, word);
    }

    tree.Inorder(root);

    fileRead.close();


    return 0;
}

errors:错误:

C:/Users/14jjo/C++ Projects/Project 0/P0.cpp:22: undefined reference to `BST::BST()'
C:/Users/14jjo/C++ Projects/Project 0/P0.cpp:23: undefined reference to `BST::Insert(BST*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'

just #include "node.h" and make sure to compile all cpp files只需#include "node.h" 并确保编译所有 cpp 文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM