开发者

User defined data type that uses another user defined data type as a parameter

开发者 https://www.devze.com 2023-01-27 02:04 出处:网络
I am having trouble getting the compiler to accept my below defined user data type (Term) that accepts another user data type (Rational) as a parameter.Any suggestions on how to make this work would b

I am having trouble getting the compiler to accept my below defined user data type (Term) that accepts another user data type (Rational) as a parameter. Any suggestions on how to make this work would be great!

#ifndef _TERM_H
#define _TERM_H

#include "Rational.h"

using namespace std;

class Term {

public:

    //constructors
    Term( const Rational &a, const int &b)
    { 
        this->coefficient = a;
        this->exponent = b;
    }

    Term(){}

    ~Term () {}

    //print the Rational
    void print()const 
    {
        cout << coefficient << " x^"  << exponent << endl;
    }

private:

    Rational *coefficient, *a;
    int exponent, b;
};

#endif

#ifndef _TERM_H
#define _TERM_H

using namespace std;

class Rational {

public:

    //constructors
    Rational( const int &a, const int &b){
        if (a != 0)
            if (b != 0)
                this->numerator = a;
            this->denominator = b;
    }

    //print the Rational
    void print()const {
        cout << numerator << "/" << denominator << endl;
    } 

    //add 2 Rationals 
    void add(const Rational &a, const Rational &b){
        numerator = ((a.numerator * b.denominator)+(b.numerator*a.denominator开发者_高级运维));
        denominator = (a.denominator*b.denominator);  
    }

 ...

    private:
     int a, b, numerator, denominator;
};

#endif

I keep getting the below error messages.

Term.h(30) : error C2440: '=' : cannot convert from 'const Rational' to 'Rational *' 1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


First, change the definition of coefficient to this:

const Rational& coefficient;

Then change the constructor to this:

Term (const Rational& a, const int& b)
  : coefficient (a), exponent (b)
{
}


your problem is, that Term::coefficient has type Rational* (pointer to Rational) and in constructor you are trying to assign a value of type Rational to that member variable.

So the correct constructor would, if you want to keep the rest intact, like this:

Term(const Rational& a, int b) : coefficient(&a), exponent(b) {}

Or you can leave constructor intact and change the private section:

private:
    Rational coefficient;
    // the rest of the member variables


void print() const 
{
    cout << *coefficient << " x^"  << exponent << endl;
} 

Are you not missing a dereferencing operator there?


It appears that you are using the same include guard name (_TERM_H) in two different header files. This is very bad--you need to make the include guard name unique among all files within your project, or else the inclusion of one file will block the other. In particular, if I read your code correctly, you have this:

#ifndef _TERM_H
#define _TERM_H
#include "Rational.h"

And Rational.h has this:

#ifndef _TERM_H
#define _TERM_H

This should mean that when you include Rational.h, _TERM_H is already defined, so the contents of Rational.h will be ignored.

0

精彩评论

暂无评论...
验证码 换一张
取 消