多重定义

时间:2015-07-03 01:48:01

标签: c++ 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;
} 

1 个答案:

答案 0 :(得分:4)

您在头文件中定义的函数(例如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)。