C++ 辅助函数的多重定义

C++ Multiple definition of helper function

编辑:已回答——问题是因为函数具有相同的签名,尽管它们在不同的文件中,C++ 看到了两个版本并感到困惑。

我有三个 class:TableBed 都继承自 Furniture

TableBed 每个都有一个辅助函数 GetLowerCase(std::string) 在每个 class 中单独定义。当 make 是 运行(如下所示的 Makefile)时,我收到一条错误消息,指出 Bed.cpp 中的 GetLowerCase 首先在 Table.cpp

中定义

生成文件:

main: main.o Furniture.o Table.o Bed.o
        g++ main.o Furniture.o Table.o Bed.o -o main

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

Furniture.o: Furniture.cpp Furniture.h
        g++ -c Furniture.cpp

Table.o: Furniture.cpp Table.cpp Table.h
        g++ -c Table.cpp

Bed.o: Furniture.cpp Bed.cpp Bed.h
        g++ -c Bed.cpp

clean:
        rm *.o main

Table.cpp:

#include "Table.h"
#include <iostream>
#include <string>

std::string GetLowerCase(std::string str)
{
        std::string out;
        for (int i=0; i<str.length(); i++)
        {
                out[i] = tolower(str[i]);
        }
        return out;
}

Table::Table(const std::string n, std::string wt) : Furniture(n)
{
        wood_type = GetLowerCase(wt);
        if (wood_type != "pine" && wood_type != "oak")
        {
                std::cerr << "Wood type must be OAK or PINE.";
        }
}

void Table::Print()
{
        Furniture::Print();
        std::cout << "Wood Type: " << wood_type << std::endl;
}

Bed.cpp:

#include "Bed.h"
#include <iostream>
#include <string>

std::string GetLowerCase(std::string str)
{
        std::string out;
        for (int i=0; i<str.length(); i++)
        {
                out[i] = tolower(str[i]);
        }
        return out;
}

Bed::Bed(const std::string n, std::string sz) : Furniture(n)
{
        size = GetLowerCase(sz);
        if (size != "twin" && size != "full" && size != "queen" && size != "king")
        {
                std::cerr << "Bed size must be TWIN, FULL, QUEEN, or KING.";
        }
}

void Bed::Print()
{
        Furniture::Print();
        std::cout << "Size: " << size << std::endl;
}

我原以为 GetLowerCase 将完全包含在定义它的 .cpp 文件中,不会被任何其他文件 "seen" 包含。

除了上面列出的两个之外,它不在任何头文件或源文件中。非常困惑,希望得到一些帮助!

声明您的函数 static 或将其包装在匿名命名空间中:

namespace {
    // duplicated names here.
}

您的选择:

  1. 将您的所有辅助函数移至辅助函数 class(如 CommomUtils)作为 static 成员函数。我推荐这种方式,比较C++,可以避免重复代码

  2. 将您的函数声明为 static。所以它的范围将在定义它的文件中,而不是全局的。

  3. 用命名空间扭曲你的函数。

  4. 你的函数只定义一次,在你要使用的文件中用extern std::string GetLowerCase(std::string str)声明,然后就可以调用了。