I look this example and practice it:
https://novus.pixnet.net/blog/post/23784820
(The code is at the end)
The compile command is:
g++ sc.cc main.cc -o main.exe
I have 2 questions:
The Compilation Unit has 2 unit which consist of sc and main. (correct?)
I know this example (Schwarz Counter) is applied to iostream (C++).Singleton pattern looks suitable for this situation. Does Singleton pattern is better than Schwarz Counter in the case for unique and global (such as file io)?
Code:
sc.h
#include <fstream>
class Log {
friend class Initializer;
public:
void Print(const char* msg) {
std::fputs(msg, onlyFile_);
}
private:
static std::FILE* onlyFile_;
};
class Initializer {
public:
Initializer();
~Initializer();
private:
static int ref_;
};
extern Log log;
static Initializer init;
#endif
sc.cc
#include "s.h"
std::FILE* Log::onlyFile_ = 0;
int Initializer::ref_ = 0;
Initializer::Initializer() {
if (0 == ref_++) {
Log::onlyFile_ = fopen("somefile.txt", "w");
}
}
Initializer::~Initializer() {
if (0 == --ref_) {
fclose(Log::onlyFile_);
}
}
main.c
#include "s.h"
int main(int argc, char const *argv[]) {
log.Print("aa");
return 0;
}
Related
I receive an error that I cannot set delimiter without object. I am not sure where I went wrong.
main.cpp
using namespace sd;
int main() {
Utilities::setDelimiter(',');
return 0;
}
Utilities.h
namespace sd {
class Utilities {
static char m_delimiter;
public:
void setDelimiter(const char c) { m_delimiter = c; }
const char getDelimiter() const { return m_delimiter; }
}
}
Utilities.cpp
namespace sd {
char Utilities::m_delimiter = ',';
}
You must call a normal member function on a object instance:
Utilities util;
util.setDelimiter(',');
In your case, maybe changing it to a static method?
static void setDelimiter(....)
Static1.hpp
#include <string>
class Static1
{
public:
static const std::string my_string;
};
Static1.cpp
#include "Static1.hpp"
const std::string Static1::my_string = "aaa";
Static2.hpp
#include <string>
class Static2
{
public:
static const std::string my_string;
};
Static2.cpp
#include "Static2.hpp"
const std::string Static2::my_string = Static1::my_string;
main.cpp
#include "Static2.hpp"
#include <iostream>
int main(argc int, char** argv)
{
cout << to_string(Static2::my_string == "aaa") << endl;
return 0;
}
If I put add_executable(printMyString main.cpp Static2.cpp Static1.cpp) in my CMakeLists.txt, I get
0
while add_executable(printMyString main.cpp Static2.cpp Static1.cpp) gives me the expected behavior of
1
To make my code easier to maintain (so that I don't need to keep track of the order I list my source files), is there any way I can ensure that I get the behavior where Static2::my_string == "aaa"?
You are experiencing effects of a static initialization order fiasco.
The usual work-around is to substitute your static variables with functions that have a static variable in the scope, initialize, and return it.
Here is how it could be done for your example: Live Example (order1)
Live Example (order2)
class Static1
{
public:
static std::string my_string();
};
...
std::string Static1::my_string()
{
static const std::string my_string = "aaa";
return my_string;
}
...
class Static2
{
public:
static std::string my_string();
};
...
std::string Static2::my_string()
{
static const std::string my_string = Static1::my_string();
return my_string;
}
...
std::cout << std::to_string(Static2::my_string() == "aaa") << std::endl;
I have three files:
main.cpp
MyClass.cpp
MyClass.hpp
I have a library header file, "testLib.hpp", that I want to include in MyClass.hpp so that I can have one of testLib's objects be a class attribute.
I include MyClass.hpp in MyClass.cpp and in main.cpp. When attempting to compile the project, I get the following errors
MyClass.cpp multiple definition of 'testLib::testLib::function1()
obj/Release/main.o:main.cpp first defined here
MyClass.cpp multiple definition of 'testLib::testLib::function2()
obj/Release/main.o:main.cpp first defined here
and so on.
Both main.cpp and MyClass.cpp include MyClass.hpp (which includes testLib.hpp). Judging by the error, it looks like MyClass.cpp is attempting to include the library functions after they've already been included by main.cpp. However, I have include guards present in MyClass.hpp so I don't understand how it's trying to include MyClass.hpp twice.
Here's the code:
MyClass.hpp
#ifndef THIS_HEADER_H
#define THIS_HEADER_H
#include <stdint.h>
#include <iostream>
#include "testLib/testLib.hpp"
class MyClass
{
public:
void test();
int foo;
private:
uint32_t bar;
//I want to include an object from the library as part of this class
//TestLib::Device device;
};
#endif
MyClass.cpp
#include <stdio.h>
#include "MyClass.hpp"
void MyClass::test()
{
}
main.cpp
#include <iostream>
#include "MyClass.hpp"
using namespace std;
int main()
{
cout << "Hello world!" << endl;
return 0;
}
Any help would be greatly appreciated!
EDIT
I tried to hide the actual filenames to make the question more general and clear, but it seems like the problem might be resulting from 'testLib.hpp', which I did not write. That file is actually the following "sweep.hpp" file. I got the 'multiple definition of/first defined here' errors for each of the public functions in this file:
sweep.hpp
#ifndef SWEEP_DC649F4E94D3_HPP
#define SWEEP_DC649F4E94D3_HPP
/*
* C++ Wrapper around the low-level primitives.
* Automatically handles resource management.
*
* sweep::sweep - device to interact with
* sweep::scan - a full scan returned by the device
* sweep::sample - a single sample in a full scan
*
* On error sweep::device_error gets thrown.
*/
#include <cstdint>
#include <memory>
#include <stdexcept>
#include <vector>
#include <sweep/sweep.h>
namespace sweep {
// Error reporting
struct device_error final : std::runtime_error {
using base = std::runtime_error;
using base::base;
};
// Interface
struct sample {
const std::int32_t angle;
const std::int32_t distance;
const std::int32_t signal_strength;
};
struct scan {
std::vector<sample> samples;
};
class sweep {
public:
sweep(const char* port);
sweep(const char* port, std::int32_t bitrate);
void start_scanning();
void stop_scanning();
bool get_motor_ready();
std::int32_t get_motor_speed();
void set_motor_speed(std::int32_t speed);
std::int32_t get_sample_rate();
void set_sample_rate(std::int32_t speed);
scan get_scan();
void reset();
private:
std::unique_ptr<::sweep_device, decltype(&::sweep_device_destruct)> device;
};
// Implementation
namespace detail {
struct error_to_exception {
operator ::sweep_error_s*() { return &error; }
~error_to_exception() noexcept(false) {
if (error) {
device_error e{::sweep_error_message(error)};
::sweep_error_destruct(error);
throw e;
}
}
::sweep_error_s error = nullptr;
};
}
sweep::sweep(const char* port)
: device{::sweep_device_construct_simple(port, detail::error_to_exception{}), &::sweep_device_destruct} {}
sweep::sweep(const char* port, std::int32_t bitrate)
: device{::sweep_device_construct(port, bitrate, detail::error_to_exception{}), &::sweep_device_destruct} {}
void sweep::start_scanning() { ::sweep_device_start_scanning(device.get(), detail::error_to_exception{}); }
void sweep::stop_scanning() { ::sweep_device_stop_scanning(device.get(), detail::error_to_exception{}); }
bool sweep::get_motor_ready() { return ::sweep_device_get_motor_ready(device.get(), detail::error_to_exception{}); }
std::int32_t sweep::get_motor_speed() { return ::sweep_device_get_motor_speed(device.get(), detail::error_to_exception{}); }
void sweep::set_motor_speed(std::int32_t speed) {
::sweep_device_set_motor_speed(device.get(), speed, detail::error_to_exception{});
}
std::int32_t sweep::get_sample_rate() { return ::sweep_device_get_sample_rate(device.get(), detail::error_to_exception{}); }
void sweep::set_sample_rate(std::int32_t rate) {
::sweep_device_set_sample_rate(device.get(), rate, detail::error_to_exception{});
}
scan sweep::get_scan() {
using scan_owner = std::unique_ptr<::sweep_scan, decltype(&::sweep_scan_destruct)>;
scan_owner releasing_scan{::sweep_device_get_scan(device.get(), detail::error_to_exception{}), &::sweep_scan_destruct};
auto num_samples = ::sweep_scan_get_number_of_samples(releasing_scan.get());
scan result;
result.samples.reserve(num_samples);
for (std::int32_t n = 0; n < num_samples; ++n) {
auto angle = ::sweep_scan_get_angle(releasing_scan.get(), n);
auto distance = ::sweep_scan_get_distance(releasing_scan.get(), n);
auto signal = ::sweep_scan_get_signal_strength(releasing_scan.get(), n);
result.samples.push_back(sample{angle, distance, signal});
}
return result;
}
void sweep::reset() { ::sweep_device_reset(device.get(), detail::error_to_exception{}); }
} // ns
#endif
A simplified version of your problem:
buggy.hpp
int function() { return 0; }
main.cpp
#include "buggy.hpp"
int main() { return 0; }
other.cpp
#include "buggy.hpp"
The problem is that buggy.hpp is defining function, not just declaring. Once the header inclusion is expanded, that means function is declared in both main.cpp and other.cpp - and that is not allowed.
The fix is to declare function as inline which allows the function to be declared in multiple translation units.
inline int function() { return 0; }
In fact, allowing multiple definitions is the only meaning of inline to the C++ standard. Compilers may treat it as a hint that the function body may be expanded inline. Good ones won't; they are better at making that sort of decision that programmers).
I have the following code in file tested.cpp:
#include <iostream>
using namespace std;
class tested {
private:
int x;
public:
tested(int x_inp) {
x = x_inp;
}
int getValue() {
return x;
}
};
I also have another file (called testing.cpp):
#include <cppunit/extensions/HelperMacros.h>
#include "tested.cpp"
class TestTested : public CppUnit::TestFixture
{
CPPUNIT_TEST_SUITE(TestTested);
CPPUNIT_TEST(check_value);
CPPUNIT_TEST_SUITE_END();
public:
void check_value();
};
CPPUNIT_TEST_SUITE_REGISTRATION(TestTested);
void TestTested::check_value() {
tested t(3);
int expected_val = t.getValue();
CPPUNIT_ASSERT_EQUAL(7, expected_val);
}
When I try to compile the testing.cpp file I get: undefined reference tomain'`. Well, this is because I do not have main (the entry point for the program). So, the compiler does not know how to start the execution of the code.
But what is not clear to me is how to execute the code in the testing.cpp. I tried to add:
int main() {
TestTested t();
return 1;
}
However, it does not print anything (and it is expected to return an error message since 3 is not equal to 7).
Does anybody know what is the correct way to run the unit test?
Since you are writing a cppunit test, why not looking at cppunit doc ? (http://cppunit.sourceforge.net/doc/lastest/cppunit_cookbook.html)
It tells you that the main sould be written like this :
#include <cppunit/ui/text/TestRunner.h>
#include "ExampleTestCase.h"
#include "ComplexNumberTest.h"
int main( int argc, char **argv) {
CppUnit::TextUi::TestRunner runner;
runner.addTest( ExampleTestCase::suite() );
runner.addTest( ComplexNumberTest::suite() );
runner.run();
return 0;
}
... and save it into self-defined object type? I'm using PostgreSQL. When I have everything in one file, it works. But I wanted to split this into class-files like you always do when writing in cpp. When I divided my code into *.h and *.cpp files, I'm getting errors.
Here are my files:
test.h
class MyInt
{
public:
MyInt();
MyInt(int i);
void set(int i);
int get() const;
private:
int i_;
};
test.cpp
#include "test.h"
#include <soci.h>
#include <postgresql/soci-postgresql.h>
MyInt::MyInt()
{
}
MyInt::MyInt(int i)
{
this->i_ = i;
}
int MyInt::get() const
{
return this->i_;
}
void MyInt::set(int i)
{
this->i_ - i;
}
namespace soci
{
template <>
struct type_conversion<MyInt>
{
typedef int base_type;
static void from_base(int i, soci::indicator ind, MyInt & mi)
{
if (ind == soci::i_null)
{
throw soci_error("Null value not allowed for this type");
}
mi.set(i);
}
static void to_base(const MyInt & mi, int & i, soci::indicator & ind)
{
i = mi.get();
ind = soci::i_ok;
}
};
}
main.cpp
#include <iostream>
#include "test.h"
int main(int argc, char **argv)
{
MyInt i;
sql.open(soci::postgresql, "dbname=mydb user=postgres password=postgrespass");
sql << "SELECT count(*) FROM person;", soci::into(i);
std::cout << "We have " << i.get() << " persons in the database.\n";
sql.close();
return 0;
}
I compile it like this:
g++ main_test.cpp test.h test.cpp -o App -lsoci_core -lsoci_postgresql
-ldl -lpq -I /usr/local/include/soci -I /usr/include/postgresql
and got those errors:
In file included from /usr/local/include/soci/into-type.h:13:0,
from /usr/local/include/soci/blob-exchange.h:12,
from /usr/local/include/soci/soci.h:18,
from main_test.cpp:3:
/usr/local/include/soci/exchange-traits.h: In instantiation of â€soci::details::exchange_traits<MyInt>’:
/usr/local/include/soci/into.h:29:60: instantiated from â€soci::details::into_type_ptr soci::into(T&) [with T = MyInt, soci::details::into_type_ptr = soci::details::type_ptr<soci::details::into_type_base>]’
main_test.cpp:29:59: instantiated from here
/usr/local/include/soci/exchange-traits.h:35:5: error: incomplete type â€soci::details::exchange_traits<MyInt>’ used in nested name specifier
THE ABOVE PROBLEM IS SOLVED, TAKE A LOOK AT #JohnBandela ANSWER.
The code where you specialize type_conversion
template<>
struct type_conversion<MyInt>
Needs to be in test.h not test.cpp. The problem is if you have it in test.cpp like you do now, it is not visible in main.cpp where you are using SOCI