因此,我正在尝试开始一项任务,我的教授给了我们Main.cpp,Main.h,Scanner.cpp,Scanner.h和其他一些实用程序。

我的工作是创建一个相似度类,以使用余弦和Jaccard系数比较文档。但是,我似乎无法正确链接项目,因此无法启动实际的代码。

经过几个小时的尝试,我发现自己在做错事,但我需要重新审视自己的所作所为,这很明显。

这是Main.cpp

#include "Main.h"

using namespace std;

static const string TAG = "Main: ";

int main(int argc, char *argv[])
{
  string inStreamName;
  string logStreamName;
  string outStreamName;

  ofstream outStream;
  string timeCallOutput;
  Scanner inStream;

  Similarity similarity;

  ///////////////////////////////////////////////////////////////
  // Boilerplate for naming files and opening files
  Utils::CheckArgs(3, argc, argv, "infilename outfilename logfilename");
  outStreamName = static_cast<string>(argv[2]);
  logStreamName = static_cast<string>(argv[3]);

  Utils::FileOpen(outStream, outStreamName);
  Utils::LogFileOpen(logStreamName);

  timeCallOutput = Utils::timecall("beginning");
  Utils::logStream << timeCallOutput << endl;
  Utils::logStream << TAG << "Beginning execution" << endl;

  Utils::logStream << TAG << "outfile '" << outStreamName << "'" << endl;
  Utils::logStream << TAG << "logfile '" << logStreamName << "'" << endl;
  Utils::logStream.flush();

  ///////////////////////////////////////////////////////////////
  // What follows here is the real work of this code.
  //   read the entire input file and echo it back
  //   compute the two similarity coefficients

  inStreamName = static_cast<string>(argv[1]);
  inStream.openFile(inStreamName);
  Utils::logStream << TAG << "infile '" << inStreamName << "'" << endl;
  Utils::logStream.flush();

  similarity.readData(inStream);

  outStream << TAG << "Data Begin\n" << similarity.toString() << endl;
  outStream << TAG << "Data End\n" << endl;
  outStream.flush();
  inStream.close();

  outStream << TAG << "Begin similarity computation" << endl;
  outStream << TAG << "Maximum Jaccard similarity:\n" <<
                       similarity.maxJaccard() << endl;
  outStream << TAG << "Maximum cosine similarity:\n" <<
                       similarity.maxCosine() << endl;
  outStream << TAG << "End similarity computation" << endl;
  outStream.flush();

  ///////////////////////////////////////////////////////////////
  // Boilerplate for terminating gracefully
  Utils::logStream << TAG << "Ending execution" << endl;
  timeCallOutput = Utils::timecall("ending");
  Utils::logStream << timeCallOutput << endl;
  Utils::logStream.flush();

  outStream.flush();
  Utils::FileClose(outStream);

  Utils::FileClose(Utils::logStream);

  return 0;
}

和Main.h
#ifndef MAIN_H
#define MAIN_H

#include "../../Utilities/Utils.h"
#include "../../Utilities/Scanner.h"

#include "Similarity.h"

class Main
{
public:
  int main();
  virtual ~Main();

private:

};

#endif // MAIN_H

我的相似点
#include "Similarity.h"

using namespace std;

void readData(Scanner& inStream){
}

string maxCosine(){
    return "cosine";
}

string maxJaccard(){
    return "jaccard";
}

string toString(){
    return "toString";
}

最后是我的相似性:
#ifndef SIMILARITY_H
#define SIMILARITY_H

#include "../../Utilities/Scanner.h"

class Similarity
{
public:
    Similarity();
    virtual ~Similarity();

    void readData(Scanner& inStream);
    string maxCosine();
    string maxJaccard();
    string toString();
private:

};

#endif

当我使用他提供的makefile以及我必须使用的makefile才能对其脚本进行评分时,出现此错误:
g++ -O3 -Wall -o Similarity.o -c Similarity.cpp
g++ -O3 -Wall -o Aprog Main.o Similarity.o Scanner.o ScanLine.o Utils.o
Undefined symbols for architecture x86_64:
  "Similarity::maxJaccard()", referenced from:
      _main in Main.o
  "Similarity::readData(Scanner&)", referenced from:
      _main in Main.o
  "Similarity::toString()", referenced from:
      _main in Main.o
  "Similarity::maxCosine()", referenced from:
      _main in Main.o
  "Similarity::Similarity()", referenced from:
      _main in Main.o
  "Similarity::~Similarity()", referenced from:
      _main in Main.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
make: *** [Aprog] Error 1

感谢您通读所有内容,任何建议或解决方案将不胜感激。

最佳答案

这里没有什么神秘之处,链接器告诉您尚未定义缺少的符号,而您尚未定义。

只是缺少Similarity::Similarity()Similarity::~Similarity(),而您定义了其他错误的代码,

void Similarity::readData(Scanner& inStream){
}


void readData(Scanner& inStream){
}

等等等

第二个是称为readData的函数,只有第一个是相似类的readData方法。

要明确这一点,请在Sameity.h中
void readData(Scanner& inStream);

但在相似性
void Similarity::readData(Scanner& inStream){
}

关于c++ - 架构x86_64的 undefined symbol : Compiling problems,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18751868/

10-11 17:11