gpt4 book ai didi

c++ - 多重定义

转载 作者:行者123 更新时间:2023-11-27 22:57:32 27 4
gpt4 key购买 nike

我似乎遇到了链接器错误。它似乎是函数的多个定义,尽管我正在使用 header 防护,但它仍然会弹出。我不知道为什么,因为我以前写过这样的代码,没有任何错误。

我使用的命令:

clang++ -std=c++11 main.cpp -o main.o -Wall -pedantic -c
clang++ -std=c++11 main.o TextQuery.cpp -o TextQuery -Wall -pedantic

错误:

/tmp/TextQuery-94b8fe.o: In function `QueryResult::QueryResult(std::string, TextQuery)':
TextQuery.cpp:(.text+0x6b0): multiple definition of `QueryResult::QueryResult(std::string, TextQuery)'
/tmp/main-be50cb.o:main.cpp:(.text+0x6b0): first defined here
/tmp/TextQuery-94b8fe.o: In function `QueryResult::QueryResult(std::string, TextQuery)':
TextQuery.cpp:(.text+0x6b0): multiple definition of `QueryResult::QueryResult(std::string, TextQuery)'
/tmp/main-be50cb.o:main.cpp:(.text+0x6b0): first defined here
/tmp/TextQuery-94b8fe.o: In function `TextQuery::query(std::string const&)':
TextQuery.cpp:(.text+0x430): multiple definition of `TextQuery::query(std::string const&)'
/tmp/main-be50cb.o:main.cpp:(.text+0x430): first defined here
/tmp/TextQuery-94b8fe.o: In function `TextQuery::TextQuery(std::basic_ifstream<char, std::char_traits<char> >&)':
TextQuery.cpp:(.text+0x0): multiple definition of `TextQuery::TextQuery(std::basic_ifstream<char, std::char_traits<char> >&)'
/tmp/main-be50cb.o:main.cpp:(.text+0x0): first defined here
/tmp/TextQuery-94b8fe.o: In function `TextQuery::TextQuery(std::basic_ifstream<char, std::char_traits<char> >&)':
TextQuery.cpp:(.text+0x0): multiple definition of `TextQuery::TextQuery(std::basic_ifstream<char, std::char_traits<char> >&)'
/tmp/main-be50cb.o:main.cpp:(.text+0x0): first defined here
clang: error: linker command failed with exit code 1 (use -v to see invocation)

我可能犯了一个非常愚蠢的错误,有人可以告诉我他们如何将声明与定义分开。以及他们将如何使用头文件。谢谢!

TextQuery.h

#ifndef TEXT_QUERY_H
#define TEXT_QUERY_H

#include <ostream>
#include <fstream>
#include <sstream>
#include <memory>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <exception>
#include <algorithm>

class TextQuery;

// QueryResult class
class QueryResult
{
friend std::ostream &print(std::ostream &, QueryResult);
public:
QueryResult() = default;
QueryResult(const std::string, TextQuery);

private:
std::string word;

std::shared_ptr<std::vector<std::string>> lines;
std::shared_ptr<std::map<std::string, std::multiset<unsigned>>> lineNum;
};




/// TextQuery class
class TextQuery
{
friend class QueryResult;
public:
TextQuery() = default;
TextQuery(std::ifstream &);

QueryResult query(const std::string &);

private:
std::shared_ptr<std::vector<std::string>> lines = std::make_shared<std::vector<std::string>>();
std::shared_ptr<std::map<std::string, std::multiset<unsigned>>> lineNum
= std::make_shared<std::map<std::string, std::multiset<unsigned>>>();
};

// ifstream contructor
TextQuery::TextQuery(std::ifstream &iFile)
{
lines = std::make_shared<std::vector<std::string>>();
lineNum = std::make_shared<std::map<std::string, std::multiset<unsigned>>>();

if (iFile)
{
std::string l, w;

for (unsigned lineCnt = 1; getline(iFile, l); ++lineCnt)
{
lines->push_back(l);
std::istringstream wStream(l);

while (wStream >> w)
{
(*lineNum)[w].insert(lineCnt);
}
}
} else
throw std::runtime_error("Unable to open input file!");
}

// query member function
QueryResult TextQuery::query(const std::string &w)
{
return ((*lineNum).find(w) != (*lineNum).end()) ? QueryResult(w, *this) : QueryResult(w, TextQuery());
}



// TextQuery contructor for QueryResult
QueryResult::QueryResult(const std::string w, TextQuery tq) : word(w), lines(tq.lines), lineNum(tq.lineNum) { }


std::ostream &print(std::ostream &, QueryResult);

#endif

TextQuery.cpp

#include "TextQuery.h"

std::ostream &print(std::ostream &os, QueryResult qr)
{
os << qr.word << " occured " << (*qr.lineNum)[qr.word].size() << ((*qr.lineNum)[qr.word].size() == 1 ? " time" : " times");
unsigned n = 0;
for (const auto u : (*qr.lineNum)[qr.word])
{
if (u != n)
os << "\n(line " << u << ") " << (*qr.lines)[u-1];

n = u;
}

return os;
}

main.cpp

#include <iostream>
#include <fstream>
#include "TextQuery.h"

void runQueries(std::ifstream &infile)
{
TextQuery tq(infile);

while (true)
{
std::cout << "Enter word to look for, or q to quit: ";
std::string s;

if (!(std::cin >> s) || s == "q")
break;
std::cout << std::endl;
print(std::cout, tq.query(s)) << "\n" << std::endl;
}

}

int main()
{
std::ifstream infile("input.txt");
runQueries(infile);

return 0;
}

最佳答案

您在头文件中定义的函数(例如 TextQuery::TextQuery(std::ifstream &iFile) )正在编译到包含该头文件的每个翻译单元中 - 每个包含该头文件的 .cpp 文件都会生成该函数的拷贝,并在链接处是时候发生冲突了。

如果你想在头文件中包含函数定义,它们需要在实际的类定义中(例如,在本例中,在 class TextQuery {...} 中)。

例如,

class TextQuery
{
friend class QueryResult;
public:
TextQuery() = default;
TextQuery(std::ifstream &)
{
// .. function body here
}
};

或者,您可以将该函数显式标记为 inline ,或者简单地将函数的定义移动到 .cpp 文件(例如 TextQuery.cpp )。

关于c++ - 多重定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31197263/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com