user3273814
user3273814

Reputation: 218

Error deriving from Boost Rational

I'm having trouble getting my compiler (g++) to include certain header files from the boost C++ libraries, that are in a subdirectory of my bin directory. Specifically, I'm trying to include the rational class header file, rational.hpp. First I tried including via this scripting code in my makefile:

intRational.o: intRational.h
    g++ -I /home/.../boost_1_58_0 -c intRational.h

However, it gives me this error:

In file included from testRational.cpp:11:0:
intRational.h:17:1: error: expected class-name before ‘{’ token
 {
 ^
make: *** [testRational.o] Error 1

This is intRational.h:

#ifndef _INTRATIONAL_H
#define _INTRATIONAL_H

#include<boost/rational.hpp>
//Derived class
class intRational: public rational
{
  bool simplify;
  public:
    void setSimple()
    {
      simplify=true;
    };
    void setNoSimplify()
    {
      simplify=false;
    };
    bool getFlag()
    {
      return simplify;
    };
};


#endif // _INTRATIONAL_H

Next I replaced

#include<boost/rational.hpp>

With the full directory

#include</home/.../boost_1_58_0/boost/rational.hpp>

(... represents part of the directory that shows my identity)

This fixed the first problem, but now I have a new error:

In file included from intRational.h:14:0,
                 from testRational.cpp:11:
/home/.../boost_1_58_0/boost/rational.hpp:82:78: fatal error: boost/integer/common_factor_rt.hpp: No such file or directory
 #include <boost/integer/common_factor_rt.hpp> // for   boost::integer::gcd, lcm

This error originates from the boost library file, rational.hpp. I don't want to put the full directory in the #include, because then I will have to do this for the #include that are in the header files referenced by common_factor_rt.hpp for instance, and each header file on down, which is a lot of work. I shouldn't have to, because it kind of defeats the purpose of using libraries.

Upvotes: 2

Views: 355

Answers (1)

sehe
sehe

Reputation: 393593

Looks like you want an integer rational:

#include <boost/rational.hpp>

// Derived class
class intRational : public boost::rational<int> {
    bool simplify;

  public:
    void setSimple()     { simplify = true;  } ;
    void setNoSimplify() { simplify = false; } ;
    bool getFlag()       { return simplify;  } ;
};

Upvotes: 3

Related Questions