簡體   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. 然后我會將.h 文件包含到.cpp 文件中。 現在我得到了所有的工作,它工作正常。 問題是當我想輸入包括警衛在內的名字時。 因為我需要在成員 function.cpp 文件和另一個使用二叉樹 class 的.cpp 文件中包含.h 文件,所以我必須包含這些保護。 但是由於某種原因,一旦我將它們都包含在內,這兩個.cpp 文件似乎就不會“看到” header 文件的內容。 我是使用 ifndef 的新手,所以我很確定我做錯了什么。 謝謝你

這是名為 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

這是名為 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;
} 

錯誤:

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)
 ^~~

這是試圖實現名為 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;
}

錯誤:

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> >)'

只需#include "node.h" 並確保編譯所有 cpp 文件

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM