简体   繁体   English

这个头文件怎么了

[英]What's wrong with this header file

This is a c++ function header file. 这是一个c ++函数头文件。 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. 它没有链接的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);
}

It seems you are returning two values, void and vector<std::string> . 似乎您正在返回两个值, voidvector<std::string> Try to remove the void in the beginning of the functions. 尝试删除函数开头的void

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 . 除了@Default的(正确和有效)观察之外,如果要将这些函数定义放在标头中,则几乎可以肯定地希望将它们标记为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. @You的建议是显而易见的替代方案:只需将类定义放在标头中,然后将函数定义放在它们自己的源文件中。

  1. void std::vector<std::string> & is wrong. void std::vector<std::string> &是错误的。 Did you mean const std::vector<std::string> & or just std::vector<std::string> & ? 您是说const std::vector<std::string> &还是只是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. 您应该在源文件中定义功能或将其内联。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM