Upon compiling my code for a fraction class, the error that i get is a good 50 or so repetitions of
error: Class "Fraction" has no member named "Numer"
and
error: Class "Fraction" has no member names "Denom"
I'm guessing it has something to do with the declaration of these private objects in the .h file, and it's use in the source code
here's a snippet of one of the functions -
bool operator>=( const Fraction& One, const Fraction& Two )
-
{
-
int a, b, c, d, e;
-
-
a = One.Numer();
-
b = One.Denom();
-
c = Two.Numer();
-
d = Two.Denom();
-
-
e = b;
-
a = a*d;
-
b = b*d;
-
c = c*e;
-
d = d*e;
-
-
return( a >= c );
-
}
-
the private declaration is -
private:
-
-
int Numer_;
-
int Denom_;
-
and the boolean operation is as follows -
bool operator>=( const Fraction& One, const Fraction& Two);
-
I'm unsure on classes in c++, but this error seems to be the only one
If you need more of my code to find the error, let me know.
Thanks,
-Myxamatosis
6 30548
Why are you so surprised? You create private variables for the class. Then you create a separate function, that tries to access those private variables. Perhaps your function shouldn't be external to the Fraction class...
the bool functions are in the class source code, and the private members are in the fraction.h file.
I guess I just don't understand what you're getting at...I thought when you did
#include "proj.fraction.h"
in the .cpp file, it grants you the access to the members
If your .cpp file is called proj.fraction.cpp, you will have access to them, if the functions are declared as member functions for your fraction class. The linker handles all this for you, as long as you #include your .h file. -
//foo.h
-
class foo{
-
public bar;
-
private blah;
-
int getBlah();
-
};
-
-
//foo.cpp
-
#include "foo.h"
-
int foo::getBlah(){
-
return blah;
-
}
-
I won't directly feed you the answer, because I want you to reach it on your own, but I'll say the following. If it doesn't make sense, don't hesitate to ask me to clarify.
the bool functions are in the class source code
I would imagine so. Your intention is that the bool functions are part of the class, right? However, just putting a function in the same source file as the rest of the class source code doesn't mean they are actually part of the class. Think of it this way. I could take each function of the class, and have it in a different source file. That's perfectly fine. I need to explicitly mark a function as part of a class. Still not getting it? -
class A
-
{
-
int add(int a, int b);
-
};
-
-
int add(int c, int d);
-
-
int A::add(int a, int b) { return a + b }
-
int add(int c, int d) { return c + d }
-
Same for operators... Look up operator overloading for classes explicitly though.
I thought when you did
#include "proj.fraction.h"
in the .cpp file, it grants you the access to the members
All an include statement does is a copy/paste. That's it. Why do you need to copy paste fraction.h into the source file? Because when the compiler sees your source file, it's going to be sitting around wondering what "Fraction" is. So you need to declare the existence of this class called Fraction, and describe what's in it. But that doesn't mean when Fraction is declared that suddenly all syntax rules go out the window.
Sure, fraction.h is included. But is the operator >= a part of the fraction class? Not the way you wrote it. Then, because the variables are private, they cannot be accessed.
I toyed around with it, and I've managed to rid myself of the operator errors.
However, when I changed my logic operators to accept one parameter ( I had an error saying it can accept only one or zero) it now tells me that
error: `bool operator!=(const Fraction&)' must take exactly two arguments
The source code for the operator goes like this -
bool operator!=( const Fraction& One )
-
{
-
Fraction temp;
-
int a, b, c, d, e;
-
-
a = One.Numer_;
-
b = One.Denom_;
-
c = temp.Numer_;
-
d = temp.Denom_;
-
-
e = b;
-
a = a*d;
-
b = b*d;
-
c = c*e;
-
d = d*e;
-
-
return( a != c );
-
}
-
I originally had it accepting two Fraction& arguments, but that gave me an error too
I'm kinda lost on this one again, some direction would be great
That has to be a class member function so that the "this" object can be the hidden first parameter, as it is with all class methods.. 'This' is a pointer to the object calling the method so that it can be set to the other Fraction object. -
class Foo{
-
int bar;
-
bool operator<(const Foo& other); //This is effectively bool operator<(this, const Foo& other);
-
}
-
-
bool Foo::operator<(const Foo& other){
-
//Implementation
-
}
-
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Jian H. Li |
last post by:
Hello,
What's the essential differences between the two ways of "class::member"
& "object.member"(or object_pointer->member)?
class C{
public:
void f() {}
int i;
};
|
by: zx.zhangxiong |
last post by:
Dear all,
I'm puzzled about the usage of class member function. Any help would be
appreciated.
class Account {
...
|
by: michael |
last post by:
How can I reference an instance of a static class member accross all translation units? I don't want to pass a reference between dlls.
Lib1.dll
PROJECT 1
A.h
class A
{
public:
|
by: Uwe Grawert |
last post by:
Hello,
a C-function expects me to give a pointer to a function as parameter.
i want to give this function a pointer to a member function of a class
but the compiler gives me an error:
void...
|
by: Phil Davidson |
last post by:
/*
(Posted to microsoft.public.dotnet.languages.vc)
In C++/CLI under Visual Studio 2005, creating an "enum class" member
called "Equals" can cause compiler error C1060 (out of memory) and C1063...
|
by: =?Utf-8?B?Y2FsZGVyYXJh?= |
last post by:
Dear all,
I have one question base on a choice of having a public interfcace access
compare to public class member.
IN other word let say that I have a public interface named ImyInterface with...
|
by: nishit.gupta |
last post by:
I was having a problem with template class memer function definition ,
so i serched the net
and find that template class member fuction definition should be in
header file else compilation
will...
|
by: zaeminkr |
last post by:
I have class member data which is a pointer to class member function.
However, I'm fail to compile the code.
What is the correct grammar to make a function call by using static
member data...
|
by: R Samuel Klatchko |
last post by:
Ron Eggler wrote:
You can do this with a forwarding function that is a static member (you
can also do it with a non-member function). Because signal does not
give you a way to attach some...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |