<ctime> is just the standard C++ library's version of the standard C library's <time.h> (same goes for <cmath>, <cassert> etc). If you want to find out what each include does,monstercameron said:ctime..............for the current time?
just comment it out and let the compiler tell you what is missing afterwards
The address size depends on the platform you are compiling for - these days it is either 32 or 64 bit. It is a good practicemonstercameron said:const string& url..?
32-bit address.....?
to not make any assumptions on the bit size of a type, unless it is actually part of the type's specification (such as SDL's Uint32 for example).
This construct is called a "guard" and is a good practice to do in header files. It prevents the declarations in the header file from being duplicated if the header file ismonstercameron said:#ifndef RUNTIME_H_INCLUDED
#define RUNTIME_H_INCLUDED
...
#endif
was there when i created an header file with codeblocks
included several times in one "compilation unit" (ie a .cpp file and all .h files it includes recursively).
monstercameron said:a. so you said its better to declare variables in teh header files?
b. and is it also better to add all functions in header also...or have each functions have its won header file?
What you generally want to do is put only declarations in your header files, and put the definitions in a source file:
runtime.h
Code:
#ifndef RUNTIME_H_INCLUDED
#define RUNTIME_H_INCLUDED
#include <fstream>
#include <string>
using namespace std;
// (Actually only variables that need to be accessed from outside runtime.cpp should be put here)
extern int running;
extern string motd;
extern string CUSinput;
extern string help;
extern string version;
extern string currentkey;
extern string statement;
extern string url;
extern ofstream myfile;
extern string opentextnow;
extern string MYbook;
extern string whatnext;
int callouts();
int runmetest2();
int readmetest();
#endif // RUNTIME_H_INCLUDED
runtime.cpp
Code:
#include "runtime.h"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <ctime>
#include <fstream>
using namespace std;
int running = 1;
string motd = "Welcome To My Language Interpreter...English!";
string CUSinput;
string help = "not yrt integrated";
string version = "0.0.1";
string currentkey;
string statement;
string url;
ofstream myfile;
string opentextnow;
string MYbook;
string whatnext = "yes";
int callouts()
{
if (CUSinput == "motd")
cout << endl << motd;
if (CUSinput == "version")
cout << endl << version;
/*
if (CUSinput == "time")
cout << endl << "the time is now: " << ctime;
*/
if (CUSinput == "help")
cout << endl << help;
system("clear");
return 0;
}
/*
int opentext(string url)
{
myfile.open (url.c_str());
cout << " opening...";
myfile.close();
return 0;
}
*/
/*
int runmetest()
{
if (CUSinput == "statement")
{
cout << "tell me what to save: ";
cin >> statement;
//ofstream myfile;
//myfile.open ("book.eng");
cout << statement << " saving...";
myfile << statement;
myfile.close();
}
return 0;
}
*/
int runmetest2()
{
if (CUSinput == "statement")
{
cout << "tell me what to open: ";
cin >> url;
cout << "tell me what to save: ";
cin >> statement;
ofstream myfile;
myfile.open (url.c_str());
cout << " opening...";
cout << statement << " saving...";
myfile << statement;
myfile.close();
}
return 0;
}
int readmetest()
{
if (CUSinput == "open")
{
cout << "tell me what to open: ";
cin >> url;
ifstream myfileread;
myfileread.open (url.c_str());
cout << " opening...";
while(!myfileread.eof())
{
getline (myfileread,MYbook);
while(whatnext == "yes")
{
cout << MYbook << endl;
cin >> whatnext;
}
whatnext = "yes";
}
myfileread.close();
}
return 0;
}
You may think of the "#include" directive as "copy and paste", so
Code:
// runtime.h
int my_function()
{
return 123;
}
//main.cpp:
#include "runtime.h"
[...]
//other.cpp:
#include "runtime.h"
[...]
... is the same as:
Code:
//main.cpp:
int my_function()
{
return 123;
}
[...]
//other.cpp:
int my_function()
{
return 123;
}
[...]
You can compile both "main.cpp" and "other.cpp", but when you link both "main.o" and "other.o" into your program, you will in the best case get a linker error, because there is a name collision. In the worst case, you will not
get a linker error but strange crashes that are rather difficult to debug.
If you split the code into "runtime.h" and "runtime.cpp", the physical code produced from the definitions will only exist once after compilation - in "runtime.o". The remaining declarations in "runtime.h" are pure information, so there is no physical representation of it that could be duplicated in the compiled program.
Last edited by a moderator: