这个头文件有什么问题

发布于 2024-10-02 00:17:25 字数 866 浏览 5 评论 0原文

这是一个c++函数头文件。它会产生大量随机错误。我知道这很明显,但我以前从未只制作过没有类的头文件。它没有链接 cpp 文件。

#include <vector>
#include <sstream>
#include <string>

#ifndef SPLIT_H
#define SPLIT_H
 void std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems);
 void std::vector<std::string> split(const std::string &s, char delim);
#endif

void std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems) {
    std::stringstream ss(s);
    std::string item;
    while(std::getline(ss, item, delim)) {
        elems.push_back(item);
    }
    return elems;
}


void std::vector<std::string> split(const std::string &s, char delim) {
    std::vector<std::string> elems;
    return split(s, delim, elems);
}

This is a c++ function header file. It gives loads of random errors. I know it will be obvious, but I have never made only header file with no class before. It has no linking cpp file.

#include <vector>
#include <sstream>
#include <string>

#ifndef SPLIT_H
#define SPLIT_H
 void std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems);
 void std::vector<std::string> split(const std::string &s, char delim);
#endif

void std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems) {
    std::stringstream ss(s);
    std::string item;
    while(std::getline(ss, item, delim)) {
        elems.push_back(item);
    }
    return elems;
}


void std::vector<std::string> split(const std::string &s, char delim) {
    std::vector<std::string> elems;
    return split(s, delim, elems);
}

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

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

发布评论

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

评论(3

傲鸠 2024-10-09 00:17:25

看来您返回了两个值:voidvector
尝试删除函数开头的 void

It seems you are returning two values, void and vector<std::string>.
Try to remove the void in the beginning of the functions.

入怼 2024-10-09 00:17:25

除了 @Default 的(正确且有效)观察之外,如果您要将这些函数定义放入标头中,您几乎肯定会希望将它们标记为内联。否则,当/如果您将标头包含在多个源文件中并尝试将它们链接在一起时,您将违反单一定义规则。您的链接器可能会允许这样做,但当然不能保证。 @You的建议是明显的替代方案:只需将类定义放在标头中,并将函数定义放在它们自己的源文件中。

In addition to @Default's (correct and valid) observation, if you're going to put these function definitions in a header, you'll almost certainly want to mark them inline. Otherwise, when/if you include the header in more than one source file and try to link them together, you'll be violating the one definition rule. It's possible your linker will allow it, but there's certainly no guarantee. @You's advice is the obvious alternative: just put the class definition in the header, and put the function definitions in a source file of their own.

假面具 2024-10-09 00:17:25
  1. void std::vector; & 是错误的。您的意思是 const std::vector; & 或只是 std::vector; &
  2. 如果您在超过 1 个源文件中包含此标头,您可能会收到有关重新定义符号的链接器错误。您应该在源文件中定义函数或将它们内联。
  1. void std::vector<std::string> & is wrong. Did you mean const std::vector<std::string> & or just std::vector<std::string> &?
  2. If you include this header in more than 1 source file you may get linker errors about symbols being redefined. You should either define the functions in the source files or make them inline.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文