这个头文件有什么问题
这是一个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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看来您返回了两个值:
void
和vector
。尝试删除函数开头的
void
。It seems you are returning two values,
void
andvector<std::string>
.Try to remove the
void
in the beginning of the functions.除了 @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.void std::vector; &
是错误的。您的意思是const std::vector; &
或只是std::vector; &
?void std::vector<std::string> &
is wrong. Did you meanconst std::vector<std::string> &
or juststd::vector<std::string> &
?