2013-02-03 48 views
-1

我一直试图将我的程序拆分成头文件和主文件,但无法弄清楚头文件如何与此一起工作。任何帮助试图了解这一点,将不胜感激。将程序拆分为头文件和主文件

#include <iostream> 
#include <fstream> 
#include <string> 
#include <set> 


void comparing(std::ifstream& fileIn, std::ifstream& keywords, std::ofstream& outFile) 
{ 
    std::string str; 
    std::string word; 
    std::set<std::string> keywordsSet; 
     int check = 0; 

     while (keywords >> word) { 
     keywordsSet.insert(word); 
     } 

    while(fileIn >> str){ 

     if(str == "<p>") { 
      check++; 
     } 


     if((check > 1 && str == "<p>") || (check == 1 && str == "</body>")) 
     { 
      outFile << "</p>"; 
      check--; 
     } 



    if(keywordsSet.find(str) != keywordsSet.end()) { 

     outFile << "<i>" << str << "</i>" << " "; 
     } 

     else{ 
     outFile << str << " "; 
     }   
    } 

} 
int main(int argc, char* argv[]) 
{ 

std::ifstream fileIn; 
fileIn.open (argv[1]); 

std::ifstream keywords; 
keywords.open (argv[2]); 

std::ofstream outFile; 
outFile.open(argv[3]); 

    comparing(fileIn, keywords, outFile); 

fileIn.close(); 
keywords.close(); 
outFile.close(); 

return 0; 
} 
+0

你应该投资一本好的C++书。 – ChiefTwoPencils

回答

0

你通常放置在头类和函数的声明,然后将在.cpp文件中的定义和实现。

2

在您的代码中,您声明了一个函数:comparing。您通常将所有声明放在一个头文件中(MSVC++使用.h后缀,但我更喜欢.hpp用于C++头文件)以及.cpp文件中的实现。

页眉(comparing.hpp,或沿着这些线路的东西)是这样的:

// Prevents redefinitions when including the same header multiple times 
#pragma once 

// Does the same thing, just to make sure 
// (some compilers don't support #pragma once) 
#ifndef COMPARING_HPP 
#define COMPARING_HPP 

#include <fstream> 

void comparing(std::ifstream&, std::ifstream&, std::ofstream&); // Function prototype 

#endif 

和你comparing.cpp文件应该是这样的:

#include "comparing.hpp" 
// Note the quotation marks instead of greater than, less than signs 
// This is because the header is not in the standard include path, 
// but rather in your project include path. 

// Implementation, belongs in cpp file 
void comparing(std::ifstream& fileIn, std::ifstream& keywords, std::ofstream& outFile) 
{ 
    std::string str; 
    std::string word; 
    std::set<std::string> keywordsSet; 
     int check = 0; 

     while (keywords >> word) { 
     keywordsSet.insert(word); 
     } 

    while(fileIn >> str){ 

     if(str == "<p>") { 
      check++; 
     } 


     if((check > 1 && str == "<p>") || (check == 1 && str == "</body>")) 
     { 
      outFile << "</p>"; 
      check--; 
     } 



    if(keywordsSet.find(str) != keywordsSet.end()) { 

     outFile << "<i>" << str << "</i>" << " "; 
     } 

     else{ 
     outFile << str << " "; 
     }   
    } 

} 

然后你可以使用compare功能在任何.cpp,只要你包含标题。所以,你的main.cpp *文件应该是这样的:

#include "comparing.hpp" 

int main(int argc, char* argv[]) 
{ 

std::ifstream fileIn; 
fileIn.open (argv[1]); 

std::ifstream keywords; 
keywords.open (argv[2]); 

std::ofstream outFile; 
outFile.open(argv[3]); 

    comparing(fileIn, keywords, outFile); 

fileIn.close(); 
keywords.close(); 
outFile.close(); 

return 0; 
} 

**的main.cpp文件永远不会需要一个头;为什么你需要在代码的其他地方拨打main