Clarifications on the discussion in Accelerated C++ header files - c++

I am reading Accelerated C++ Chapter 4: Organizing Programs and Data. I came across the following piece of code, which is saved to a file called median.cpp:
// median.cpp file contents
#include <algorithm>
#include <stdexcept>
#include <vector>
using std::domain_error;
using std::sort;
using std::vector;
// Compute the median of a vector<double>
double median(vector<double> vec)
{
// Code for what the median function does goes here
}
and then, later on on page 67., the authors discuss an include guard like below, which is saved to a file called median.h.
#ifndef GUARD_median_h
#define GUARD_median_h
// median.h - final version
#include<vector>
double median(std::vector<double>);
//
#endif
Are we supposed to "include" the median.h into median.cpp before compiling, like below?
// median.cpp file contents
#include "median.h"
#include <algorithm>
#include <stdexcept>
#include <vector>
using std::domain_error;
using std::sort;
using std::vector;
// Compute the median of a vector<double>
double median(vector<double> vec)
{
// Code for what the median function does goes here
}
I am asking this because, in section 4.3 where the authors discuss this topic, they have failed to mention how someone is supposed to make use of median.cpp and median.h for compilation.

Related

int Method in C++ Class "Requies and Identifier"

I am attempting to make a class to contain some math operations from a CRC math tables handbook I have, in creating one of the functions I got a strange error I had not seem before. The code for both the cpp and the header are below:
//Header File
#include <iostream>
#include <cmath>
#include <string>
#define int "CRCMathLib_H"
using namespace std;
class CRCMathLib
{
public:
int DoReturn_Totient(int Toter); //Error comes from here when trying to declare as an int
};
//CPP Class File
#include "CRCMathLib.h"
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
int CRCMathLib::DoReturn_Totient(int Toter)
{
return 0;
}
//CPP Main File
#include <iostream>
#include <cmath>
#include <string>
#include "CRCMathLib.h"
using namespace std;
int main()
{
return 0;
}
The Main file does not do anything as of yet as this is a completely new file for these operations, I believe this may be a preprocessing error and its not picking up on the int statement as I ran it on another PC with VS and it was able to read the statement. anything would help. Also it was requesting a decleration of the header file, so thats why I placed the int there, is this possibly the issue? removing it returns the error of not having a decleration.
In your .h remove #define int "CRCMathLib_H" which is most probably a typo
replace it by
#include <iostream>
#include <cmath>
#include <string>
#pragma once
The #pragma once ensure you can safely include your .h from the cpp implementation file and the main.cpp
You mis understood include guard protection usually done by
ifndef CRCMathLib_H
#define CRCMathLib_H
// all of you .h file delcaration
#endif
This can be easily replace by the #pragma once statement at the begining of the file
More on this here: https://www.learncpp.com/cpp-tutorial/header-guards/

error: ‘GlobalRNG’ was not declared in this scope

