473,805 Members | 2,281 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

class hate

Alright .... this makes no sense ...

Declared a class 'diskStorage' in a header ...diskStorage. h
Defined it's contructor and methods ... in diskStorage.cpp

Included diskStorage header in a another class ...(server.h)
and then tried to declare a variable of type 'diskStorage' ...
like this ( inside server.h )

diskStorage local_disk;

this is what I get
"error: no matching function for call to `diskStorage::d iskStorage()"

Now, when I do this in the second class header file:
diskStorage* local_disk;

and then in the second class c o n t r u c t o r:
local_disk= new diskStorage(cyl inders,tracks,t rack_time);

it compiles and behaves....exce pt ...
calling local_disk->get_info() returns what it should o n l y in the
'server' class contructor.

when I call the local_disk->get_info() in a method that is part of
server class and not in it's constructor.... it returns garbage ...

I'm guessing the local_disk in the second case went out of scope.
But why does it not like me declaring a variable of type 'diskStorage'
in the first place?

Do I need to do this in the header file
diskStorage local_disk(); ////// would this not be a function !? and
not a variable

Dunno ...
Any ideas ?
---- server.h -----

#include "diskStorag e.h"

class server
{
private:
diskStorage local_disk; // should server not know what
diskStorage is ?
}
---- diskStorage.h-------
class diskStorage {
protected:
u_long _cylinder_count ;
u_long _sector_count;
u_long _track_time;
unsigned char geometry[12];

public:
//! class constructor
diskStorage(u_l ong cylinders,u_lon g sectors,u_long track_time);
~diskStorage();

void get_info();
};
--- diskStorage.cpp ----

#include "diskStorag e.h"

diskStorage::di skStorage(u_lon g cylinders, u_long sectors, u_long
track_time)
{
_cylinder_count = cylinders;
_sector_count = sectors;
_track_time = track_time;

cout<<"Cylinder s :"<< _cylinder_count <<endl;
cout<<"Sectors :" << _sector_count <<endl;
}
--- server.cpp---

server::server( u_long cylinders, u_long sectors, u_long track_time)
{
diskStorage local_disk(cyli nders,sectors,t rack_time);//this does not
work
local_disk = new diskStorage(cyl inders,sectors, track_time);//this does
work

}

I was sure I could declare a variable of any type in a header and
initialize it in elsewhere
Just like I can with any data type...but I seem to be wrong ...
Or just looked at the code for too long and missing something trivial.
These are of course just parts of the code but the rest is not relevant
and quite a mess.

The questions you may ask:
Q: Why am I separating class declarations and definitions?
A: 'Cause I hope to work on a project big enough (some day) where that
makes sense and am trying to get used to the idea.

Q: Why did I not look for examples ?
A: I did, but found nothing similar.

Q: Am I just trying to have someone else do my HW?
A: No, I need to learn b e f o r e someone asks me to do this (at
work).

btw. gdb tracing did not break where I told it to ... so now I'm
completely lost :-)

Nov 22 '06 #1
17 1582
On 21 Nov 2006 17:19:05 -0800 in comp.lang.c++, "Amchi"
<am*********@gm ail.comwrote,
>diskStorage local_disk;

this is what I get
"error: no matching function for call to `diskStorage::d iskStorage()"
diskstorage has no constructor that takes no arguments (also known as a
'default' constructor.) For a simple diskstorage variable as written,
you must supply arguments for initialization.

diskStorage local_disk(cyli nders,tracks,tr ack_time);
>Do I need to do this in the header file
diskStorage local_disk(); ////// would this not be a function !? and
not a variable
You're right, that's a function.

The class member is:

diskStorage local_disk;

It's initialized in the constructor initialization list as

