Reputation: 51
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);
}
Upvotes: 0
Views: 243
Reputation: 490018
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.
Upvotes: 3
Reputation: 55524
void std::vector<std::string> &
is wrong. Did you mean const std::vector<std::string> &
or just std::vector<std::string> &
?Upvotes: 1
Reputation: 11635
It seems you are returning two values, void
and vector<std::string>
.
Try to remove the void
in the beginning of the functions.
Upvotes: 4