Reputation: 107
So I'm studying templates in C++ and I have this template class, "Test.h":
#ifndef TEST_H
#define TEST_H
#include <cassert>
#include <iostream>
#include <cmath> // for std::abs
template<unsigned int DIM> class Test
{
private:
double abs_error = 1e-6;
double mData[DIM];
public:
double& operator[](int index)
{
// To check that the input index is a valid one
assert(index < DIM);
assert(index > -1);
// Condition that checks for values between 0 and 1 inclusive
if (mData[index] >= 0.0 && mData[index] <= 1.0)
{
return mData[index];
}
// Values less than zero
else if (std::abs(mData[index]) <= abs_error && mData[index] <= 0.0)
{
return mData[index] = 0;
}
// Values more than one
else if (mData[index] >= 1.0 && std::abs(mData[index] - 1.0) <= abs_error)
{
std::cout << "You used this condition." << std::endl;
return mData[index] = 1;
}
// For every other possible value
else
{
assert(0);
}
return mData[index];
}
};
#endif //TEST_H
which technically checks for the value assigned to a particular index in the array. What I want it to do is that, if the number is between 0 and 0.000001, I want it to return 0 for that particular index. If the number is between 1 and 1.000001 I want it to change the value assigned to that particular index to 1. And if the number is less than zero or more than one, I want it to raise an assert statement, for example, if the value stored in a particular index of the array is 2 or -0.3.
So, I tested it out in a main file: "main.cpp"
#include <iostream>
#include <cmath>
#include "Test.h"
int main()
{
Test<6> p;
p[0] = 0.5;
std::cout << "p[0]: " << p[0] << std::endl;
p[1] = -1e-7;
std::cout << "p[1]: " << p[1] << std::end;
// Comment the following two lines and check the test statement
p[2] = 1+1e-8;
std::cout << "p[2]: " << p[2] << std::endl;
// This code tests the same condition as in Test.h for
// values more than one
bool foo = 1+1e-8 >= 1.0 && std::abs(1+1e-8 - 1.0) <= 1e-6;
std::cout << "foo (should return 1): " << foo << std::endl;
return 0;
}
So, in the "main.cpp" file, the first value is 0.5, so it checks true because it's between 0 and 1 inclusive.
The next one also returns true because it's between 0 and 0.000001 inclusive, so it is assigned 0 and should print it on screen.
But the third one raises the assertion, but if you comment that part out and leave the test statement at the bottom of the file, it will return true, even though it's the same logical condition.
My question is, what am I doing wrong here?
EDIT: If I compile in Linux Ubuntu 14.06 it won't work, but if I compile in OS X Yosemite it works.
Upvotes: 0
Views: 165
Reputation: 4275
The problem is probably that you not initialize the values.
Lines like
p[0] = 0.5;
also call the double& operator[]
. But when first assigning a value, the result already depends on whatever is in your memory. If its smaller than 1+abs_error
everything is fine, if not, it raises the assert(0)
.
So you should initialize your array with something that at least not raise an assert, e.g. {0}
Upvotes: 2