• ### Announcements

#### Archived

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

# Need help on Incredibly wierd problem

## Recommended Posts

JoshG    127
Roight, I''ve had a thread that dealt with this problem earlier, But there was no help that came out of it, Partly because I don''t think I named the thread properly, and I didn''t supply all of the information at the start. So, Here is the code for the project. I''ll tell you the problem I am having with it at the end. Main.cpp:
  #include "BinaryTree.cpp" char *OOStrCat(char *one,char *two){ char *ret = (char*)malloc(strlen(one)+strlen(two)+1); strcpy(ret,one); strcpy(ret+strlen(one),two); return ret; } #include "Console.h" void main(){} 
Console.h:
  #ifndef MAX_CONSOLE_LEN #define MAX_CONSOLE_LEN 255 //Make sure the Dims of the Consol are defined #endif #ifndef MAX_CONSOLE_LINES #define MAX_CONSOLE_LINES 4 //Make sure the Dims of the Console are defined #endif #ifndef _OOConsoleDef #define _OOConsoleDef //Make sure that this Class doesn''t exist already class OOConsoleFunction; //------------------------ class OOConsole{ public: OOConsole(); ~OOConsole(); static void ConsoleIn(char*); //Allows input to the console static void ConsoleOut(char*); //Allows Output from console static void Register(OOConsoleFunction*); //Identifies new commands static void getBuffer(char*,int); //Retrieves Console output static void clearBuffer(); //Removes all Console Output static void clearLine(int); //Removes Output on a given line static void LoadFile(char*); //Loads and executes commands from a file static void Scan(char*,char**,char**); //Breaks up a commandline into words private: static char Lines[MAX_CONSOLE_LINES][MAX_CONSOLE_LEN]; //Array containing Output static BTree *FuncMap; //Map of all commands static int LinesFilled; //Position of Last Output }; //------------------------ typedef void (*ConsoleFunction)(char*); //Definition of Console Command class OOConsoleFunction:public BTreeNode{ public: OOConsoleFunction(char*,char,ConsoleFunction); //Creates a Command for use in Console private: ConsoleFunction Function; //Pointer to a Command''s Function static OOConsole Console; //Global Console }; //------------------------ #endif 
Console.cpp:
  #include "Console.h" //Console Constructers & Destructers: OOConsole::OOConsole(){} OOConsole::~OOConsole(){} //Console Functions etc... 
