CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Compile error

  1. #1
    Join Date
    Feb 2012
    Posts
    9

    Compile error

    I get three errors that say "must declare a body because it is not marked abstract, extern or partial" This code is suppose to just add, subtract, multiply and divide fractions.

    Thanks for any replies.




    I know the problem is in this part of the code:

    }


    public static fraction operator +(fraction lhs, fraction rhs);
    public static fraction operator -(fraction lhs, fraction rhs);
    public static fraction operator *(fraction lhs, fraction rhs);
    public static fraction operator /(fraction lhs, fraction rhs)
    {

    {

    //

    Here is the hole code that I have.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace ConsoleApplication60
    {
    public class fraction
    {
    private int numerator;
    private int denominator;
    public fraction(int numerator, int denominator)


    {
    this.numerator = numerator;
    this.denominator = denominator;
    }


    public static fraction operator +(fraction lhs, fraction rhs);
    public static fraction operator -(fraction lhs, fraction rhs);
    public static fraction operator *(fraction lhs, fraction rhs);
    public static fraction operator /(fraction lhs, fraction rhs)
    {

    {

    }
    int firstProduct = lhs.numerator + rhs.numerator;
    int secondProduct = rhs.denominator = lhs.denominator;

    return new fraction(
    lhs.numerator + rhs.numerator,
    lhs.denominator = rhs.denominator
    );

    }
    public override string ToString()
    {
    String s = numerator.ToString() + "/" +
    denominator.ToString();
    return s;
    }


    }

    public class Tester
    {
    public void Run()
    {
    // addition of fractions
    fraction f1 = new fraction(3, 4);
    Console.WriteLine("f1: {0}", f1.ToString());
    //
    fraction f2 = new fraction(2, 4);
    Console.WriteLine("f2: {0}", f2.ToString());
    //
    fraction f3 = f1 + f2;
    Console.WriteLine("f1 + f2 = f3: {0}", f3.ToString());
    //
    //
    // Subtraction of fractions
    fraction f4 = new fraction(6, 8);
    fraction f5 = new fraction(7, 8);

    fraction f6 = f4 - f5;
    Console.WriteLine(f4 + "-" + f5 + " = " + f6);
    //
    //
    // multiplication of fractions
    fraction f7 = new fraction(6, 8);
    fraction f8 = new fraction(7, 8);

    fraction f9 = f7 * f8;
    Console.WriteLine(f7 + "*" + f8 + " = " + f9);
    //
    //
    // Division of fractions
    fraction f10 = new fraction(6, 8);
    fraction f11 = new fraction(7, 8);

    fraction f12 = f10 / f11;
    Console.WriteLine(f10 + "/" + f11 + " = " + f12);





    }
    static void Main()
    {
    Tester t = new Tester();
    t.Run();
    }
    }
    }
    Attached Files Attached Files

  2. #2
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,016

    Re: Compile error

    These lines are the problem:

    Code:
    public static fraction operator +(fraction lhs, fraction rhs);
    public static fraction operator -(fraction lhs, fraction rhs);
    public static fraction operator *(fraction lhs, fraction rhs);
    They declare functions, but then do not provide an implementation of those functions. You must write code saying what those functions do.

    Your structure seems to be really confused. The outline for this code should be:
    Code:
    public static fraction operator +(fraction lhs, fraction rhs)
    {
        //Put the code to add fractions here
    }
    public static fraction operator -(fraction lhs, fraction rhs)
    {
        //Put the code to subtract fractions here
    }
    public static fraction operator *(fraction lhs, fraction rhs)
    {
        //Put the code to multiply fractions here
    }
    public static fraction operator /(fraction lhs, fraction rhs)
    {
        //Put the code to divide fractions here
    }
    Does that make sense?

    In the future, please also surround your code with [code] and [/code] tags to preserve formatting.
    Best Regards,

    BioPhysEngr
    http://blog.biophysengr.net
    --
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured