c++模板专业化 - 链接器错误多个定义

发布于 2024-10-27 01:27:30 字数 2168 浏览 1 评论 0原文

我今天的第三个问题;-),但我对 C++ 模板编程和运算符重载真的很陌生。

我正在尝试以下操作:

terminallog.hh

//snipped code

class Terminallog {
public:

    Terminallog();
    Terminallog(int);
    virtual ~Terminallog();

    template <class T>
    Terminallog & operator<<(const T &v);
    template <class T>
    Terminallog & operator<<(const std::vector<T> &v);
    template <class T>
    Terminallog & operator<<(const T v[]);
    Terminallog & operator<<(const char v[]);

//snipped code
};

//snipped code
template <class T>
Terminallog &Terminallog::operator<<(const T &v) {
    std::cout << std::endl;
    this->indent();
    std::cout << v;
    return *this;
}

template <class T>
Terminallog &Terminallog::operator<<(const std::vector<T> &v) {
    for (unsigned int i = 0; i < v.size(); i++) {
        std::cout << std::endl;
        this->indent();
        std::cout << "Element " << i << ": " << v.at(i);
    }
    return *this;
}

template <class T>
Terminallog &Terminallog::operator<<(const T v[]) {
    unsigned int elements = sizeof (v) / sizeof (v[0]);
    for (unsigned int i = 0; i < elements; i++) {
        std::cout << std::endl;
        this->indent();
        std::cout << "Element " << i << ": " << v[i];
    }
    return *this;
}

Terminallog &Terminallog::operator<<(const char v[]) {
    std::cout << std::endl;
    this->indent();
    std::cout << v;
    return *this;
}
//snipped code

尝试编译此代码,它给了我一个链接器错误:

g++ src/terminallog.cc inc/terminallog.hh testmain.cpp -o test -Wall -Werror 
/tmp/ccifyOpr.o: In function `Terminallog::operator<<(char const*)':
testmain.cpp:(.text+0x0): multiple definition of `Terminallog::operator<<(char const*)'
/tmp/cccnEZlA.o:terminallog.cc:(.text+0x0): first defined here
collect2: ld returned 1 exit status

因此我目前正在用头撞墙,寻找解决方案。但我在墙上找不到一个...

如果有人能告诉我我的错误那就太好了

非常感谢

ftiaronsem

My third question here today ;-), but I am really new to c++ template programming and operator overloading.

I am trying the following:

terminallog.hh

//snipped code

class Terminallog {
public:

    Terminallog();
    Terminallog(int);
    virtual ~Terminallog();

    template <class T>
    Terminallog & operator<<(const T &v);
    template <class T>
    Terminallog & operator<<(const std::vector<T> &v);
    template <class T>
    Terminallog & operator<<(const T v[]);
    Terminallog & operator<<(const char v[]);

//snipped code
};

//snipped code
template <class T>
Terminallog &Terminallog::operator<<(const T &v) {
    std::cout << std::endl;
    this->indent();
    std::cout << v;
    return *this;
}

template <class T>
Terminallog &Terminallog::operator<<(const std::vector<T> &v) {
    for (unsigned int i = 0; i < v.size(); i++) {
        std::cout << std::endl;
        this->indent();
        std::cout << "Element " << i << ": " << v.at(i);
    }
    return *this;
}

template <class T>
Terminallog &Terminallog::operator<<(const T v[]) {
    unsigned int elements = sizeof (v) / sizeof (v[0]);
    for (unsigned int i = 0; i < elements; i++) {
        std::cout << std::endl;
        this->indent();
        std::cout << "Element " << i << ": " << v[i];
    }
    return *this;
}

Terminallog &Terminallog::operator<<(const char v[]) {
    std::cout << std::endl;
    this->indent();
    std::cout << v;
    return *this;
}
//snipped code

Trying to compile this code, it gives me a linker error:

g++ src/terminallog.cc inc/terminallog.hh testmain.cpp -o test -Wall -Werror 
/tmp/ccifyOpr.o: In function `Terminallog::operator<<(char const*)':
testmain.cpp:(.text+0x0): multiple definition of `Terminallog::operator<<(char const*)'
/tmp/cccnEZlA.o:terminallog.cc:(.text+0x0): first defined here
collect2: ld returned 1 exit status

Therfore I am currently banging my head against the wall, searching for a solution. But I can't find one in the wall...

It would be great if someone could show me my mistake

Thank you so much

ftiaronsem

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜光 2024-11-03 01:27:30

该函数

Terminallog &Terminallog::operator<<(const char v[]) {
    std::cout << std::endl;
    this->indent();
    std::cout << v;
    return *this;
}

不是模板,而是常规成员函数。如果此 .h 文件包含在多个 .cpp 过滤器中,则会导致您看到的多重定义错误。如果您声明它内联,编译器将允许多个定义,并且您的错误应该得到修复。

inline
Terminallog &Terminallog::operator<<(const char v[]) {

This function

Terminallog &Terminallog::operator<<(const char v[]) {
    std::cout << std::endl;
    this->indent();
    std::cout << v;
    return *this;
}

is not a template, but a regular member function. If this .h file is included in several .cpp filtes, it will cause the multiple definition error you're seeing. If you declare it inline, the compiler will allow the multiple definitions and your error should be fixed.

inline
Terminallog &Terminallog::operator<<(const char v[]) {
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文