473,689 Members | 2,920 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Split class over 2 files

Hi,

I want to create a class with its definition in cls.h and
implementation in cls.cpp I get errors about redfinitions and errors
wahtever way I try to initialise the static proivate variable
*log_file.

// cls.h:

class logger
{
public:
void create(char *log_file_name) ;
};

// cls.cpp
class logger
{
public:
void create(char *log_file_name)
{
srtcpy *log_file_name, *log_file);
}
private:
static char *log_file;
};

What is the right way to split this so that compiler knows what I am
trying to do?

Oct 23 '06 #1
7 2336
In article <11************ **********@i3g2 000cwc.googlegr oups.com>,
"pkirk25" <pk****@kirks.n etwrote:
Hi,

I want to create a class with its definition in cls.h and
implementation in cls.cpp I get errors about redfinitions and errors
wahtever way I try to initialise the static proivate variable
*log_file.

// cls.h:

class logger
{
public:
void create(char *log_file_name) ;
};

// cls.cpp
class logger
{
public:
void create(char *log_file_name)
{
srtcpy *log_file_name, *log_file);
}
private:
static char *log_file;
};

What is the right way to split this so that compiler knows what I am
trying to do?
Put include guards in your header file.
http://en.wikipedia.org/wiki/Include_guard

--
To send me email, put "sheltie" in the subject.
Oct 23 '06 #2
pkirk25 wrote:
Hi,

I want to create a class with its definition in cls.h and
implementation in cls.cpp I get errors about redfinitions and errors
wahtever way I try to initialise the static proivate variable
*log_file.

// cls.h:

class logger
{
public:
void create(char *log_file_name) ;
};

// cls.cpp
class logger
{
public:
void create(char *log_file_name)
{
srtcpy *log_file_name, *log_file);
}
private:
static char *log_file;
};

What is the right way to split this so that compiler knows what I am
trying to do?
The reason you're getting errors about "redefiniti ons" is because you have
indeed defined the class twice. You need to use a different syntax in the cpp
file for implementing the class' member functions.

I'm pretty certain you don't want log_file to be a static variable. (Do you?
What static means in the context of a class is that every class shares that
single variable - so in this case, every logger you create will automatically be
writing to the same file.)

The way you should split your code is like this:

// cls.h

class logger
{
public:
void create(char *log_file_name)
private:
char *log_file;
};

// cls.cpp

void logger::create( char *log_file_name)
{
srtcpy (*log_file_name , *log_file);
}

There are two significant issues with this code. The first is that you've used a
char* to store text: this is very hard, and your code is buggy as a result.
The first bug is that you're passing *log_file_name to the function strtcpy:
this is the value _stored_ at the address log_file_name, and not the _actual
address_, which is what it will certainly be expecting - there's the same thing
with *log_file - to do that you would write srtcpy (log_file_name, log_file).

However, this is still buggy: at the moment, log_file hasn't been initialised
yet, and so doesn't point to anything - or rather, it probably points to some
random piece of memory. This is very bad. I could show you how to sort this out
as well, but in fact there's a much better solution: std::string, in the
standard library, which handles all text processing much better and more easily
than you or I could ever hope to do. Rewritten with std::string your code would
look like this:

// cls.h

#include <string>

using std::string;

class logger
{
public:
void create(string const& log_file_name)
private:
string log_file;
};

// cls.cpp

void logger::create( string const& log_file_name)
{
// guarantees log_file gets its own bit of memory
log_file = log_file_name.s ubstr();
}

A good reference on strings can be found here:
http://www.camtp.uni-mb.si/books/Thi...Chapter01.html

However, there's still more improvements. Have you studied constructors yet? A
constructor is essentially a built-in create function. I won't go into detail
here, but again Thinking in C++ is an excellent reference:
http://www.camtp.uni-mb.si/books/C++/Chapter06.html

Rewritten with a proper constructor, your code becomes this:

// cls.h

#include <string>

using std::string;

class logger
{
public:
logger()
logger(string const& log_file_name)
private:
string log_file;
};

// cls.cpp

logger::logger( )
: log_file ("default.lo g")
{
}

logger::logger( string const& log_file_name)
: log_file (log_file_name. substr());
{
}

Having done that, you can now declare a new logger very straightforward ly:
logger MyLogger("error s.log");

This is quite a lot of information - hope some of it helps...

Tom
Oct 23 '06 #3

pkirk25 wrote:
Hi,

I want to create a class with its definition in cls.h and
implementation in cls.cpp I get errors about redfinitions and errors
wahtever way I try to initialise the static proivate variable
*log_file.

// cls.h:

class logger
{
public:
void create(char *log_file_name) ;
};

// cls.cpp
class logger
{
public:
void create(char *log_file_name)
{
srtcpy *log_file_name, *log_file);
}
private:
static char *log_file;
};
I can't understand why you write code like that,you defined class
logger twice
Why not void logger::create
>
What is the right way to split this so that compiler knows what I am
trying to do?
..

Oct 23 '06 #4
Tom,

Wow - thanks! I was completely messign it up.

Mind if I pick your brain further?

I want to have the logger class have a member function called write()
that is overlaoded.

write(char *file_name, string message)
write(string message)

I thought using the static term would mean that if I created an
instance called myLog whcih writes to "new_log.ht ml", any other class
could append to the same logfile just by using the write(string
message) function.

A file name used by fstream shoudl be in the char *file_name format
which is only reason I use it.

What is best way to have the filename persist if static is not right?

Patrick

Oct 23 '06 #5

pkirk25 wrote:
Tom,

Wow - thanks! I was completely messign it up.

Mind if I pick your brain further?

I want to have the logger class have a member function called write()
that is overlaoded.