I'm using Crypto++ to encrypt files in C++. And I'm using the code below.
It doesn't contain the headers files so I added my own :
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <fstream>
#include <cryptopp/cryptlib.h>
#include <cryptopp/sha.h>
#include <cryptopp/secblock.h>
#include <cryptopp/files.h>
#include <cryptopp/queue.h>
#include <cryptopp/hex.h>
#include <cryptopp/base64.h>
#include <cryptopp/filters.h>
#include <cryptopp/osrng.h>
#include <cryptopp/integer.h>
#include <cryptopp/dh.h>
#include <cryptopp/sha.h>
#include <cryptopp/modes.h>
#include <cryptopp/eax.h>
#include <cryptopp/tea.h>
#include <cryptopp/blowfish.h>
#include <cryptopp/pssr.h>
#include <cryptopp/rsa.h>
#include <cryptopp/nbtheory.h>
#include <cryptopp/eccrypto.h>
#include <cryptopp/oids.h>
#include <cryptopp/modes.h>
#include <cryptopp/gzip.h>
#include <cryptopp/blowfish.h>
#include <cryptopp/rsa.h>
#include <cryptopp/rng.h>
#include <cryptopp/cryptlib.h>
#include <cryptopp/filters.h>
#include <cryptopp/rdrand.h>
using namespace std;
using namespace CryptoPP;
But unfortunately the code doesn't work
Saying that the GlobalRNG is not declared !
error: ‘GlobalRNG’ was not declared in this scope
I googled and kept looking for a solution for 2 days i found that it's a bug and fixed but i'm having the latest version : 5.6.3 !
So i really don't know why this error is showing !
In the version 5.6.3 GlobalRNG is defined in the file validate.h, as:
// Functions that need a RNG; uses AES inf CFB mode with Seed.
CryptoPP::RandomNumberGenerator & GlobalRNG();
Just add this inclusion:
#include <cryptopp/validate.h>
to solve definition problem.
GloablaRNG is part of testing and bench-marking. It should not be part of the library proper (i.e., libcryptopp.a or libcryptopp.so). If your programs are complaining about a missing GloablaRNG, then the library was cross-contaminated with some of the testing and bench-marking gear.
These are the files used for testing and bench-marking. They should not be included in your build of the library or your project:
validate.h
bench.h
test.cpp
bench1.cpp, bench2.cpp
validat0.cpp, validat1.cpp, validat2.cpp, validat3.cpp
datatest.cpp, regtest.cpp, fipsalgt.cpp, dlltest.cpp
You are free to use a function called GlobalRNG(). Here's how its used in the library's test and bench-marking gear. But you might consider using an AutoSeededRandomPool instead. The AutoSeededRandomPool is a PGP-style generator, and its seeded from /dev/urandom, /dev/srandom, /dev/random or the Windows entropy pool.
Declaration in validate.h
NAMESPACE_BEGIN(CryptoPP)
NAMESPACE_BEGIN(Test)
CryptoPP::RandomNumberGenerator & GlobalRNG();
NAMESPACE_END // Test
NAMESPACE_END // CryptoPP
Definition in test.cpp
NAMESPACE_BEGIN(CryptoPP)
NAMESPACE_BEGIN(Test)
ANONYMOUS_NAMESPACE_BEGIN
OFB_Mode<AES>::Encryption s_globalRNG;
NAMESPACE_END
RandomNumberGenerator & GlobalRNG()
{
return dynamic_cast<RandomNumberGenerator&>(s_globalRNG);
}
NAMESPACE_END // Test
NAMESPACE_END // CryptoPP
Seeding in test.cpp
// Don't do this in production because it creates a deterministic generator
OFB_Mode<AES>::Encryption& aesg = dynamic_cast<OFB_Mode<AES>::Encryption&>(Test::GlobalRNG());
aesg.SetKeyWithIV((byte *)seed.data(), 16, (byte *)seed.data());
A lot of folks have had this problem over the years. At Crypto++ 6.0, we moved GlobalRNG() into the Test namespace. Test is a new namespace, and we hope Test::GlobalRNG() will provide the signals that something is amiss in your library build or project configuration.
Also see Issue 379, Add Test namespace within CryptoPP namespace and Commit 73836e58a5f5c11c.

Full process for compiling/linking and using MATLAB coder generated C++ files

I've been struggling with this for a very long time.
Using MATLAB's coder, I've generated a C++ code, but I'm not sure how to implement it in my main file.
This is the directory containing the MATLAB generated C++ code. Could somebody please walk me through how to use this code in another main file?
(The whole process of making a make file, include statements, etc;) I really would appreciate it if the explanation is specific and thorough.
Here is one of the header files that I need to include (IOPfinal.h)
#ifndef IOPFINAL_H
#define IOPFINAL_H
// Include Files
#include <math.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include "rt_nonfinite.h"
#include "rtwtypes.h"
#include "IOPfinal_types.h"
// Function Declarations
extern double IOPfinal(const emxArray_real_T *wavelength, const emxArray_real_T *
intensity, double range, const emxArray_real_T *polymat);
#endif
I've heard that there are makefiles that are also provided by MATLAB but I'm not sure exactly how to use them and what I should do after using that makefile either. The code below is the main file that I intend to use.
#include "IOPfinal.h"
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include "IOPfinal.h"
#include "IOPfinal_emxAPI.h"
using namespace std;
int main() {
double *wavelength, *intensity;
// some code to initialize wavelength and intensity
emxArray_real_T *inputs, *outputs;
inputs = emxCreateWrapper_real_T(wavelength, 1, 3);
outputs = emxCreateWrapper_real_T(intensity, 1, 3);
return 0;
}
Thanks in advance!

