开发者

What's wrong with this header file

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);
}


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


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.


  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.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