This is the code for a fractions program that is to add and subtract(i know it does not subtract yet)
I am having error messages that I dont understand.

// File Fraction.h

#ifndef FRACTION_H
#define hbg_cmpsc122

#include <iostream>
using namespace std;

namespace hbg_cmpsc122
{

    class Fraction
    {
    private:

	int num;
	int denom;

    public:

	Fraction();
	Fraction(int n, int d);
	int setNum(int n);
	int setDenom(int d);
	void print();

	Fraction add_func (Fraction a);
	Fraction sub_func (Fraction b);

	
    };
}

#endif
// File fraction.cxx


#include "fraction.h"
#include <iostream>
#include <cstdlib>
#include <cassert>

using namespace std;

namespace hbg_cmpsc122
{
  
    Fraction::Fraction()
    {
	num = 0;
	denom = 1;
    }

    Fraction::Fraction(int n, int d)
    {
	num = n;
	denom = d;
    }


    int Fraction::setNum(int )
    {

	return num;
    }

    int Fraction::setDenom(int )
    {
      
	return denom;
    }

    void Fraction::print()
    {
	// displays fraction on screen
	cout << "Fraction: " << num << "/" << denom << endl;
    }

    Fraction Fraction::add_func(Fraction a)
    {
	Fraction answer;

	if (a.denom =! a.setDenom())
	{
	    answer.denom = a.denom * a.setDenom() + (a.setNum() * a.denom);
	    answer.num = (a.num * a.setNum())+(a.setNum() * a.denom);
	    a.denom = answer.denom;
	    a.num = answer.num;
	}
	else
	{
	    answer.denom = a.denom;
	    answer.num = a.num + a.setNum();
	}

	return answer;



    }    

}
// File Main.cpp

#include <iostream>
#include <cstdlib>
#include "fraction.h"

using namespace std;

using hbg_cmpsc122::Fractions;

int main()
{
    
    fraction f1(3,2), f2(4,5), f3, f4;

    cout <<endl << "Fraction f1 is: ";
    f1.print();

    cout <<endl << "Fraction f2 is: ";
    f2.print();

    f3 = f1.add_func(f2);

    cout << endl << endl << "The sum of the fractions is ";
    f3.print;


    return 0;
}

This is the code for a fractions program that is to add and subtract.
I am having error messages that I dont understand.

fraction.cxx: In member function `<unnamed>::Fraction <unnamed>::Fraction::add_func(<unnamed>::Fraction)':
fraction.cxx:40: error: `((<unnamed>::Fraction*)this)-><unnamed>::Fraction::denom' cannot be used as a function
fraction.cxx:42: error: no matching function for call to `<unnamed>::Fraction::setDenom()'
fraction.h:25: note: candidates are: int <unnamed>::Fraction::setDenom(int)
fraction.cxx:42: error: no matching function for call to `<unnamed>::Fraction::setNum()'
fraction.h:24: note: candidates are: int <unnamed>::Fraction::setNum(int)
fraction.cxx:43: error: no matching function for call to `<unnamed>::Fraction::setNum()'
fraction.h:24: note: candidates are: int <unnamed>::Fraction::setNum(int)
fraction.cxx:43: error: no matching function for call to `<unnamed>::Fraction::setNum()'
fraction.h:24: note: candidates are: int <unnamed>::Fraction::setNum(int)
fraction.cxx:50: error: no matching function for call to `<unnamed>::Fraction::setNum()'

Recommended Answers

All 4 Replies

>>using hbg_cmpsc122::Fractions;

Fractions is not in namespace hbg_cmpsc122 because you have misspelled the class name. Pay closer attention to spelling and capitalization -- fraction is not the same thing as Fraction.

What compiler are you using? I'm on MS-Windows using vc++ 2010 Express and I get lots of error messages and warnings.

Also, delete line 4 in Fractions.h.

I am using g++

I don't use that compiler, but I know there are flags that will make it spit out more warnings and errors.

Two quick comments, AD is correct that you should have deleted line 4 in Fraction.h, [and that you have misspelled fractions in main()] (what it does is define the symbol hbg_cmpsc122 as and empty value. Therefore when you write namespace hbg_cmpsc122 { you have effectively writen namespace { .

This is why you have all the error messages to the unnamed namespace.

The normal include file guard, is to write this

#ifndef FRACTION_H
#define FRACTION_H

 // your stuff here...

#endif

So perhaps it would be better to change line 4 of Fraction.h to #define FRACTION_H .

Finally, if you want to turn warnings on for g++ then use g++ -W -Wall.

commented: Yes, that's what I meant :) +33
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.