Error calling method on object... I'M PULLING MY HAIR OUT HERE!!

R

Richard

I'm using Eclipse with the new C/C++ plugin and the MSYS compiler to
complete my assignments for programming class. The program I have now
is almost complete, however, it is generating a syntax error when I
try to call the readFraction method on my Fraction object.

I've been over it again and again and I don't see what's wrong here. I
tried modifying readFraction to accept no arguments, and changed the
method calls to not pass any arguments. No dice. Same error!!

The syntax error is generated in main() on the f1.readFraction() and
f2.readFraction() method calls. The error I'm getting is "request for
member `readFraction' in `f1()', which is of", whatever the heck that
means.

It seems like this should work. Any help would be appreciated! Please
post here and don't e-mail as I don't check my Hotmail that often.


CODE BELOW -------------------------

#include <iostream>

class Fraction {
int numerator, denominator;
public:
Fraction(int whole) {
this->numerator = whole;
this->denominator = 1;
}
Fraction(int numerator, int denominator) {
this->numerator = numerator;
this->denominator = denominator;
}
void readFraction(char* prompt) {
char* sfrac;
std::cout << prompt << "\n";
std::cin >> sfrac;
}
};

int main() {
Fraction f1(), f2(), f3();
f1.readFraction("Enter 1st fraction:");
f2.readFraction("Enter 2nd fraction:");
}
 
R

Rob Williscroft

Richard wrote in
#include <iostream>

class Fraction {
int numerator, denominator;
public:
Fraction(int whole) {
this->numerator = whole;
this->denominator = 1;
}
Fraction(int numerator, int denominator) {
this->numerator = numerator;
this->denominator = denominator;
}
void readFraction(char* prompt) {
char* sfrac;

A char * is not a string, change the above to:

std::string sFract;

remember to #include said:
std::cout << prompt << "\n";
std::cin >> sfrac;

}
};

int main() {
Fraction f1(), f2(), f3();

The above are not what you think, they are actually declaration's
of function's that take no arguments and return a fraction object.

Change to:

Fraction f1, f2, f3;

f1.readFraction("Enter 1st fraction:");
f2.readFraction("Enter 2nd fraction:");
}

HTH.

Rob.
 
A

Andy

Richard said:
I'm using Eclipse with the new C/C++ plugin and the MSYS compiler to
complete my assignments for programming class. The program I have now
is almost complete, however, it is generating a syntax error when I
try to call the readFraction method on my Fraction object.

I've been over it again and again and I don't see what's wrong here. I
tried modifying readFraction to accept no arguments, and changed the
method calls to not pass any arguments. No dice. Same error!!

The syntax error is generated in main() on the f1.readFraction() and
f2.readFraction() method calls. The error I'm getting is "request for
member `readFraction' in `f1()', which is of", whatever the heck that
means.

It seems like this should work. Any help would be appreciated! Please
post here and don't e-mail as I don't check my Hotmail that often.


CODE BELOW -------------------------

#include <iostream>

class Fraction {
int numerator, denominator;
public:
Fraction(int whole) {
this->numerator = whole;
this->denominator = 1;
}
Fraction(int numerator, int denominator) {
this->numerator = numerator;
this->denominator = denominator;
}
void readFraction(char* prompt) {
char* sfrac;
std::cout << prompt << "\n";
std::cin >> sfrac;
}
};

int main() {
Fraction f1(), f2(), f3();
f1.readFraction("Enter 1st fraction:");
f2.readFraction("Enter 2nd fraction:");
}

I remember this, I had to do this for a comp programming class in high school
once, and if this is not the same for you, then it looks really
familiar.
 
D

David White

Richard said:
#include <iostream>

