473,396 Members | 2,039 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

error: Class * has no member named *

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
Expand|Select|Wrap|Line Numbers
  1. bool operator>=( const Fraction& One, const Fraction& Two )
  2. {
  3.     int a, b, c, d, e;
  4.  
  5.     a = One.Numer();
  6.     b = One.Denom();
  7.     c = Two.Numer();
  8.     d = Two.Denom();
  9.  
  10.     e = b;
  11.     a = a*d;
  12.     b = b*d;
  13.     c = c*e;
  14.     d = d*e;
  15.  
  16.     return( a >= c );
  17. }
  18.  
the private declaration is
Expand|Select|Wrap|Line Numbers
  1.   private:
  2.  
  3.     int Numer_;
  4.     int Denom_;
  5.  
and the boolean operation is as follows
Expand|Select|Wrap|Line Numbers
  1. bool operator>=( const Fraction& One, const Fraction& Two);
  2.  
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
Feb 28 '08 #1
6 30548
oler1s
671 Expert 512MB
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...
Feb 28 '08 #2
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
Feb 28 '08 #3
Laharl
849 Expert 512MB
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.

Expand|Select|Wrap|Line Numbers
  1. //foo.h
  2. class foo{
  3.   public bar;
  4.   private blah;
  5.   int getBlah();
  6. };
  7.  
  8. //foo.cpp
  9. #include "foo.h"
  10. int foo::getBlah(){
  11.   return blah;
  12. }
  13.  
Feb 28 '08 #4
oler1s
671 Expert 512MB
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?

Expand|Select|Wrap|Line Numbers
  1. class A
  2. {
  3.     int add(int a, int b);
  4. };
  5.  
  6. int add(int c, int d);
  7.  
  8. int A::add(int a, int b) { return a + b }
  9. int add(int c, int d) { return c + d }
  10.  
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.
Feb 28 '08 #5
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
Expand|Select|Wrap|Line Numbers
  1. bool operator!=( const Fraction& One )
  2. {
  3.     Fraction temp;
  4.     int a, b, c, d, e;
  5.  
  6.     a = One.Numer_;
  7.     b = One.Denom_;
  8.     c = temp.Numer_;
  9.     d = temp.Denom_;
  10.  
  11.     e = b;
  12.     a = a*d;
  13.     b = b*d;
  14.     c = c*e;
  15.     d = d*e;
  16.  
  17.     return( a != c );
  18. }
  19.  
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
Feb 29 '08 #6
Laharl
849 Expert 512MB
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.

Expand|Select|Wrap|Line Numbers
  1. class Foo{
  2.  int bar;
  3.  bool operator<(const Foo& other); //This is effectively bool operator<(this, const Foo& other);
  4. }
  5.  
  6. bool Foo::operator<(const Foo& other){
  7.  //Implementation
  8. }
  9.  
Feb 29 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

4
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; };
1
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 { ...
0
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:
7
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...
2
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...
3
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...
8
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...
3
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...
2
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
0
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...
0
Oralloy
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,...
0
tracyyun
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...
0
isladogs
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.