server::server( u_long cylinders, u_long sectors, u_long track_time)
: local_disk(cyli nders,sectors,t rack_time)
{
...

This issue is covered in Marshall Cline's C++ FAQ. See the topic
"[10.6] Should my constructors use "initializa tion lists" or
"assignment "?" It is always good to check the FAQ before posting.
You can read the FAQ at:
http://www.parashift.com/c++-faq-lite/

Nov 22 '06 #2
Thank you.
Worked around it by changing the constructor to be empty and defined a
'mutator' method.

I appreciate you taking time to help.
Thanks again.
David Harmon wrote:
On 21 Nov 2006 17:19:05 -0800 in comp.lang.c++, "Amchi"
<am*********@gm ail.comwrote,
diskStorage local_disk;

this is what I get
"error: no matching function for call to `diskStorage::d iskStorage()"

diskstorage has no constructor that takes no arguments (also known as a
'default' constructor.) For a simple diskstorage variable as written,
you must supply arguments for initialization.

diskStorage local_disk(cyli nders,tracks,tr ack_time);
Do I need to do this in the header file
diskStorage local_disk(); ////// would this not be a function !? and
not a variable

You're right, that's a function.

The class member is:

diskStorage local_disk;

It's initialized in the constructor initialization list as

server::server( u_long cylinders, u_long sectors, u_long track_time)
: local_disk(cyli nders,sectors,t rack_time)
{
...

This issue is covered in Marshall Cline's C++ FAQ. See the topic
"[10.6] Should my constructors use "initializa tion lists" or
"assignment "?" It is always good to check the FAQ before posting.
You can read the FAQ at:
http://www.parashift.com/c++-faq-lite/
Nov 22 '06 #3
On 22 Nov, 04:23, "Amchi" <amer.ter...@gm ail.comwrote:
Thank you.
Worked around it by changing the constructor to be empty and defined a
'mutator' method.
I'd like to point out (if you did not already know) that you can have
two (or more) constructors, one that takes no arguments, and one that
does. So you could keep your constructor and just create a new one that
creates a diskStorage with some default values.

PS. Next time, write your reply below that which you are replying to.
People get annoyed if you don't.

--
Erik Wikström

Nov 22 '06 #4

er****@student. chalmers.se wrote:
On 22 Nov, 04:23, "Amchi" <amer.ter...@gm ail.comwrote:
Thank you.
Worked around it by changing the constructor to be empty and defined a
'mutator' method.

I'd like to point out (if you did not already know) that you can have
two (or more) constructors, one that takes no arguments, and one that
does. So you could keep your constructor and just create a new one that
creates a diskStorage with some default values.
Had a nightmare about it and in the middle of the night realized I
could have done that.
Stepping back and forgetting about it for a moment would have helped.
PS. Next time, write your reply below that which you are replying to.
People get annoyed if you don't.
Ok. My bad.

Nov 22 '06 #5
---- server.h -----

#include "diskStorag e.h"

class server
{
private:
diskStorage local_disk; // should server not know what
diskStorage is ?
}

I don't know if you've omitted them because you're posting to a newsgroup,
but you should have inclusion guards in header files:

#ifndef INC_SERVER_HPP
#define INC_SERVER_HPP

/* Contents of Header file */

#endif

By the way, I advocate the "hpp" extension for C++ header files, an "h" for
C header files. (Same goes for "cpp" and "c").

---- diskStorage.h-------
class diskStorage {
protected:
u_long _cylinder_count ;
u_long _sector_count;
u_long _track_time;
unsigned char geometry[12];

public:
//! class constructor
diskStorage(u_l ong cylinders,u_lon g sectors,u_long track_time);
~diskStorage();

void get_info();
};

--- diskStorage.cpp ----

#include "diskStorag e.h"

diskStorage::di skStorage(u_lon g cylinders, u_long sectors, u_long
track_time)
{
_cylinder_count = cylinders;
_sector_count = sectors;
_track_time = track_time;

cout<<"Cylinder s :"<< _cylinder_count <<endl;
cout<<"Sectors :" << _sector_count <<endl;
}

You might prefer use of an "constructo r initialiser list" here. If you
don't know what that is, Google for it.

--

Frederick Gotham
Nov 22 '06 #6
Frederick Gotham schrieb:
>---- server.h -----

#include "diskStorag e.h"

class server
{
private:
diskStorage local_disk; // should server not know what
diskStorage is ?
}


I don't know if you've omitted them because you're posting to a newsgroup,
but you should have inclusion guards in header files:

#ifndef INC_SERVER_HPP
#define INC_SERVER_HPP

/* Contents of Header file */

#endif

By the way, I advocate the "hpp" extension for C++ header files, an "h" for
C header files. (Same goes for "cpp" and "c").
Yes, this is absolutely wise.
By the way, to add some confusion to a C++ learner, let me add that in
the above case (in "server.h") you could (and imho should) use a class
forward declaration. If a class name is used only as parameter or return
value or member it is not necessary to include the header, it's ok to
add a line
class diskStorage;
and the compiler is perfectly happy. Try it ;)
Some of my colleagues swear that this makes loading time shorter, but I
never noticed that - maybe my projects were too few files....

Jan
Nov 23 '06 #7
Jan Bornschlegel wrote:
Frederick Gotham schrieb:
>>---- server.h -----

#include "diskStorag e.h"

class server
{
private:
diskStorage local_disk; // should server not know what
diskStorage is ?
}


I don't know if you've omitted them because you're posting to a
newsgroup, but you should have inclusion guards in header files:

#ifndef INC_SERVER_HPP
#define INC_SERVER_HPP

/* Contents of Header file */

#endif

By the way, I advocate the "hpp" extension for C++ header files, an
"h" for C header files. (Same goes for "cpp" and "c").

Yes, this is absolutely wise.
By the way, to add some confusion to a C++ learner, let me add that in
the above case (in "server.h") you could (and imho should) use a class
forward declaration. If a class name is used only as parameter or return
value or member it is not necessary to include the header, it's ok to
add a line
Not quite true. If it's used as a param, return value, or *pointer or
reference* member, it's not necessary to include the header. If it's
used as a regular member, it is necessary. Otherwise, how would the
compiler know how much memory to allocate for (in the example) the servr
class?
Nov 23 '06 #8

Frederick Gotham wrote:
By the way, I advocate the "hpp" extension for C++ header files, an "h" for
C header files. (Same goes for "cpp" and "c").
I use .hpp for exported headers, .hxx for private headers.

A private header is one that is not used outside of its module. (A
module here meaning a group of classes that implement functionality
that are reasonably well coupled together).

Nov 23 '06 #9

Amchi wrote:

< snip down to the code >
>
---- server.h -----

#include "diskStorag e.h"

class server
{
private:
diskStorage local_disk; // should server not know what diskStorage is ?
}
I don't know at this point what diskStorage is or what server is but
this class hides nothing because anyone including server.h is
indirectly including diskStorage.h as well.

Better is generally to reduce the coupling by using a foward
declaration. This is often done with a "pImpl" class.

If using boost, one way here is to use scoped_ptr so:

class server
{
private:
boost::scoped_p tr< diskStorage local_disk;

// plus public interface, of course
};

You have to include the boost/scoped_ptr header to do this but you can
forwardly declare diskStorage here, including the header only in the
server.cpp.

Assuming that everyone is using boost regularly, it will not be
increasing the coupling by including the boost header.

If you do not want to add a dependency on boost for those using the
class (eg you are exporting it) then you can use a raw pointer, but you
have to manage it yourself (as well as managing copying and
assignment).
---- diskStorage.h-------
class diskStorage {
protected:
u_long _cylinder_count ;
u_long _sector_count;
u_long _track_time;
unsigned char geometry[12];
These should probably be private rather than protected. Magic numbers
(like 12) should normally be "enum"ed.
The questions you may ask:
Q: Why am I separating class declarations and definitions?
A: 'Cause I hope to work on a project big enough (some day) where that
makes sense and am trying to get used to the idea.
And also it's usually a sensible thing to do. The only main exceptions
are
1. when writing templates (because most compilers don't support export)
2. Writing something very generic that you want to be able to include
from multiple projects without having to link against anything
3. Writing a very trivial class, often a functor.

I also sometimes writing implementation classes with no header file at
all, and these are then sometimes exported as symbols but that's
another issue.

Nov 23 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
4546
by: Jon | last post by:
Hello all, I'm certain this question has been asked before; I was unsure what terms to search for within the newsgroup archive to find the proper answer I wanted. Hopefully someone can point me to some relevant postings or websites. In any case, I want to create a geometry class that has some base functionalities. The problem I am particularly interested in is such:
13
2394
by: Bryan Parkoff | last post by:
I have created three classes according to my own design. First class is called CMain. It is the Top Class. Second class and third class are called CMemory and CMPU. They are the sub-classes. Two sub-classes have the relationship to communicate back and forth through this pointer. The pointer is responsible inside Top class for allocating and deallocating two sub-classes. CMemory class is responsible to allocate and deallocate memory...
3
8730
by: Bob | last post by:
In C++, default access is private. In Java, default access is package. In C++, default access is ___________ ??
0
853
by: Rick | last post by:
We have an class sent to us by a vendor in a .h file. This class controls access to a subsystem for gathering data. Currently we are using C++ 6.0 to access this class. Now we want to move to .net. The class sent to us can not be compiled as managed code so I have built a managed wrapper class... here is the general code #using <mscorlib.dll> #pragma once
16
2016
by: Allen | last post by:
I have a class that returns an arraylist. How do I fill a list box from what is returned? It returns customers which is a arraylist but I cant seem to get the stuff to fill a list box. I just learning and really need some help bad. Public Shared Function GetAll() As ArrayList Dim dsCustomer As New DataSet() Dim sqlQuery As String = "SELECT Name, Address, PhoneNo " & _ "FROM CustomerTable" Try
10
5502
by: Bonzol | last post by:
vb.net Hey there, could someone just tell me what the differnce is between classes and modules and when each one would be used compared to the other? Any help would be great Thanx in advance
5
2335
by: Slant | last post by:
Here's a question that most will have different answers to. I'm just dying to find a solution that seems halfway automated!! There really are two seperate issues which might be answered by the same answer. Web App: - constants.php begin - define("DB_HOST", "locahost"); .... to include the standard connection info define("DB_NAME", "mydatabase");
9
2101
by: jerry.upstatenyguy | last post by:
I am really stuck on this. I am trying to write a string array containing a "word" and a "definition" to a class called Entry. Ultimately this will end up in another class called dictionary. No, this is not a homework assignment. In fact it was a question on my class midterm that everyone bombed. Unfortunately we never covered this in class prior to the midterm. Anyway, please help if you would be so kind. Here is what I have. I...
7
1264
by: Tamagafk | last post by:
error_reporting(E_ALL); class cls{ var $val; function cls(){ $this->$val = 999; } } $c = new cls; With error_reporting set to E_ALL PHP floods me with notifications that
6
3571
by: Marvin Barley | last post by:
I have a class that throws exceptions in new initializer, and a static array of objects of this type. When something is wrong in initialization, CGI program crashes miserably. Debugging shows uncaught exception. How to catch an exception that happened before main() try { ... } catch (...) { ... } block? Is there a way?
0
9596
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10364
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...
0
9186
agi2029
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...
1
7649
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
6876
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
5545
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...
1
4328
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3849
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3008
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.