class Fraction {
int numerator, denominator;
public:
Fraction(int whole) {
this->numerator = whole;
this->denominator = 1;
}
Fraction(int numerator, int denominator) {
this->numerator = numerator;
this->denominator = denominator;
}
void readFraction(char* prompt) {
char* sfrac;
std::cout << prompt << "\n";
std::cin >> sfrac;
}

Unless it is part of your assignment to do so, I would not recommend putting
readFraction in the Fraction class. Most classes are designed to be of the
most general use possible. There are countless ways that you can get the
parameters to pass to the constructor of a Fraction object - entered by the
user; read from a file of just fractions; read from a file with a mixture of
fractions and other, unrelated, items; calculated by expressions in your
code, retrieved from an array, returned from a function, etc. You should be
able to use the Fraction class in any application without having to change
it, so why should it have a member function that reads a value from 'cin'?

I suggest that you move that function out of the class and make it a global
function.

DW
 
E

Ekkehard Morgenstern

Hi Richard,

Richard said:
class Fraction {
Fraction(int whole)
Fraction(int numerator, int denominator)
}

Fraction f1(), f2(), f3();

First off, you have not declared a default constructor ( Fraction() or
Fraction( void ) ) for the class, hence you might want to do that (like,
setting numerator and denominator to 1 as a default value).
void readFraction(char* prompt)

f1.readFraction("Enter 1st fraction:");
f2.readFraction("Enter 2nd fraction:");

Then, you have provided "const char[]" arguments to a "char*" function
(readFraction()). Hence, the compiler chokes on it.
Change the "readFraction(char*)" declaration to "readFraction( const char*
prompt )".
char* sfrac;
std::cin >> sfrac;

This will crash, because "sfrac" doesn't point to memory. Change it to
something like "char sfrac[512];" instead.

I hope that helps.

Regards,
Ekkehard Morgenstern.
 
T

Thomas Matthews

Richard wrote:
#include said:
class Fraction {
int numerator, denominator;
A good style is one declaration per line. A lot easier
to remove or single step in a debugger.

public:
Fraction(int whole) {
this->numerator = whole;
this->denominator = 1;
}
1. Did you know that you don't need to use the "this"
pointer inside member functions?
2. Did you know about initialization lists:
Fraction(int whole)
: numerator(whole), denominator(1)
{ }
Fraction(int numerator, int denominator) {
this->numerator = numerator;
this->denominator = denominator;
}
Another good place for initialization list:
Fraction (int new_numerator, int new denominator)
: numerator(new_numerator),
denominator(new_denominator)
{
}

Have you considered copy constructor:
Fraction(const Fraction& f)
: numerator(f.numerator), denominator(f.denominator)
{ }

And perhaps a destructor (to complete the set):
virtual ~Fraction() {}
void readFraction(char* prompt) {
char* sfrac;
std::cout << prompt << "\n";
std::cin >> sfrac;
Hmm, you read a single character but don't do anything
with it. Did you have any processing in mind?
For example, extracting the numerator from the input
string?
std::cin >> numerator;
std::cin.ignore(100000, '/');
std::cin >> denominator;
if (denominator == 0)
throw /* exception */

Of course there is also the assignment operator:
Fraction& operator=(const Fraction& f)
{
if (this != &f)
{
numerator = f.numerator;
denominator = f.denominator;
}
return *this;
}

Oh, and what about checking for zeros in the denominator?
};

int main() {
Fraction f1(), f2(), f3();
f1.readFraction("Enter 1st fraction:");
f2.readFraction("Enter 2nd fraction:");
You forgot a return value.
return 0;
or
return EXIT_SUCCESS; // See <cstdlib>
or
return EXIT_FAILURE; // See said:


--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 
R

Richard

Thanks for all of the great replies, fellows. Adding a default
constructor and changing the declaration of the fraction objects fixed
the problem for me. I also implemented a few other suggestions,
including parsing the fraction on input using the cin.ignore functon
(I didn't even know that existed!)

I, too, considered removing the readFraction() function from the
Fraction object, but the assignment specifically stated that it was to
be part of the object and not external. Go figure.

Thanks again! I had no clue there were so many smart people on here.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,737
Latest member
Georgeengab

Latest Threads

Top