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 30728 oler1s 671
Recognized Expert Contributor
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
Laharl 849
Recognized Expert Contributor
If your .cpp file is called proj.fraction.c pp, 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;
-
}
-
oler1s 671
Recognized Expert Contributor
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!=(cons t 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
Laharl 849
Recognized Expert Contributor
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 (MyClass::*)(void*, void*) can not be converted to
void (*)(void*, void*)
i declared the function as protected. Is it not possible to give
|
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
(internal error). See the following example program, a CLR console app.
Of course the obvious workaround is to refrain from using "Equals". But
does that really violate the specification of the language or the CLR?
And can the compiler be...
| |
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
public function and property.
Then I define a class which implements that interface.My class contains
public fcunction which provide services.
|
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 be successful but not linking.
Can somebody help me in telling the exact reason why compiler cannot
find the reference of
template class member function definition defined in cpp file??
following problem is not linking (undefined reference of...
|
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 which is a pointer to a member function?
class Test
{
|
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 caller defined data, you will also need a
static member variable to hold a pointer to the instance of the class
you want to use:
foo.h:
class foo
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |