A
Amchi
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::diskStorage()"
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(cylinders,tracks,track_time);
it compiles and behaves....except ...
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 "diskStorage.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_long cylinders,u_long sectors,u_long track_time);
~diskStorage();
void get_info();
};
--- diskStorage.cpp ----
#include "diskStorage.h"
diskStorage::diskStorage(u_long cylinders, u_long sectors, u_long
track_time)
{
_cylinder_count = cylinders;
_sector_count = sectors;
_track_time = track_time;
cout<<"Cylinders :"<< _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(cylinders,sectors,track_time);//this does not
work
local_disk = new diskStorage(cylinders,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
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::diskStorage()"
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(cylinders,tracks,track_time);
it compiles and behaves....except ...
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 "diskStorage.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_long cylinders,u_long sectors,u_long track_time);
~diskStorage();
void get_info();
};
--- diskStorage.cpp ----
#include "diskStorage.h"
diskStorage::diskStorage(u_long cylinders, u_long sectors, u_long
track_time)
{
_cylinder_count = cylinders;
_sector_count = sectors;
_track_time = track_time;
cout<<"Cylinders :"<< _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(cylinders,sectors,track_time);//this does not
work
local_disk = new diskStorage(cylinders,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