簡體   English   中英

我需要在我的主文件和頭文件cpp文件中使用向量類……什么是正確的協議?

[英]I need to use the vector class in my main and in my header cpp file … What's the proper protocol?

抱歉。 我對構建更大的程序,將文件鏈接在一起以及所有這些好東西有些陌生。

我的標頭.h文件中的函數聲明中包含std :: vector。 我的標頭.cpp文件中的那些函數實現也使用std :: vector。 根據我的理解,我應該只在頭文件.h文件中使用“ #include”,因為該文件中的所有內容實際上都將在編譯時粘貼到我的頭文件.cpp文件中。 我只是想確保。

我的.cpp文件:

#include "PlagiarismDetector_Header.h"

// implementations of functions defined in PlagiarismDetector_Header.h

PlagiarismDetector_Header.h:

#include <iostream> // std::cout, std::endl
#include <vector>  // std::vector
#include <string> // std::string
#include <algorithm> // std::swap
#include <math.h> // sqrt()
#include <set> // std::set
#include <map> // std::map
#include <fstream> // std::ifstream

std::vector<std::vector<std::string> > get_file_sntncs(std::fstream&);
std::vector<std::string> get_sntnc_wrds(const std::string&);
double sntnc_smlrty_qtnt(std::vector<std::string>, std::vector<std::string>);

// All the characters that constitute a word 
const char LETTERS_ARR[] = {"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'."};
const std::set<char> LETTERS_SET(LETTERS_ARR, LETTERS_ARR +    sizeof(LETTERS_ARR)/sizeof(char));

您完全正確,但請考慮以下方面:

  • 嘗試在頭文件中放置一個include防護 ,因為您可能需要在以后的文件中將它包含在行中兩次
  • 考慮將不需要公開給extern模塊的所有常量和函數聲明下推到實現文件中(包括頭文件的任何文件都將看到它們,甚至不需要使用它們也很困難)
  • 如果您在其他文件(將包含標頭)中使用這些常量,請遵循john的建議並將其extern

暫無
暫無

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

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