#### Archived

This topic is now archived and is closed to further replies.

# Buggy Class or is VC++ a little lost?

This topic is 5821 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello everyone, I''m having a bit of trouble with the log file class that I am attempting to make. I''ve gone over it a few times now and it seems to be just fine, but VC++ insists on telling my that it is messed up. Mind taking a look and teling me what''s up? The error that I am receiving is: "log.h(28) : error C2011: ''CLog'' : ''class'' type redefinition" Here is my log class:
  // Log.h ------------------------------------------------------- #ifndef _LOG_H_ #define _LOH_H_ //-------------------------------------------------------------- // INCLUDES //-------------------------------------------------------------- #include #include #include //-------------------------------------------------------------- // CLASS - CLOG //-------------------------------------------------------------- class CLog { // Error seems to occur on this line public: Create(char *Filename); Log(char *Message); Destroy(); private: ofstream LogFile; }; #endif // Log.cpp ----------------------------------------------------- //-------------------------------------------------------------- // INCLUDES //-------------------------------------------------------------- #include #include #include #include "Log.h" //-------------------------------------------------------------- // Method: Create() // Desc: Creates the Log File and Opens it for Writing. //-------------------------------------------------------------- CLog::Create(char *Filename) { // Open the Log File LogFile.open(Filename, ios::out); } //-------------------------------------------------------------- // Method: Log() // Desc: Outputs the Message to the Log File. //-------------------------------------------------------------- CLog::Log(char *Message) { LogFile.write(Message, strlen(Message)); } //-------------------------------------------------------------- // Method: Destroy() // Desc: Closes the Log File. //-------------------------------------------------------------- CLog::Destroy() { LogFile.close(); } 
The class is so simple that it should work rather well. Any ideas? Thanks for any help.

##### Share on other sites
You don''t have any return types defined for the functions. When I did that it asked me to return a value because that was what it assumed the functions would do. I couldn''t regenerate that exact error from your source though.

##### Share on other sites
Did you inlcude the cpp in another file? That could cause problems... Include the header and add the cpp to the project.

Magmai Kai Holmlor

"Oh, like you''ve never written buggy code" - Lee

"What I see is a system that _could do anything - but currently does nothing !" - Anonymous CEO

#ifndef _LOG_H_
#define _LOH_H_

##### Share on other sites
Zipster''s right. You need to do something like this:
//Log.h

void Create(char *Filename); //returns void, replace with what
//you like.

//Log.cpp
void CLog::Create(char *Filename){ // Open the Log File LogFile.open(Filename, ios::out);
}

Now the function is declared correctly. It might be (?) good to change void to something like BOOL or RETCODE, just to make sure everything goes well.

##### Share on other sites
*ahem*

Take a look at the anon post.

D.V.

Carpe Diem

##### Share on other sites

Well I''m feeling a little stupid now. Thanks for pointing out the little mistake, it is working perfectly now. Guess I''m gonna have to smack my proofreader around a bit for missing something that simple

As well I have added return types to the member functions, better to be safe than sorry.

Thanks a lot guys.

##### Share on other sites
If you don''t specify a return type, its return type is int.

Next time you post a title like this:
quote:

Buggy Class or is VC++ a little lost?

http://support.microsoft.com/default.aspx?scid=kb;EN-US;q243451

If the problem isn''t listed on the page, it''s your fault.

##### Share on other sites
quote:
Original post by Stoffel
If you don''t specify a return type, its return type is int.

Since he is using a ''class'', then he must be using C++ and C++ requires a return type, there is no default.

quote:
Quote from ''Thinking in C++ Vol 1'' by Bruce Eckel
A C++ function prototype must specify the return value type of the function (in C, if you leave off the return value type it defaults to int).

---
Make it work.
Make it fast.

##### Share on other sites
"C++ requires a return type, there is no default"

Absolutely incorrect

##### Share on other sites
use #pragma once
instead of the #ifndef trick, and you can avoid that problem in the future

##### Share on other sites
quote:
Original post by Magmai Kai Holmlor
use #pragma once
instead of the #ifndef trick, and you can avoid that problem in the future

I believe the #pragma trick is a VC++ specific thing and non-standard. (But I could be wrong since I'm not going to through the trouble of looking up the standard to be sure. ) Of course, this doesn't matter too much if you never plan on porting your code to another platform. It is generally bad to teach yourself non-standard ways of doing things, however.

Edited by - jaxson on January 3, 2002 4:29:11 AM

• ### Forum Statistics

• Total Topics
628638
• Total Posts
2983970

• 10
• 18
• 20
• 13
• 9