2016-09-21 48 views
0

所以我有一个make文件为散列图编译3个不同的文件。一个文件是哈希映射中的条目,一个文件是哈希映射本身,一个文件是我的主文件。我可以单独编译每个文件,但是当我尝试编译并链接主文件时,它说我对Hashtable类的引用是未定义的,尽管我在主文件中包含了该类的头文件。使文件无法链接C++,即使.o文件存在

这是我的make文件:

all: project1 

project1: 
    g++ -o project1 main.cpp HashTable.cpp HashEntry.cpp -g -Wall 

main.o: HashTable.o 
    g++ -c -Wall -g main.cpp 

HashTable.o: HashEntry.o 
    g++ -c -Wall -g HashTable.cpp 

HashEntry.o: 
    g++ -c -Wall -g HashEntry.cpp 

clean: 
    rm *.o 

我试图寻找周围的其他地方却没有看到,我还没有介绍的情况。代码中还有什么可能是错的?我已经尝试在project1命令中使用.o作为文件。

这是主要的班级代码。这是抱怨我引用的HashTable任何时间(线24,45,53,57说,有一个未定义的引用)

#include <vector> 
#include <iostream> 
#include <fstream> 
#include <string> 
#include <sstream> 
#include "HashTable.h" 

int main(int argc, char *argv[]){ 
    std::vector<std::string> keyList; 
    std::ifstream inputFile(argv[1]); 
    int numInputs = 0; 
    std::string line; 
    if(inputFile.is_open()){ 
     while(getline(inputFile,line)){ 
      std::stringstream currLine(line); 
      numInputs++; 
      std::string textCatch; 
      getline(currLine,textCatch,','); 
      keyList.push_back(textCatch); 
     } 
    } 
    inputFile.close(); 

    HashTable *dataTable = new HashTable(numInputs); 

    std::ifstream secondPass(argv[1]); 
    if(secondPass.is_open()){ 
     while(getline(secondPass,line)){ 
      std::stringstream currLine(line); 
      std::string field; 
      int value1; 
      int value2; 
      getline(currLine, field, ','); 
      std::string key = field; 
      getline(currLine, field, ','); 
      std::istringstream convert(field); 
      if(!(convert >> value1)){ 
       value1 = NULL; 
      } 
      getline(currLine, field); 
      std::istringstream convert2(field); 
      if(!(convert2 >> value2)){ 
       value2 = NULL; 
      } 
      dataTable->put(key,value1,value2); 
     } 
    } 
    secondPass.close(); 

    std::ofstream outputFile("output.dat"); 
    for(int i = 0; i < keyList.size(); i++){ 
     std::string key = keyList[i]; 
     double average = dataTable->getAverage(key); 
     std::ostringstream stringStream; 
     stringStream << average; 
     std::string avgString = stringStream.str(); 
     int max = dataTable->getMax(key); 
     stringStream << max; 
     std::string maxString = stringStream.str(); 
     outputFile << key + " | " + avgString + " | " + maxString; 
    } 
    outputFile.close(); 
    return 0; 
} 
+0

你的makefile显然坏了。每次只使用第一个目标,这将始终编译和链接来自源的三个cpp文件。这不是makefile的用处。但是,这可能不一定与你的问题有关。这个问题在每个相关文件的内容方面都没有[mcve]的回答。你需要编辑你的问题,并添加[mcve]。 –

+0

为什么明确使用Makefile而不是例如CMake在2016年? – iksemyonov

+0

由于我们的教授固执己见,所以需要使用makefile来完成我们的任务。另外,更新OP显示我的主类 – user3311613

回答

0

当我改变了从“HashTable.h”到“哈希表中的包含的问题是固定的。 CPP“

出于某种原因,我认为.h足以让链接器知道在另一个文件中找到方法的实现,但我想它不是(现在我想到了,它没有意义)。

+0

你的Makefile可能看起来像这样(http://pastebin.com/yQMsUHDy)。如果你只想使用*隐式规则*,它可能更短。一个很好的在线参考Gnu make [可以在这里找到](https://ftp.gnu.org/old-gnu/Manuals/make-3.79.1/html_chapter/make_toc.html)。祝你好运。 – WhozCraig

+0

这是一个非常糟糕的解决方案。与其修正你的makefile或正确学习使用链接器,你已经以一种抛弃了拥有目标文件的优势的方式来扼杀你的源文件。 – Beta