I'm just learning C++ and and have run into a problem that the Sublime Text console debugger can't solve for me. I have a stringify
function with two arguments, a const auto& arr
, and a const std::string& ch
. I'd like to be able to initialize this in my main.h file that I import in every single .cpp file for my project so it is available to the global scope.
I've tried the normal way of doing it, just defining it first in main.h and then filling out the rest within my main.cpp
main.cpp
std::string stringify(const auto& arr, const std::string& ch)
{
std::stringstream ss;
unsigned i = 0;
for (auto& element : arr) {
++i;
// add element to s
ss << element;
// if element isnt the last one.
(i != arr.size()) ? ss << ch : ss;
}
return ss.str();
}
main.h
#ifndef MAIN_H
#define MAIN_H
#include <iostream>
#include <string>
#include <sstream>
#include <array>
#include <vector>
#include <iterator>
#include <algorithm>
std::string stringify(const auto& arr, const std::string& ch);
#endif // MAIN_H
I constantly get errors for "undefined reference to function" stringify no matter what I try unless I put the full function definition within main.h. What am I missing here? I've been reading through docs and can't seem to figure it out.