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.
Note using auto
in a function parameter, like your const auto& arr
, is not currently Standard C++. It will be valid in C++20, but some compilers already implement it for other C++ versions as an extension.
What it actually does, though, is makes the function into a function template, since the actual type of the parameter now needs to be deduced from the function arguments. So the declaration
std::string stringify(const auto& arr, const std::string& ch);
is essentially equivalent to
template <typename T>
std::string stringify(const T& arr, const std::string& ch);
Because your function acts like a template, its definition needs to be in a header file so that the compiler can instantiate it as needed whenever it is used with a new type of arr
. (See Why can templates only be implemented in the header file?)