Linker error when using unordered_map but not map? - c++

This is a very strange issue. I have two classes: a custom console class (CConsole) and a test class (CHashTableTest) which I've made to play around with maps and unordered_maps to learn how they work.
In my console class I have a public static member variable of CConsole that exposes a static console object to the rest of the project so that I can write to this console when ever I want. This works fine for all of my classes including the test class but only when that test classes uses map and not unordered_map!
The error that I receive is:
error LNK2001: unresolved external symbol "public static class CConsole CConsole:output" (?output#CConsole##2V1#A)
It comes from the class that calls the methods on the test class and not the test class itself but nothing strange is happening in the calling class, it simply instantiates the CHashTableTest object (passing in a CConsole object) and calls Add and Get on it. It is placed in a separate project but this isn't a problem when I use map as the static member variable has been made external using _declspec(ddlexport).
The solution is setup in Visual Studio 2012, the CConsole and CHashTableTest classes are in a DLL project which is an external reference of a Unit Test project where the calling code exists.
The CConsole and CHashTableTest files:
Console.h
#ifndef _CONSOLE_H_
#define _CONSOLE_H_
#define _CRT_SECURE_NO_DEPRECATE
#include <iostream>
#include <fstream>
#include <string>
#include <ctime>
#include <time.h>
// Defines a set of output modes for the CConsole class. This will affect what output the console will target.
// The default is COUT.
enum _declspec(dllexport) EConsoleMode
{
// Output sent to a CConsole object will be printed using cout.
COUT,
// Output sent to a CConsole object will be printed to a file.
OUTPUT_FILE,
// Output sent to a CConsole object will be printed using OutputDebugString.
OUTPUT_DEBUG_STRING,
// Output sent to a CConsole object will be printed using Debug::WriteLine.
DEBUG_WRITE_LINE,
// Output sent to a CConsole object will be printed using Console::WriteLine.
CONSOLE_WRITE_LINE,
// Output sent to a CConsole object will not be printed.
NO_OUTPUT,
};
// An output wrapper class that allows logging and redirecting of log and debugging messages to different
// output targets.
class _declspec(dllexport) CConsole
{
public:
static CConsole output;
// Constructs a CConsole object with a specific console mode, default is COUT.
CConsole(EConsoleMode mode = COUT, const char* filePath = "C:/output.txt");
~CConsole(void);
// Gets the mode of this CConsole object.
EConsoleMode GetMode();
const char* GetFilePath();
// Sets the output file path of this CConsole object.
void SetFilePath(const char* filePath);
void TimeStamp();
// Logs a message with this specific CConsole object. An indirect call to an operator overload of
// CConsole << Type
template <typename T> void Log(const T &message);
protected:
// The mode of this CConsole object.
EConsoleMode m_mode;
// The file path of the output file for this CConsole object.
const char* m_filePath;
};
// Operator overload of CConsole << Type, queries the mode of the given CConsole object and
// selects the appropriate output method associated with the mode.
template <typename T>
CConsole operator<< (CConsole console, const T &input)
{
switch(console.GetMode())
{
case COUT:
{
std::cout << input << "\n";
break;
}
case OUTPUT_FILE:
{
ofstream fout;
fout.open (console.GetFilePath(), ios::app);
fout << input;
fout.close();
break;
}
#if ON_WINDOWS
case OUTPUT_DEBUG_STRING:
{
OutputDebugString(input);
break;
}
case DEBUG_WRITE_LINE:
{
Debug::WriteLine(input);
break;
}
case CONSOLE_WRITE_LINE:
{
Console::WriteLine(input);
break;
}
#endif
case NO_OUTPUT:
{
break;
}
default:
{
std::cout << input;
break;
}
}
return console;
}
// Logs a message by calling the operator overload of << on this CConsole object with the message
// parameter.
template <typename T>
void CConsole::Log(const T &message)
{
this << message;
}
#endif
Console.cpp
#include "Console.h"
CConsole CConsole::output = *new CConsole(OUTPUT_FILE, "C:/LocalProjects/---/output.txt"); // Known memory leak here, discussed in comments
// Constructs a CConsole object by assigning the mode parameter to the
// m_mode member variable.
CConsole::CConsole(EConsoleMode mode, const char* filePath)
{
m_mode = mode;
m_filePath = filePath;
TimeStamp();
}
CConsole::~CConsole(void)
{
//Log("\n\n");
}
// Returns the current mode of this CConsole object.
EConsoleMode CConsole::GetMode()
{
return m_mode;
}
const char* CConsole::GetFilePath()
{
return m_filePath;
}
void CConsole::TimeStamp()
{
if(m_mode == OUTPUT_FILE)
{
std::ofstream file;
file.open (m_filePath, std::ios::app); //
std::time_t currentTime = time(nullptr);
file << "Console started: " << std::asctime(std::localtime(&currentTime));
file.close();
}
}
void CConsole::SetFilePath(const char* filePath)
{
m_filePath = filePath;
}
HashTableTest.h
#ifndef _HASH_TABLE_TEST_H_
#define _HASH_TABLE_TEST_H_
#include <unordered_map>
#include <map>
#include <vector>
#include "Debuggable.h"
#include "Console.h"
using namespace std;
//template class __declspec(dllexport) unordered_map<int, double>;
struct Hasher
{
public:
size_t operator() (vector<int> const& key) const
{
return key[0];
}
};
struct EqualFn
{
public:
bool operator() (vector<int> const& t1, vector<int> const& t2) const
{
CConsole::output << "\t\tAdding, t1: " << t1[0] << ", t2: " << t2[0] << "\n"; // If I delete this line then no error!
return (t1[0] == t2[0]);
}
};
class __declspec(dllexport) CHashTableTest : public CDebuggable
{
public:
CHashTableTest(CConsole console = *new CConsole());
~CHashTableTest(void);
void Add(vector<int> key, bool value);
bool Get(vector<int> key);
private:
CConsole m_console;
unordered_map<vector<int>, bool, Hasher, EqualFn> m_table; // If I change this to a map then no error!
};
#endif
Just to be clear, if I change 'unordered_map' above to 'map' and remove the Hasher function object from the template argument list this will compile. If I don't I get the linker error. I haven't included HashTableTest.cpp because it contains practically nothing.
EDIT
I'm not sure if I'm using unordered_map properly here is the implementation for the CHashTableTest class:
HashTableTest.cpp
#include "HashTableTest.h"
CHashTableTest::CHashTableTest(CConsole console) : CDebuggable(console)
{
}
CHashTableTest::~CHashTableTest(void)
{
}
void CHashTableTest::Add(vector<int> key, bool value)
{
m_table[key] = value;
}
bool CHashTableTest::Get(vector<int> key)
{
return m_table[key];
}

When a DLL's generated import library is being linked with an consuming EXE (or another DLL) the declspecs of the items being brought in should be declspec(dllimport) on the receiver-side. When you're building the actual DLL those same items are made available by usage of declspec(dllexport). Your code has the latter, but not the former.
This is normally achieved by a single preprocessor macro that is defined only for your DLL project in the preprocessor section of the compiler configuration. For example in your header(s) that are declaring things exported from your DLL:
#ifndef MYDLL_HEADER_H
#ifdef MYDLL_EXPORTS
#define EXPORT __declspec(dllexport)
#else
#define EXPORT __declspec(dllimport)
#endif
class EXPORT CConsole
{
// stuff here.
};
#endif
Doing this will tell the compiler when building the DLL to export the class members, including class statics, because MYDLL_EXPORTS is defined in the preprocessor configuration flags for your DLL project.
When building a consuming project, do NOT define MYDLL_EXPORTSin the preprocessor configuration of the compiler settings. It will thusly use dllimport, rather than dllexport. That should square away the linker to know where to look for the things it needs.
Best of luck.

Related

creating own error handling mechanism in c++

I would like to create a class and enum to handle errors in my project. As of now I am doing it in the below way.
enum class eErrorType
{
eJsonFileNotFound = 0,
eJsonInvalidFormat,
eJsonKeyNotFound,
eJsonEmptyArray,
eNoError,
eCustom
};
class Error
{
public:
// Constructors
Error() { errorType = eErrorType::eNoError; message = ""; }
Error(eErrorType type) { errorType = type; SetMessage(type); }
Error(std::string msg) { errorType = eErrorType::eCustom; message = msg; }
// Public Methods
std::string getErrMessage() { return message; }
private:
eErrorType errorType;
std::string message;
void SetMessage(eErrorType type)
{
switch (type)
{
case eErrorType::eJsonFileNotFound: message = "Json file not found"; break;
case eErrorType::eJsonInvalidFormat: message = "Invalid json file"; break;
case eErrorType::eJsonKeyNotFound: message = "Specified key is not found in json"; break;
case eErrorType::eJsonEmptyArray: message = "No elements in json array"; break;
case eErrorType::eNoError: message = "Entry contained an attempt to divide by zero!"; break;
default: message = ""; break;
}
}
};
int main()
{
try
{
//open json file. If file open failed, throw error
throw eErrorType::eJsonFileNotFound;
//parse json file. If parsing failed, throw error
throw eErrorType::eJsonInvalidFormat;
//check for particular key in the json. If not found, throw error
throw eErrorType::eJsonKeyNotFound;
}
catch (eErrorType errCode)
{
Error errObj(errCode);
std::cout << errObj.getErrMessage() << std::endl;
}
return 0;
}
I would like some suggestions for improvements. Is there any better way of doing it or any language based features are available to achieve this.
For custom errors you can inherit from std::exception, override exception methods and implement your own stuff, example:
#include <exception> // std::exception
//
// custom exception class
//
class error final :
public std::exception
{
public:
error(const char* description, short code = -1) throw() :
description(description), code(code) { }
const char* what() const throw() override
{
return description;
}
short Code() const throw()
{
return code;
}
error(error&& ref)
: description(ref.description), code(ref.code) { }
error& operator=(error&&)
{
return *this;
}
error(const error& ref)
: description(ref.description), code(ref.code) { }
private:
const char* description;
const short code;
error& operator=(const error&) = delete;
};
Define a macro to show the filename where the error occured:
#include <cstring> // std::strrchr
// Show only file name instead of full path
#define __FILENAME__ (std::strrchr(__FILE__, '\\') ? std::strrchr(__FILE__, '\\') + 1 : __FILE__)
Define universal function to show the error (followin implemetation shows message box but you can redefine it for console program)
#include <string>
#include <windows.h>
#include <codecvt> // string conversion std::wstring_convert and std::codecvt_utf8
//
// converts string or const char* to wstring
//
std::wstring stringToWstring(const std::string& t_str)
{
//setup converter
typedef std::codecvt_utf8<wchar_t> convert_type;
std::wstring_convert<convert_type, wchar_t> converter;
//use converter (.to_bytes: wstr->str, .from_bytes: str->wstr)
return converter.from_bytes(t_str);
}
//
// Set error message to your liking using error class
// and show message box, this function is also used to pass in
// std::exception objects
//
template <typename ExceptionClass>
void ShowError(
HWND hWnd,
ExceptionClass exception,
const char* file,
int line,
long info = MB_ICONERROR)
{
std::string error_type = TEXT("Rutime Error");
std::string error_message = TEXT("File:\t");
#ifdef UNICODE
error_message.append(stringToWstring(file));
#else
error_message.append(file);
#endif // UNICODE
error_message.append(TEXT("\r\nLine:\t"));
error_message.append(std::to_string(line));
error_message.append(TEXT("\r\nError:\t"));
#ifdef UNICODE
error_message.append(stringToWstring(exception.what()));
#else
error_message.append(exception.what());
#endif // UNICODE
// Show error message
MessageBox(hWnd,
error_message.c_str(),
error_type.c_str(), static_cast<UINT>(MB_OK | info));
}
You then show the error like this:
ShowError(nullptr, error("You error message"), __FILENAME__, __LINE__);
For Win32/COM error types the function can be overloaded like this:
#include <comdef.h> // _com_error
#include <windows.h>
#include <string>
//
// Format error code into a string and show message box
// COM and System errors
//
void ShowError(HWND hWnd, const char* file, int line, HRESULT hr = S_OK)
{
string error_type = TEXT("Rutime Error");
string error_message = TEXT("File:\t");
#ifdef UNICODE
error_message.append(stringToWstring(file));
#else
error_message.append(file);
#endif // UNICODE
error_message.append(TEXT("\r\nLine:\t"));
error_message.append(std::to_string(line));
error_message.append(TEXT("\r\nError:\t"));
// If HRESULT is omited or S_OK
// format last error code message
if (hr == S_OK)
{
LPVOID lpBuff = nullptr;
DWORD dwChars = FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM,
nullptr,
GetLastError(),
0,
reinterpret_cast<LPTSTR>(&lpBuff),
0,
nullptr);
// If the function succeeds, the return value is
// the number of TCHARs stored in the output buffer
if (dwChars)
{
error_message.append(reinterpret_cast<LPCTSTR>(lpBuff));
}
else // If the function fails, the return value is zero
{
error_message.append(TEXT("Unknown Error\t"));
error_message.append(to_string(GetLastError()));
}
// Free the buffer allocated by FormatMessage
LocalFree(lpBuff);
}
else // Format com error code into a message
{
_com_error err(hr);
error_message.append(err.ErrorMessage());
}
// Play the sound and show error message
MessageBox(hWnd,
error_message.c_str(),
error_type.c_str(), MB_OK | MB_ICONERROR);
}
The function is called slight differently for system errors:
ShowError(nullptr, __FILENAME__, __LINE__); // type hresult if needed
edit:
I copied the code from my project, and currently std::to_string where mentioned works only for ANSI version, you need to modify ShowError function to conditionaly use std::to_wstring for unicode.
Also string inside ShowError function is ANSI string, you can conditionally use wstring or define a macro for string if you wan't like this:
#ifdef UNICODE
typedef std::wstring string;
#else
typedef std::string string;
#endif // UNICODE
also for to_string if you wish:
// conditionaly use string or wide string
#ifdef UNICODE
#define to_string std::to_wstring
#else
#define to_string std::to_string
#endif // UNICODE
You can also implement enum class code types and pass them to exception class as second or 3rd additional argument and implement showing the custom error code if you wish to avoid typing error message for every separate function call.
Also note that ShowError function can be used for std errors inside catch statement, where you simply pass std error object like this for example:
try
{
// example, or do some heavy memory allocation here to throw
throw std::bad_alloc;
}
catch(std::bad_alloc& err);
{
ShowError(nullptr, err, __FILENAME__, __LINE__);
}
This approach can be extended to modify the function to also format NTSTATUS messages
For complete list of possible error messages in Win32 see this.
For additional information on functions used in above code see following link:
FormatMessage function
GetLastError function
Some of the code has been copied from this site ex:
Convert to wstring
Show only file name
Format COM Error code
Lets start with the headers.
#include <system_error> // For std::error_code
#include <exception> // For std::exception
#include <iostream> // To print example output
Often you are stuck with some macros (could be constexpr int's)
#define FOO_ERROR1 1
#define FOO_ERROR2 2
You should be working in a namespace, lets use n here.
namespace n {
Step 1. define an enum type (you might already HAVE an enum, which is good).
enum class Error {
XE_FOO_ERROR1 = FOO_ERROR1, // XE_ is a random prefix to distinguish it from the macro FOO_ERROR1.
XE_FOO_ERROR2 = FOO_ERROR2
};
Step 2. define the following functions that will be found through ADL (Argument-dependent lookup). This is why the namespace (n) should be very specific for this error type. As an example, the namespace that I use for errors of my xcb system is xcb::errors::org::freedesktop::xcb. Just n is probably too short, and we should be using foo anyway, but I think it is less confusing for the example to use different names.
std::string to_string(Error code); // You probably want those enums to be convertible to strings with `to_string`.
std::ostream& operator<<(std::ostream& os, Error code); // In order to print the enum's to an ostream.
inline char const* get_domain(Error) { return "foo:org.freedesktop.foo.Error"; } // An error domain string.
std::error_code make_error_code(Error code) noexcept; // Converting the Error to a std::error_code.
} // namespace n
Step 3. register n::Error as valid error code.
namespace std {
template<> struct is_error_code_enum<n::Error> : true_type { };
} // namespace std
And yes, it is legal to define this in namespace std. It is what is intended by the standard.
Next, lets define the above functions in our .cpp file:
namespace n {
// Implement to_string
std::string to_string(Error code)
{
#if 0
switch(code)
{
case Error::XE_FOO_ERROR1:
return "FOO_ERROR1";
case Error::XE_FOO_ERROR2:
return "FOO_ERROR2";
}
return "Never reached";
#else
// Or if you use magic_enum (https://github.com/Neargye/magic_enum), we can use the simple:
auto sv = magic_enum::enum_name(code);
sv.remove_prefix(3); // Remove "XE_" from the name.
return std::string{sv};
#endif
}
// Implement operator<<
std::ostream& operator<<(std::ostream& os, Error code)
{
return os << to_string(code);
}
Before we can define make_error_code we first need to define the error category (this is still in that same .cpp file!):
namespace {
struct FooErrorCategory : std::error_category
{
char const* name() const noexcept override;
std::string message(int ev) const override;
};
char const* FooErrorCategory::name() const noexcept
{
return "n::Error"; // Can be anything you want.
}
std::string FooErrorCategory::message(int ev) const
{
auto error = static_cast<Error>(ev);
return to_string(error);
}
FooErrorCategory const theFooErrorCategory { };
} // namespace
And now we can define make_error_code.
std::error_code make_error_code(Error code) noexcept
{
return std::error_code(static_cast<int>(code), theFooErrorCategory);
}
} // namespace n
In order to support exceptions we need some exception class that supports std::error_code. This is a general class thus, not something specific to one n::Error enum. Personally I use a specialized system that you can find here, but lets just code something minimal for this example (this is the header file):
namespace errors {
struct ErrorCode : public std::exception
{
std::error_code m_ec;
char const* m_text;
ErrorCode(std::error_code ec, char const* text) : m_ec(ec), m_text(text) {}
void print_on(std::ostream& os) const;
};
inline std::ostream& operator<<(std::ostream& os, ErrorCode const& ec)
{
ec.print_on(os);
return os;
}
} // namespace errors
And the .cpp file
namespace errors {
void ErrorCode::print_on(std::ostream& os) const
{
os << m_text << ": " << m_ec.message() << " [" << m_ec << "]";
}
} // namespace errors
Finally, here is some code to test the above.
You can also find this complete example on wandbox.
int main()
{
// A test enum error code.
n::Error error = n::Error::XE_FOO_ERROR2;
// We want to be able to use to_string.
using std::to_string; // Fall back (that we are not using here).
std::cout << to_string(error) << std::endl;
// We want to be able to print an Error.
std::cout << error << std::endl;
// We want to be able to convert the error to a std::error_code.
std::error_code ec = error;
// We want to be able to print that error_code.
std::cout << ec << " [" << ec.message() << "]\n";
// You can convert a macro (or int) value to the enum type.
n::Error e1 = static_cast<n::Error>(FOO_ERROR1);
// We want to be able to throw 'Error' values.
try
{
throw errors::ErrorCode(e1, "Some text");
}
catch (errors::ErrorCode const& error)
{
std::cout << "Caught: " << error << std::endl;
}
}
Which outputs
FOO_ERROR2
FOO_ERROR2
n::Error:2 [FOO_ERROR2]
Caught: Some text: FOO_ERROR1 [n::Error:1]

Empty std::string in static method initialiser

I am using a static method to initialise the const fields of a class. The static method uses some const variables that are stored in a separate header file. Primitive types are correctly being passed to the static method, but the std::strings are being passed empty. I cannot understand why this is.
After doing some searching I have stumbled upon something called the static initialiser fiasco, but I'm having trouble wrapping my head around it, and can't work out if it is to blame. As the the object is in global scope, is the problem that it is being 'setup' before the std::string class has been 'setup'?
I have tried to replicate a minimal example below:
// File: settings.hpp
#include <string>
const std::string TERMINAL_STRING "Printing to the terminal";
const std::string FILE_STRING "Printing to a file";
// File: printer.hpp
#include <string>
#include <iostream>
class Printer
{
private:
const std::string welcomeMessage;
static std::string initWelcomeMessage(std::ostream&);
public:
Printer(std::ostream&);
}
extern Printer::print;
// File: printer.cpp
#include "settings.hpp"
std::string Printer::initWelcomeMessage(std::ostream &outStream)
{
if (&outStream == &std::cout)
{
return (TERMINAL_STRING);
}
else
{
return (FILE_STRING);
}
}
Printer::Printer(std::ostream &outStream) :
message(initWelcomeMessage(outStream)
{
outStream << welcomeMessage << std::endl;
return;
}
// File: main.cpp
#include "printer.hpp"
printer print(std::cout);
int main()
{
return (0);
}
Thanks very much!
As the the object is in global scope, is the problem that it is being 'setup' before the std::string class has been 'setup'?
Yes.
Have your strings be function-statics, returned by reference from some function, instead.
This is the traditional fix for the static initialisation order fiasco.

C++ DLL not exporting functions

I'm developing a C++ library for export as a DLL in Visual Studio 2013 (C++ 11), and I'm running into a challenge where the library, once imported as an external dependency by another program, contains the classes that I wanted, but none of the functions of the classes are included.
View of classes within external dependency
I have included one particular class that is supposed to be part of this DLL export.
Here is my RegressionTrainer class header file:
#ifndef REGRESSION_TRAINER_H
#define REGRESSION_TRAINER_H
#include "MachineLearning.h"
#pragma once
#ifndef DLL_EXPORT
#define DLL_EXPORT __declspec(dllexport)
#endif
using namespace MachineLearning;
namespace MachineLearningTraining{
public class RegressionTrainer{
public:
DLL_EXPORT RegressionTrainer();
virtual DLL_EXPORT ~RegressionTrainer();
std::vector<sample_type> DLL_EXPORT getInputData();
std::vector<double> DLL_EXPORT getAugmentedOutputs();
std::vector<double> DLL_EXPORT getNonAugmentedOutputs();
protected:
pugi::xml_parse_result DLL_EXPORT setDataFile(pugi::xml_document &doc, char* file_name);
void DLL_EXPORT setDataFolder(char* folder_name);
std::vector<char*> DLL_EXPORT findDataFiles();
char* data_folder;
std::vector<char*> file_names;
std::vector<sample_type> input_data;
/*
sample_type m;
m(0, 0) = 14.86;
m(1, 0) = 0.24;
*/
std::vector<double> augmented_outputs;
std::vector<double> non_augmented_outputs;
pugi::xml_parse_result result;
void DLL_EXPORT setInputData();
void DLL_EXPORT setAugmentedOutputs();
void DLL_EXPORT setNonAugmentedOutputs();
virtual int DLL_EXPORT trainAugmentedModel();
virtual int DLL_EXPORT trainNonAugmentedModel();
};
}
#endif
Here are the contents of MachineLearning.h:
#include <vector>
#include <iostream>
#include <exception>
#include <fstream>
#include <string>
#include <dlib/svm.h>
#include "pugixml.hpp"
namespace MachineLearning{
// Here we declare that our samples will be 2 dimensional column vectors.
typedef dlib::matrix<double, 3, 1> sample_type;
// Now we are making a typedef for the kind of kernel we want to use. I picked the
// radial basis kernel because it only has one parameter and generally gives good
// results without much fiddling.
typedef dlib::radial_basis_kernel<sample_type> kernel_type;
}
And here is my RegressionTrainer.cpp file:
#include "Stdafx.h"
#include "RegressionTrainer.h"
#include "dirent.h"
using namespace std;
using namespace dlib;
using namespace MachineLearning;
namespace MachineLearningTraining{
RegressionTrainer::RegressionTrainer(){
file_names = findDataFiles();
}
RegressionTrainer::~RegressionTrainer(){
}
pugi::xml_parse_result RegressionTrainer::setDataFile(pugi::xml_document &doc, char *file_name){
return doc.load_file(file_name);
}
void RegressionTrainer::setDataFolder(char *folder_name){
data_folder = folder_name;
}
std::vector<char*> RegressionTrainer::findDataFiles(){
DIR *dir;
struct dirent *ent;
std::vector<char*> file_names;
if ((dir = opendir(data_folder)) != NULL) {
/* print all the files and directories within directory */
while ((ent = readdir(dir)) != NULL) {
file_names.push_back(ent->d_name);
}
closedir(dir);
}
else {
/* could not open directory */
perror("Could not open directory");
}
return file_names;
}
std::vector<sample_type> RegressionTrainer::getInputData(){
return input_data;
}
std::vector<double> RegressionTrainer::getAugmentedOutputs(){
return augmented_outputs;
}
std::vector<double> RegressionTrainer::getNonAugmentedOutputs(){
return non_augmented_outputs;
}
void RegressionTrainer::setInputData(){
pugi::xml_document doc;
for (unsigned i = 0; i < file_names.size(); i++){
setDataFile(doc, file_names[i]);
std::cout << "Load result: " << result.description() << endl;
pugi::xml_node measures = doc.child("case").child("measures");
sample_type m;
int count = 0;
for (pugi::xml_node measure = measures.first_child(); measure; measure = measure.next_sibling()){
m(count, 0) = measure.text().as_double();
count++;
}
input_data.push_back(m);
}
}
void RegressionTrainer::setAugmentedOutputs(){
pugi::xml_document doc;
for (unsigned i = 0; i < file_names.size(); i++){
setDataFile(doc, file_names[i]);
std::cout << "Load result: " << result.description() << endl;
pugi::xml_node output = doc.child("case").child("studyresults").child("averageangledeviation");
augmented_outputs.push_back(output.text().as_double());
}
}
void RegressionTrainer::setNonAugmentedOutputs(){
pugi::xml_document doc;
for (unsigned i = 0; i < file_names.size(); i++){
setDataFile(doc, file_names[i]);
std::cout << "Load result: " << result.description() << endl;
pugi::xml_node output = doc.child("case").child("studyresults").child("averageangledeviationAR");
augmented_outputs.push_back(output.text().as_double());
}
}
int RegressionTrainer::trainAugmentedModel(){
return 0;
}
int RegressionTrainer::trainNonAugmentedModel(){
return 0;
}
}
Would welcome any thoughts!
Your code is confusing:
public class RegressionTrainer
Is this C++ or C#? Some other part of your code clearly shows it is C++. Therefore you must always put exact (or syntactically same) code.
Back to your problem, you cannot export members of a class. You must export entire class from a DLL.
Then issues start to begin. For one, you must expose (export or not, doesn't matter) all types the class uses (pugi::xml_parse_result for example). Then, you need to take care of different compilers versions (Even VC2015, various versions, debug/release, compiler settings etc). For example a vector on VC2015 debug build would be different from a release version. Service pack would complicate the problem.
In short: Don't export entire class having data-members. Even if entire data is private, you will need to export entire class, so that client (consumer of DLL) can compile and link (to code) properly.
So, what's the solution?
Well, just export a helper class:
class DLL_EXPORT RegressionTrainerHelper
{
RegressionTrainer* pCoreClass;
};
Expose (export) all required methods (for the client) from this class. You'd just need to forward the calls from helper to real class.
Now, you might wonder, you will need to export the underlying type RegressionTrainer, and you are back to same problem.
Well, yes and no. If this helper class is being compiled within DLL, RegressionTrainer would be real thing. If not, just:
typedef int RegressionTrainer;
Pointer of anytype is of same size (32-bit or 64-bit). Hence the size of entire exported class would always match the size in DLL and in EXE.
EDIT
For example, there is a XML parser class, ParseXML, but it uses comlpex data members, you have a method Parse.
class ParseXML
{
// complex data members
// some private OR public datamembers and functions, you dont want/need to export
public:
void Parse(const char*); // or something different
};
You would want to export only Parse, through helper class.
class DLL_EXPORT Exported_ParseXML
{
private:
ParseXML* pCoreXMLInstance;
public:
Exported_ParseXML()
{
// implementation in CPP
pCoreXMLInstance = new ParseXML();
}
// The function!
void Parse(const char* pData)
{
// FORWARD
pCoreXMLInstance->Parse(pData);
}
The client will simply use the exported class:
Exported_ParseXML parser;
parser.Parse("XML-data");
The server compiler (DLL) would see ParseXML as real class type. But the client (EXE, or other DLL), would need see ParseXML as int. You got to figure out this!

Make a wrapper for cout?

So here's an interesting question, How would I make something kinda like a wrapper for cout?
I want to be able to add it into a dll so I can throw it into my programs. but the basic syntax of it should be
Mything::mesage << "I'm some text" << im_an_int << someclass << mything::endl;
or
Mything::mesageandlog << "I'm going to print to console, and to a file!" << mything::endl;
I can handle most of the internal logic but as to what I should put to even do this. kinda stumped.
Possibly make a static stream member in my class called message, then have an event fire when its written too that runs it through a method?
Idk, I looked around and found something sortA similar, but as for throwing it into a dll I'm at a loss. (How to write a function wrapper for cout that allows for expressive syntax?)
because this requires me to use extern and a variable, but how would I make it static so I can just straight call it without creating a variable?
Bit of clarification, something like this:
mydll.h
#include <iostream>
namespace mynamespace {
extern struct LogMessage{};
template <typename T>
LogMessage& operator<< (LogMessage &s, const T &x) {
SetStdHandle(STD_OUTPUT_HANDLE, GetStdHandle(STD_OUTPUT_HANDLE));
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_BLUE);
std::cout << "[IF] ";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_WHITE);
//LogTimestamp(); --ill impliment this.
std::cout << x << endl;
//writeStreamToLogfile(s); --and ill handle this.
return s;
}
}
driverprogram.h
#include <mydll.h>
#include <iostream>
int _tmain(int argc, _TCHAR* argv[])
{
mynamespace::LogMessage << "Something: << std::endl;
}
expected output:
"[IF] [00:00:00] Something
You can create a struct, that has a << operator
struct OutputThing
{
template< class T >
OutputThing &operator<<( T val )
{
std::cout<<val;
return *this;
}
};
Now whenever you want to log, you will have to instance the object.
OutputThing()<<"x ="<<x;
If you want to avoid the repeated construction and destruction of the object, you can make it a singleton.
struct OutputThingSingleton
{
static OutputThingSingleton& GetThing()
{
static OutputThingSingleton OutputThing;
return OutputThing;
}
template< class T >
OutputThingSingleton &operator<<( T val )
{
std::cout<<val;
return *this;
}
private:
OutputThingSingleton()
{};
};
So the call now looks like
OutputThingSingleton::GetThing()<<"x ="<<x;
Which you could shorten using a macro.
This will work across multiple dlls, however depending on how it is used you can have multiple instances of the singleton existing. This would work fine as long as you don't want to maintain any state in your singleton. If you do need to ensure a single instance, you can compile it in its own dll. Any other binary that uses this dll will share the single instance 'owned' by the dll.
First of all, just to give fair warning, I'm pretty sure this won't work in a DLL. You want to put it into a header (as it's shown here).
Second, it's probably a little more elaborate than you were considering. In particular, it defines a multi-output stream class that works like any other stream. Essentially any normal overload of operator<< should work fine with it.
Unlike a normal stream operator, however, the output goes to multiple streams, and each line of output (on all the streams) is preceded by a prefix (currently set to the value "[FIX]", but it just uses the content of a string, so whatever you put in that string should work. A more polished/finished implementation would probably allow you to set the prefix with something like a manipulator, but this (currently) doesn't support that.
Finally, it does some variadic template trickery, so you can specify the output files as either file names or existing ostream objects, or a combination thereof (e.g., see demo main at end).
First, the header:
#ifndef LOGGER_H_INC_
#define LOGGER_H_INC_
#include <iostream>
#include <streambuf>
#include <vector>
#include <fstream>
class logger: public std::streambuf {
public:
logger(std::streambuf* s): sbuf(s) {}
~logger() { overflow('\n'); }
private:
typedef std::basic_string<char_type> string;
int_type overflow(int_type c) {
if (traits_type::eq_int_type(traits_type::eof(), c))
return traits_type::not_eof(c);
switch (c) {
case '\n':
case '\r': {
prefix = "[FIX]";
buffer += c;
if (buffer.size() > 1)
sbuf->sputn(prefix.c_str(), prefix.size());
int_type rc = sbuf->sputn(buffer.c_str(), buffer.size());
buffer.clear();
return rc;
}
default:
buffer += c;
return c;
}
}
std::string prefix;
std::streambuf* sbuf;
string buffer;
};
namespace multi {
class buf : public std::streambuf {
std::vector<std::streambuf *> buffers;
public:
typedef std::char_traits<char> traits_type;
typedef traits_type::int_type int_type;
buf() {}
void attach(std::streambuf *s) { buffers.push_back(s); }
void attach(std::ofstream &s) { buffers.push_back(s.rdbuf()); }
int_type overflow(int_type c) {
bool eof = false;
for (std::streambuf *buf : buffers)
eof |= (buf->sputc(c) == traits_type::eof());
return eof ? traits_type::eof() : c;
}
};
class logstream : public std::ostream {
std::vector<std::ofstream *> streams;
buf outputs;
logger log;
void attach(std::ostream &s) { outputs.attach(s.rdbuf()); }
void attach(char const *name) {
std::ofstream *s = new std::ofstream(name);
streams.push_back(s);
outputs.attach(s->rdbuf());
}
template <typename T, typename...pack>
void attach(T &t, pack&...p) {
attach(t);
attach(p...);
}
public:
template <typename...pack>
logstream(pack&...p) : log(&outputs), std::ostream(&log) { attach(p...); }
~logstream() {
for (auto d : streams) {
d->close();
// Bug: crashes with g++ if delete is allowed to execute.
//delete d;
}
}
};
}
#endif
Then the demo of how to use it:
#include "logger"
int main(){
multi::logstream l(std::cout, "c:/path/log.txt");
l << "This is a prefixed string\n";
}
Obviously the header is fairly large, but the code to use it seems (at least to me) about as simple as you can hope for -- create an object, specifying where you want the output to go, just a normal stream -- except that you can specify more than one. Then write to it like you would to any other stream, and the output goes to all of the specified outputs, with each line preceded by the specified prefix.

Proper way to create ostream to file or cout

I am trying to create a Log class for my project at school. It needs to either be able to write information to the stdout or to a file depending on the parameters it is passed. I was looking into how to do this and I stumbled upon a thread with a similar question here: Obtain a std::ostream either from std::cout or std::ofstream(file)
The only difference between this thread and my own is that I want to do it inside of a class. Looking at the solution though they use std::ostream out(buf) and construct the ostream on the fly with buf. How can i declare this properly in my Log class to be able to construct the "out" object only once i enter my Log constructor?
I took a quick stab at it below but I am not sure if it is correct or if I am on the right track. Appreciate any help, thanks.
EDIT: I want to be able to do out << "Some string" << endl; after i get this Log class working properly.
EDIT2: An error I am now receiving with the new code below error : 'std::basic_ostream<_CharT, _Traits>::basic_ostream() [with _CharT = char, _Traits = std::char_traits<char>]' is protected
// log.h
#include <string>
#include <fstream>
#ifndef LOG_H_
#define LOG_H_
class Log
{
public:
enum Mode { STDOUT, FILE };
// Needed by default
Log(const char *file = NULL);
~Log();
// Writing methods
void write(char *);
void write(std::string);
private:
Mode mode;
std::streambuf *buf;
std::ofstream of;
std::ostream out;
};
#endif
// log.cpp
#include "log.h"
#include <iostream>
#include <stdlib.h>
#include <time.h>
Log::Log(const char *file)
{
if (file != NULL)
{
of.open(file);
buf = of.rdbuf();
mode = FILE;
}
else
{
buf = std::cout.rdbuf();
mode = STDOUT;
}
// Attach to out
out.rdbuf(buf);
}
Log::~Log()
{
if (mode == FILE)
of.close();
}
void Log::write(std::string s)
{
out << s << std::endl;
}
void Log::write(char *s)
{
out << s << std::endl;
}
You create tmp with std::ostream tmp(buf); and store the address of it in out with this->out = &tmp;. However, tmp will go out of scope at the end of the constructor and the pointer will no longer be pointing at a valid object.
What you should do instead is make out not a std::ostream* but simply a std::ostream:
std::ostream out;
Then in your constructor, once you've got the buf ready, you can give it to out by doing out.rdbuf(buf);.
Response to edit:
The std::ostream doesn't have a default constructor - it has to take a buffer pointer. My mistake. However, the fix is simple. Use your constructor's member initialization list to pass a null pointer (nullptr in C++11, 0 or NULL in C++03):
Log::Log(const char *file)
: out(nullptr)
{
// ...
}