2

Consider the following code:

#include<iostream>
using namespace std;
class sample
{
    int a;
    int b;
public:
    void setValue()
    {
        a=25;
        b=40;
    }
    friend float mean(sample s)
    {
        return float(s.a+s.b)/2.0;
    }
}
int main()
{
    sample x;

    x.setValue();
    cout<< "mean value:"<< mean(x)<<endl;

    cin.ignore();
    getchar();
    return (0);
}

I expected this to output when attempting to compile and run it:

mean value: 32.5

However, I get these compiler errors isntead:

1>  frndF.cpp
1>c:\users\mg\documents\visual studio 2010\projects\frndf\frndf\frndf.cpp(18): error C2628: 'sample' followed by 'int' is illegal (did you forget a ';'?)
1>c:\users\mg\documents\visual studio 2010\projects\frndf\frndf\frndf.cpp(19): error C3874: return type of 'main' should be 'int' instead of 'sample'
1>c:\users\mg\documents\visual studio 2010\projects\frndf\frndf\frndf.cpp(27): error C2664: 'sample::sample(const sample &)' : cannot convert parameter 1 from 'int' to 'const sample &'
1>          Reason: cannot convert from 'int' to 'const sample'
1>          No constructor could take the source type, or constructor overload resolution was ambiguous
1>c:\users\mg\documents\visual studio 2010\projects\frndf\frndf\frndf.cpp(15): warning C4244: 'return' : conversion from 'double' to 'float', possible loss of data

What did I do wrong?

In silico
  • 51,091
  • 10
  • 150
  • 143
  • 3
    And I quote: "did you forget a ';'?".... – Bart Dec 17 '11 at 11:25
  • Step 1: Get a [good book on C++](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). Step 2: Read it. Nothing wrong with making silly errors, but you need to learn the language and be able to error check your own code for simple things. – Cody Gray - on strike Dec 17 '11 at 11:26

2 Answers2

6

You forgot the semicolon after the closing bracket of the class definition:

class sample
{
   //code omitted for brevity

}; <------------------- you forgot this

Although, this has nothing to do with the error or expected output, you should pass the argument to mean() by const reference as:

friend float mean(sample const & s)
{                      //^^^^^^^ const reference
    return float(s.a+s.b)/2.0;
}

This avoids unnecessary copy of the argument.

Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • 4
    @jeet.mg: For future reference, try reading the error messages the next time something goes wrong. Sometimes, it'll even tell you what might've gone wrong (e.g. "error C2628: 'sample' followed by 'int' is illegal (**did you forget a ';'?**)"). Don't give up so easily after a few compiler errors. – In silico Dec 17 '11 at 11:29
  • 2
    i read that... but, i checked only statements. m frm java background. so didnt expected a ; after class –  Dec 17 '11 at 11:31
  • 2
    @jeet.mg: So you basically ignored what the compiler told you. You're surprised to see that it didn't work? The first thing you should do is (1) listen to the compiler, not to your preconceptions on how the language works, and (2) realize that C++ is nothing like Java. Get a [good intro to C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and learn proper C++. It's a lot less frustrating that way. – In silico Dec 17 '11 at 11:47
1

You need to end the class declaration with ;

class sample
{
    // code
};
Tudor
  • 61,523
  • 12
  • 102
  • 142