ok, Those are all my files. I know the BTree class (which is defined in "BinaryTree.h" works fine, as I have used it at other times. The line that gets the error is in "Console.h" where is states: static BTree *functions; it complains on this line that it is missing an '';'', However, as you can see, there are all the '';'' it could possibly want. If I remove the ''*'' in the declaration then it seems to not bother with it...But I don''t see why it doesn''t like it in the first place so I don''t like to avoid the problem by compromising like that. Previously I had the code inlined in the one file, and thought it looked messy, so i copied & pasted the sections into the files "Console.h" and "Console.cpp". and now it doesn''t work. If I inline it again it works fine. Any help is appreciated --Josh

##### Share on other sites
First off, your inclusion system is totally screwed. There is only one way to do it, and that is this:

  #ifndef __MYCLASSNAME#define __MYCLASSNAME/*put your class DECLARATION here.*/#endif

  #include "classname.h" //or whatever/* put your class IMPLEMENTATION (definition) here.*/

  #include "classname.h"MyClassName ThisIsAnInstance...

If you do it any other way, you're headed for trouble. Give in and let yourself be assimilated.

Later,
ZE.

EDIT: Something hosed my source tags...

Edited by - ZealousElixir on January 3, 2002 10:57:27 PM

##### Share on other sites
JoshG    127
Yeah, I did that, I''m fairly sure,
But take a look at the "Console.h" file, the forum has mucked up the formatting a bit on the code and put the "#endif" at the end of the lines and not on new lines.

Is this what you were getting at or have I actually done something different?

--Josh

##### Share on other sites
I''ll make myself a tad clearer.

  #include "BinaryTree.cpp" // NEVER DO THIS!!!!!!!!!!!!

There.

Later,
ZE.

##### Share on other sites
JoshG    127
Well I''ll tell you also that this is the first time that I HAVN''T done that, And this is the time when it doesn''t work on me.

Will the BinaryTree thing really impact on this situation If I have used it before and it works fine?

--Josh

##### Share on other sites
quote:
Original post by JoshG
Well I''ll tell you also that this is the first time that I HAVN''T done that, And this is the time when it doesn''t work on me.

Then shut your mouth and do it that way again. What''s the problem here?

Later,
ZE.

##### Share on other sites
DanG    122
here is what i think you should do to start with as your code is incredibly conveluted.

Put each class in a .hpp or .h file looking like this:

#ifndef CLASSNAME
#define CLASSNAME

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

// if not then don''t do that

// now put basically this

class YourclassName
{
// variables, declarations
};

#endif

then in a .cpp file with the same name as the .h do this

#include <filename.hpp>

// implmentations of functions, static variable sets

#include <filename.hpp>

and start writing

further more, don''t do this

#ifndef MAX_CONSOLE_LEN
#define MAX_CONSOLE_LEN 255
#endif

this is not necessary for constants, you can just
#define MAX_CONSOLE_LEN 255
multiple times. Also it''s becoming more popular to use
const unsigned char MAX_CONSOLE_LEN = 255;
but its a matter of style.

Do this and repost with clearer code.

He who said money was the root of all evil knew little of the nature of money and less about the nature of man.

##### Share on other sites
JoshG    127
Ok, Lemme explain myself.

I AM trying to do what you have said. I recently learnt of this method and I am TRYING to do it, Console.h is the FIRST conversion to this method that I am trying to make. That is why "BinaryTree.cpp" is not done this way. I would like to get it working on ONE class before I go and screw up the rest of the Libraries of classes that I have made.

Ok. Now, I HAVE done what you have said. it was ALREADY done. and the reason I have used the:
#ifndef MAX_CONSOLE_LEN
#define MAX_CONSOLE_LEN 255
#endif

is so that I do not Overwrite any previous settings of that constant. Those values are only Default values and If I wish I can set them elsewhere, and the OTHER values will be used, NOT these ones.

But now the problem has changed. The code hasn''t though. It is completely the same. Now my Linker is giving out "Unresolved External" errors, which makes more sense (kinda) but still I don''t know how to fix it. Bearing in mind the code is STILL the same

##### Share on other sites
Mark Duffill    156
As far as I can see you need to either include BTree.h in the console.h file, or prototype the class BTree. This is since the decalaration

static BTree *FuncMap;

The compiler dosn't know what BTree is. So either

  #ifndef __CONSOLE_FILE__#define __CONSOLE_FILE__#include "BTree.h"... rest of console.h#endif

or

  #ifndef __CONSOLE_FILE__#define __CONSOLE_FILE__// forward declaration of BTree.// note only can have pointer to vars if do this...class BTree;... rest of console.h#endif

Then put BTree.cpp in your project so it gets compiled (then linked in).

Hope this is of some assistance.

[The views stated herein do not necessarily represent the view of the company Eurocom ]

Eurocom Entertainment Software
www.Eurocom.co.uk

The Jackal

Edited by - Mark Duffill on January 4, 2002 4:35:06 AM

##### Share on other sites
JoshG    127
Yes that works,

It reconignises the BTree now, and that error has gone.
But now, It can''t seem to find the implementation of my classes. It reads the Console.h file, But it says "Unresolved External error" and then mentions a few of the functions in my Console Class.

All these functions are accounted for in the .cpp file, But it claims that they arn''t!

Any help with this one?
--Josh

##### Share on other sites
Mark Duffill    156
Make sure you include the .cpp files in the project so they get compile in. ( then linked in ), if you dont it will say the functions/vars in these files are unresolved.

##### Share on other sites
JoshG    127
But I was lead to beleive that you Simply have to type:
#include "x.h"
and violla, You don''t need to worry about includeing the Cpp file. I beleive they said that when you include the .h then the "MakeFile" automatically adds the cpp file in for you, is this true? or am I completely mistaken!??

##### Share on other sites
Mark Duffill    156
When I said include the .cpp''s in your project I didn''t mean do

#include "xxx.cpp"

I mean''t in the actual project. This can be done in msdev either via the menu project->add to project->files, or right click on a folder in the project view. For other dev enviroments you''ll have to look at the help for how to do this.

##### Share on other sites
Or, if you are not using MSDEV, you have to compile each source file separately, then link them all together at the end. Or use make files to do it for you.

---
Make it work.
Make it fast.

##### Share on other sites
Excuse my ever-so-abrasive writing style. You took my harshness with dignity, and I appreciate that and appologize if I have offended.

Do tell us what compiler you''re using. If it isn''t VC or Dev-C++, there may be specific methods to do what you want to do.

Later,
ZE.

##### Share on other sites
JoshG    127
I''m using Borland C++ Builder.
I can add the cpp file to the "project" but originally I thought that was my problem. If I do that, then the project never makes it to the Linker, and it goes back to having the error that I mentioned at the beginning of this Thread

So, With the Makefile... Would I have to run this SEPARATLY to the compile? and then somehow combine it in the compile?

Thanks
--Josh

##### Share on other sites
Sounds like its time to start a clean project and structure your inclusion sceme better. You know how to protoype functions right? Well, that''s what the #include method stated above is sort of like too. You put only the information that your code needs in the header files, such as class definitions or constants. Then you implement the actual member functions in your .cpp file. Now, when you include the header file with your definitions in it your code will be able to interface with the functions you defined in the .cpp file. Are you using templates? If that''s the case, you have to put all of the code for it including member functions in the header file because templated code isn''t compiled until it is called.