‘hash’ is already declared in this scope using tr1::hash;

I am trying to compile C++ code shown below but I got an error saying,
In file included from src/LM.h:3:0,
from src/LM.cpp:1:
src/common.h:30:13: error: ‘hash’ is already declared in this scope
using tr1::hash;
This is the command I used to compile the files below.
g++ -std=c++11 -Wall src/Foo.cpp
Foo.cpp
#include "Foo.h"
...
Foo.h
#ifndef FOO_H
#define FOO_H
#include "common.h"
//more code here
#endif
common.h
#ifndef _COMMON_H_
#define _COMMON_H_
#include <iostream>
#include <fstream>
#include <cmath>
#include <cassert>
#include <cstdlib>
#include <utility>
#include <vector>
#include <string>
#include <array>
#include <algorithm>
#include <set>
#include <tr1/unordered_map>
#include <tr1/functional>
namespace std {
using tr1::unordered_map;
using tr1::hash;
} // namespace std
using namespace std;
//more code here
#endif
I want the source code to use std::tr1::unordered_map and std::tr1::hash rather than std::unordered_map and std::hash(Actually I am making some modifications to distributed files which does uses std::tr1::unordered_map and std::tr1::hash).
What is possibly wrong with my codes?
UPD:
https://github.com/clab/fast_align/blob/master/src/port.h seems to do the same thing as mine. However, this compiles without any problem... Have any idea?
There is already std::hash in C++11. You cannot redefine it. You can use another name for tr1::hash.
Probably the best idea (if you really want to use std::tr1::hash/std::tr1::unordered_map instead of C++11 structures) is to write your own namespace in which using all structures, that you want without std::hash/std::unordered_map.
namespace common
{
using std::tr1::hash;
using std::tr1::unordered_map;
using std::vector;
// and so on
}

Segmentation fault (core dumped) when using larger boost-matrices

If I run the code the error occurs: Segmentation fault (core dumped)
The problem does not exist if I decrease the dimensions of MatSamplesDimM in the header-file (e.g. from 300000 to 30000) or if I just declare Matrix1 and not Matrix2 in the main-file. The problem is independent of environment in which I run the code (eclipse or from terminal).
Do you have any ideas? Thank you very much for your help!
StrangeBehavior.cpp is my main-File:
// System includes
#include <iostream>
#include <fstream> //fstream: Stream class to both read and write from/to files; for output
// Boost includes
#include <boost/numeric/ublas/vector.hpp>
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/io.hpp>
#include <boost/assign/list_of.hpp>
#include <boost/math/distributions/normal.hpp> //to create normal distribution
#include <vector> // stl vector header
#include <boost/accumulators/accumulators.hpp>
#include <boost/accumulators/statistics/stats.hpp> //accumulator for mean, variance...
#include <boost/accumulators/statistics/mean.hpp>
#include <boost/bind/bind.hpp>
#include <boost/numeric/ublas/vector_proxy.hpp> // for using row()...
#include <boost/numeric/ublas/matrix_proxy.hpp> // for using row()...
#include <boost/numeric/ublas/vector_expression.hpp> // for using inner_prod
#include <boost/numeric/ublas/matrix_expression.hpp> // for using inner_prod, element_div
#include <boost/math/special_functions/gamma.hpp> // for Gamma function
#include "ParameterDeclaration.hpp"
using namespace std; // saves us typing std:: before vector
int main() {
std::cout << "Starting " << std::endl;
MatSamplesDimM Matrix1;
MatSamplesDimM Matrix2;
std::cout << "Finishing" << std::endl;
return 0;
}
and ParameterDeclaration.hpp my header-File:
#ifndef parameter_
#define parameter_
#include <iostream>
namespace ublas = boost::numeric::ublas;
typedef ublas::bounded_matrix<double,300000,2> MatSamplesDimM;
#endif
Try using the heap instead of the stack. This means allocate the matrices using new.