write(char *file_name, string message)
write(string message)
the above are not functions, if you properly make them functions you
can simply add them to your interface and implementation. Note that you
don't need the first write since you have presumeably already loaded
the filename using the def ctor or parametized ctor.

// logger.h
#ifndef LOGGER_H_
#define LOGGER_H_ /*include guards */
#include <string>

class logger
{
std::string log_file;
public:
logger();
logger(const std::string& log_file_name);
void write(std::stri ng& msg) const;
};

#endif /* LOGGER_H_ include guard */
___
// cls.cpp
#include "logger.h"
using std::string;

logger::logger( ) : log_file ("default.lo g")
{
}

logger::logger( const string& log_file_name)
: log_file (log_file_name) ;
{
}

void logger::write(c onst string& msg) const
{
// do stuff here (ie: open a std::ofstream to log_file and << msg)
}
>
I thought using the static term would mean that if I created an
instance called myLog whcih writes to "new_log.ht ml", any other class
could append to the same logfile just by using the write(string
message) function.
Not neccessarily. Static means that the object persists in global space
where *only* static functions can access static objects. That
complicates your class's interfaces because you now would need a static
function everywhere you want to access the static logger. Also, static
does not mean persist accross compilation boundaries. You could very
well end up with distinct loggers in seperate compilation units. You'ld
have to make the logger static and extern(al). Why deal with such a
mess?
Imagine a static logger overhere writing to the same file as a logger
overthere?
You also want to consider the implications of having to "couple" your
other classes.
Where a bunch of objects rely on some static logger to be available in
order for an instance of that class to do anything. It gets nasty.
>
A file name used by fstream shoudl be in the char *file_name format
which is only reason I use it.
a std::string variable can return a constant char* easily.

std::string filename("log.t xt");
const char* = filename.c_str( );
>
What is best way to have the filename persist if static is not right?
Instead of making it static, just initialize it in main and let main's
scope "persist" it.

#include "logger.h"

int main()
{
logged thelog("log.txt ");

// do whatever including thelog.write( ... )
// or let other objects take a reference to your logger
// window.logit(th elog);

} // thelog gets zapped here

Oct 23 '06 #6
pkirk25 wrote:
Hi,

I want to create a class with its definition in cls.h and
implementation in cls.cpp I get errors about redfinitions and errors
wahtever way I try to initialise the static proivate variable
*log_file.

// cls.h:

class logger
{
public:
void create(char *log_file_name) ;
};

// cls.cpp
class logger
{
public:
void create(char *log_file_name)
{
srtcpy *log_file_name, *log_file);
}
private:
static char *log_file;
};

What is the right way to split this so that compiler knows what I am
trying to do?

Did you mean:

void create(char* log_file_name)
{
strcpy(log_file , log_file_name);
}

This would copy the log_file_name into log_file.
But until log_file is uninitialised, the code
will crash.

It's better to make log_file an array:

char log_file[MAX_FILENAME_LE NGTH];

Even better: using std::string class for strings ;)

Best regards,
-Martin



Oct 23 '06 #7
Learnt a lot here - thanks all.

Oct 24 '06 #8

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

Similar topics

6
2581
by: Srinivas Kadiyala | last post by:
Hi, I have a form that has many controls and related actions. My <form.cs> file has crossed 13,500 lines of code. It takes quite a long to load the form or .cs file. I want to divide into small portions. I am thinking of dividing it module-wise. I want to write the event handling functions of a specific module in another file. Is that possible? Can we write the event handler in another file? Is there any better way to handle this situation?...
12
4033
by: Xah Lee | last post by:
Python Doc Problem Example Quote from: http://docs.python.org/lib/module-os.path.html ---------- split( path) Split the pathname path into a pair, (head, tail) where tail is the last pathname component and head is everything leading up to that. The tail part will never contain a slash; if path ends in a slash, tail will be empty. If there is no slash in path, head will be empty. If
2
6674
by: Jen | last post by:
Trying to take one table in access and split it into multiple excel files(using an excel template); and then email based on email addresses in Table2; Of course, I would like to do all of this with minimum user-interface...If there is anyone out there that can help... please feel free to share!:) PS... Using MSOffice 2000 Example: Table1
3
8563
by: Krish | last post by:
I have requirement, that i get one big chunk of text file. This text file will have has information, that on finding "****End of Information****", i have to split them individual text file with our naming standard (unique id) and create them designated folder. This requirement should be created as a batch job and preferrably this job should monitor the folder where one big chunk of text file lands and process them immediately. ...
2
3142
by: jeremy.figgins | last post by:
Hi, I have a class that is fairly large and I would like to split the file into two files, but still use only one class. What is the best way to accomplish this? Thanks!
3
1681
by: jerry.levan | last post by:
Hi, Is it possible to split a Class definition over two or more text files? (if so, how:) Jerry
2
3608
by: Curious Joe | last post by:
I have some files that are anywhere from 3GB to 9GB and I need to split them down to a series of smaller files similar to what the "split" command in linux can do. Unfortunately, I do not have access to a linux machine right now. I have been told that a program could be written in C/C++ that would do this very quickly. Can anyone point me to a tutorial or how-to that will teach me to write a quick program do accomplish this? for...
5
5202
by: D_a_n_i_e_l | last post by:
Is it possible to split a class definition over two files? Thanks.
7
4378
by: John Smith | last post by:
Hi, I am very new to C# and NET framework. I am trying to hash (using MD5CryptoServiceProvider) a source that is split into several files. Now when the source is in one file I can produce the correct md5 hash. My issue is how can I reproduce the correct hash when the file is split into different files.
0
8594
marktang
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...
0
9076
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, 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...
0
8786
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...
0
7621
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
6454
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
5810
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
4547
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2965
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
2219
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.