473,756 Members | 1,841 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C++ classes using each other

6 New Member
I have this code it is not important tah methods have Serbian name it;s not that that's problem :)

Expand|Select|Wrap|Line Numbers
  1.  
  2. class cosinus;
  3.  
  4. class sinus : public funkcija
  5. {
  6. private:
  7.   funkcija *_op;
  8. public:
  9.   sinus(funkcija* op1)
  10.    :_op(op1)
  11.   {}
  12.  
  13.   void stampaj() const
  14.   {
  15.    cout << "sin(";
  16.     _op->stampaj();
  17.    cout << ")"; 
  18.   }
  19.   proizvod* izvod() const
  20.   {
  21.     return new proizvod(_op->izvod(),new cosinus(_op));
  22.   }
  23.   double vrednost(double argument) const
  24.   {
  25.     return sin(_op->vrednost(argument));
  26.   }
  27. };
  28.  
  29. class cosinus : public funkcija
  30. {
  31. private:
  32.   funkcija *_op;
  33. public:
  34.   cosinus(funkcija* op1)
  35.    :_op(op1)
  36.   {}
  37.  
  38.   void stampaj() const
  39.   {
  40.    cout << "cos(";
  41.     _op->stampaj();
  42.    cout << ")"; 
  43.   }
  44.   uminus* izvod() const
  45.   {
  46.     return new uminus(new proizvod(_op->izvod(),new sinus(_op)));
  47.   }
  48.   double vrednost(double argument) const
  49.   {
  50.     return cos(_op->vrednost(argument));
  51.   }
  52.  
  53.  
  54. };
  55.  
I get this errors
klase.cpp: In member function 'virtual proizvod* sinus::izvod() const':
klase.cpp:231: error: invalid use of incomplete type 'struct cosinus'
klase.cpp:212: error: forward declaration of 'struct cosinus'


what should I do, to make this work?
Aug 18 '09 #1
2 2672
Banfa
9,065 Recognized Expert Moderator Expert
@mirketiger
This error occurs when you try to use a type (class/struct) that you have declared but not defined. That is a type for which the compiler has only seen a forward declaration not and actual definition.

You are getting this because function sinus::izvod is trying to use the type cosinus but at that point there has only been a forward declaration of cosinus.

You need to define sinus::izvod in the presence of the definition of cosinus. The structure you have for your code does not allow this because you have mixed the definitions of the member functions of both sinus and cosinus into the definition of the types sinus and cosinus. You will have to change this so that your code has the following structure:
  1. define sinus including declarations (rather than definitions as you have at present) of its member functions.
  2. define cosinus including declarations (rather than definitions as you have at present) of its member functions.
  3. define member functions of sinus
  4. define member functions of cosinus
Note that no forward declarations are required because neither of the classes sinus and cosinus use a pointer to the other one in there definition. Also note that all the member functions of both sinus and cosinus are defined in the presence of definitions of both sinus and cosinus.

This is one of the reasons the standard way to implement a class is using 1 header and 1 source file for the class. The header contains the class definition and source file the member function definitions allowing the inclusion of headers for any other class used.
Aug 19 '09 #2
mirketiger
6 New Member
Thanks man. It worked :)
Aug 25 '09 #3

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

Similar topics

45
3618
by: Steven T. Hatton | last post by:
This is a purely *hypothetical* question. That means, it's /pretend/, CP. ;-) If you were forced at gunpoint to put all your code in classes, rather than in namespace scope (obviously classes themselves are an exception to this), and 'bootstrap' your program by instantiating a single application object in main(), would that place any limitations on what you could accomplish with your program? Are there any benefits to doing things that...
6
2700
by: Robert | last post by:
Hello. I have been trying out the Lebans ToolTip Classes at http://www.lebans.com/tooltip.htm, to display "balloon" style help tips in a form. The classes I am using are located at http://www.lebans.com/DownloadFiles/A2kTooltip.zip So far the classes work perfectly, except that now I need to extend it to support other controls besides the ones given in the example form. I have gotten it to work with some controls, but not others. I...
30
2512
by: Frank Rizzo | last post by:
We are having one of those religious debates at work: Interfaces vs Classes. My take is that Classes give you more flexibility. You can enforce a contract on the descendant classes by marking methods abstract (which is all that an interface does). In addition, the classes allow you to be flexible by adding functionality that child classes inherit. Or by providing optional functionality via virtual methods. Now, I understand that...
8
16917
by: Robert W. | last post by:
I've almost completed building a Model-View-Controller but have run into a snag. When an event is fired on a form control I want to automatically updated the "connnected" property in the Model. This works fine if all of the properties are at the top (root level) of the model but I'd like to keep them in nested classes to organize them better. So, for example, part of my data model looks like this (simplified) : public class MainClass
3
2984
by: Dave | last post by:
Please - anyone that can help. I am getting confusing results while trying to expose a collection from a web service. I have a webservice in which a web method accepts a collection as a parameter and returns an array of datasets. The collection consists of database connection objects. Based on the simple hierarchy below, you can see that starting with the
5
2006
by: Chris Szabo | last post by:
Good afternoon everyone. I'm running into a problem deserializing a stream using the XmlSerializer. A stored procedure returns the following from SQL Server: <Student StudentId="1" Status="1" Gpa="3.50"> <Person Id="1" FirstName="FirstName0" LastName="LastName0" MiddleInitial="W"/> </Student> In my code, person is the base class and student extends it. When I
6
2944
by: ivan.leben | last post by:
I want to write a Mesh class using half-edges. This class uses three other classes: Vertex, HalfEdge and Face. These classes should be linked properly in the process of building up the mesh by calling Mesh class functions. Let's say they point to each other like this: class Vertex { HalfEdge *edge; }; class HalfEdge { Vertex* vert;
2
35611
weaknessforcats
by: weaknessforcats | last post by:
Handle Classes Handle classes, also called Envelope or Cheshire Cat classes, are part of the Bridge design pattern. The objective of the Bridge pattern is to separate the abstraction from the implementation so the two can vary independently. Handle classes usually contain a pointer to the object implementation. The Handle object is used rather than the implemented object. This leaves the implemented object free to change without affecting...
12
11107
by: raylopez99 | last post by:
Keywords: scope resolution, passing classes between parent and child forms, parameter constructor method, normal constructor, default constructor, forward reference, sharing classes between forms. Here is a newbie mistake that I found myself doing (as a newbie), and that even a master programmer, the guru of this forum, Jon Skeet, missed! (He knows this I'm sure, but just didn't think this was my problem; LOL, I am needling him) If...
45
3009
by: =?Utf-8?B?QmV0aA==?= | last post by:
Hello. I'm trying to find another way to share an instance of an object with other classes. I started by passing the instance to the other class's constructor, like this: Friend Class clsData Private m_objSQLClient As clsSQLClient Private m_objUsers As clsUsers
0
9869
jinu1996
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...
1
9838
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9708
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 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...
1
7242
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 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...
0
6534
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();...
0
5140
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5302
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3354
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2665
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.