does anyone know of any tutorials etc for creating and using your own header files?
Printable View
does anyone know of any tutorials etc for creating and using your own header files?
Make New Unit (in dev) and write whatever you want in it .. Then rename it into hello.h and in your application put #include "hello.h" (It has to be added to the project first) :)
yes i know that. but how would i call to it. or would it be like if i had a header file i made including tons of variables, it would be like the varibales were writtin into my main .cpp document?
If you've got a function in your header file call it this way:
function() .. just like all other ones :)
The only difference is that you dont make #include <file.h> put with quotes (") like #include "file.h" :)
Yep, thats pretty much how it goes. When you start including that same header in multiple files though, if you want them to use the see same variables you will need to use the extern keyword. If you want a simple example of a single header suited for a single cpp here goes..any questions on part of it, just ask.Quote:
Originally posted by Klinerr1
yes i know that. but how would i call to it. or would it be like if i had a header file i made including tons of variables, it would be like the varibales were writtin into my main .cpp document?
Code:#ifndef VECTOR_H
#define VECTOR_H
typedef struct _tagVector
{
float x;
float y;
float z;
}Vector3;
#endif /* VECTOR_H */
uhh that is not a library file, that is a header file.
In visual C++ in the new project menu, you choose Static Linked Library and write your code, hit the build button and voila you have a library.
wut im trying to do is put my intro for a game in a seperatefile. because its like 500 lines down. i want the intro to play before the game so would i pretend intro.h or wutever is a function and call it as if the function was named intro.h
Call it like all other functions but #include "intro.h" in main.cpp :)
why not put it in another .cpp file, and add it to your project, and prototype it in main.cpp (or whatever your main file is called)
oh stupid me
You're just starting :)
so is this right (im not usign my intro ni the header)
menuscreen_.cpp looks like thisPHP Code:
#include <iostream>
#include <string>
#include <stdlib.h>
#include <windows.h>
#include <conio.h>
#include <fstream>
#include "menuscreen_.cpp"
using namespace std;
int menuscreen_.cpp();
char * menuscreenv;
int gold, health, magic, attack, aim, defence, ynvari, charactertype;
string empty, weapon, shield, armor, szWord, szLine, player, item1, item2, item3, item4, item5, item6, item7, item8, item9, item10, item11, item12, item13, item14, item15, item16, item17, item18, item19, item20, item21, item22, item23, item24, item25, item26, item27, item28, item29, item30;
bool menuselect;
int main(int argc, char *argv[])
{
int menuscreen_.cpp();
cin.get();
return 0;
}
do i have to do anything if i want to use the same variables in both files? and have it so both variables hold the same data?PHP Code:
#include <iostream>
#include <string>
#include <stdlib.h>
#include <windows.h>
#include <conio.h>
#include <fstream>
int main(int argc, char *argv[])
{
cout << menuscreenv << endl;
cin >> menuselect;
if (menuscreen == 1){
}
else if (menuscreen == 2){
ofstream fout;
fout.open("stats.sav");
fout << "Player: " << player << " ";
fout << "CharacterType: " << charactertype << " ";
fout << "Health: " << health << " ";
fout << "Gold: " << gold << " ";
fout << "Magic: " << magic << " ";
fout << "Attack: " << attack << " ";
fout << "Aim: " << aim << " ";
fout << "Defence: "<< defence << " ";
fout << "Weapon: " << weapon << " ";
fout << "Shield: " << shield << " ";
fout << "Armor: " << armor << " ";
fout << "Item1: " << item1 << " ";
fout << "Item2: " << item2 << " ";
fout << "Item3: " << item3 << " ";
fout << "Item4: " << item4 << " ";
fout << "Item5: " << item5 << " ";
fout << "Item6: " << item6 << " ";
fout << "Item7: " << item7 << " ";
fout << "Item8: " << item8 << " ";
fout << "Item9: " << item9 << " ";
fout << "Item10: " << item10 << " ";
fout << "Item11: " << item11 << " ";
fout << "Item12: " << item12 << " ";
fout << "Item13: " << item13 << " ";
fout << "Item14: " << item14 << " ";
fout << "Item15: " << item15 << " ";
fout << "Item16: " << item16 << " ";
fout << "Item17: " << item17 << " ";
fout << "Item18: " << item18 << " ";
fout << "Item19: " << item19 << " ";
fout << "Item20: " << item20 << " ";
fout << "Item21: " << item21 << " ";
fout << "Item22: " << item22 << " ";
fout << "Item23: " << item23 << " ";
fout << "Item24: " << item24 << " ";
fout << "Item25: " << item25 << " ";
fout << "Item26: " << item26 << " ";
fout << "Item27: " << item27 << " ";
fout << "Item28: " << item28 << " ";
fout << "Item29: " << item29 << " ";
fout << "Item30: " << item30 << " ";
fout.close();
}
else if (menuscreen == 3){
}
else if (menuscreen == 4){
}
else if (menuscreen == 5){
}
}
no, you don't have to include .cpp files. I don't even think that would compile. Just add the other .cpp file to your project, and any variables you need from it, put an extern at the top of the main source file for them. And any functions, just prototype them like you regularly would.
extern?and i need it so the cpp fies read variables from the main.cpp file.
i got it to wokr i just gotta find out how to get the menuscreen._.cpp file to read variaables from the other cpp file
(no, im not just starting, i just cant do functions but i got classes and most the other **** down)
aparently i do, sdo i jsut have to lsit them on the top? (of menuscreen_.cpp)Quote:
Originally posted by frenchfry164
no, you don't have to include .cpp files. I don't even think that would compile. Just add the other .cpp file to your project, and any variables you need from it, put an extern at the top of the main source file for them. And any functions, just prototype them like you regularly would.
this is what i did apperantly there is linker problem(at bottom of post)
this is menuscreen_.cpp
this is excalibur.cppCode:#include <iostream>
#include <string>
#include <stdlib.h>
#include <windows.h>
#include <conio.h>
#include <fstream>
char * menuscreenv;
int gold, health, magic, attack, aim, defence, ynvari, charactertype;
string empty, weapon, shield, armor, szWord, szLine, player, item1, item2, item3, item4, item5, item6, item7, item8, item9, item10, item11, item12, item13, item14, item15, item16, item17, item18, item19, item20, item21, item22, item23, item24, item25, item26, item27, item28, item29, item30;
bool menuselect;
int mainb(int argc, char *argv[])
{
cout << menuscreenv << endl;
cin >> menuselect;
if (menuselect == 1){
}
else if (menuselect == 2){
ofstream fout;
fout.open("stats.sav");
fout << "Player: " << player << " ";
fout << "CharacterType: " << charactertype << " ";
fout << "Health: " << health << " ";
fout << "Gold: " << gold << " ";
fout << "Magic: " << magic << " ";
fout << "Attack: " << attack << " ";
fout << "Aim: " << aim << " ";
fout << "Defence: "<< defence << " ";
fout << "Weapon: " << weapon << " ";
fout << "Shield: " << shield << " ";
fout << "Armor: " << armor << " ";
fout << "Item1: " << item1 << " ";
fout << "Item2: " << item2 << " ";
fout << "Item3: " << item3 << " ";
fout << "Item4: " << item4 << " ";
fout << "Item5: " << item5 << " ";
fout << "Item6: " << item6 << " ";
fout << "Item7: " << item7 << " ";
fout << "Item8: " << item8 << " ";
fout << "Item9: " << item9 << " ";
fout << "Item10: " << item10 << " ";
fout << "Item11: " << item11 << " ";
fout << "Item12: " << item12 << " ";
fout << "Item13: " << item13 << " ";
fout << "Item14: " << item14 << " ";
fout << "Item15: " << item15 << " ";
fout << "Item16: " << item16 << " ";
fout << "Item17: " << item17 << " ";
fout << "Item18: " << item18 << " ";
fout << "Item19: " << item19 << " ";
fout << "Item20: " << item20 << " ";
fout << "Item21: " << item21 << " ";
fout << "Item22: " << item22 << " ";
fout << "Item23: " << item23 << " ";
fout << "Item24: " << item24 << " ";
fout << "Item25: " << item25 << " ";
fout << "Item26: " << item26 << " ";
fout << "Item27: " << item27 << " ";
fout << "Item28: " << item28 << " ";
fout << "Item29: " << item29 << " ";
fout << "Item30: " << item30 << " ";
fout.close();
}
else if (menuselect == 3){
}
else if (menuselect == 4){
}
else if (menuselect == 5){
}
}
linker errorsCode:#include <iostream>
#include <string>
#include <stdlib.h>
#include <windows.h>
#include <conio.h>
#include <fstream>
using namespace std;
int menuscreen_();
char * menuscreenv;
int gold, health, magic, attack, aim, defence, ynvari, charactertype;
string empty, weapon, shield, armor, szWord, szLine, player, item1, item2, item3, item4, item5, item6, item7, item8, item9, item10, item11, item12, item13, item14, item15, item16, item17, item18, item19, item20, item21, item22, item23, item24, item25, item26, item27, item28, item29, item30;
bool menuselect;
int main(int argc, char *argv[])
{
int menuscreen_();
cin.get();
return 0;
}
apperntly im not supposed to name the variables in menuscreen_.cpp but if i dont it says the variables arent named. how do i use the same variables with sama data in both?Code:c:\dev-c++\projects\menuscreen_.o(.bss+0x94):menuscreen_.cp: multiple definition of `menuscreenv'
c:\dev-c++\projects\excalibur.o(.bss+0x94):excalibur.cpp: first defined here
c:\dev-c++\projects\menuscreen_.o(.bss+0xb8):menuscreen_.cp: multiple definition of `menuselect'
c:\dev-c++\projects\excalibur.o(.bss+0xb8):excalibur.cpp: first defined here
c:\dev-c++\projects\menuscreen_.o(.bss+0x18):menuscreen_.cp: multiple definition of `player'
c:\dev-c++\projects\excalibur.o(.bss+0x18):excalibur.cpp: first defined here
c:\dev-c++\projects\menuscreen_.o(.bss+0xb4):menuscreen_.cp: multiple definition of `charactertype'
c:\dev-c++\projects\excalibur.o(.bss+0xb4):excalibur.cpp: first defined here
c:\dev-c++\projects\menuscreen_.o(.bss+0x9c):menuscreen_.cp: multiple definition of `health'
c:\dev-c++\projects\excalibur.o(.bss+0x9c):excalibur.cpp: first defined here
c:\dev-c++\projects\menuscreen_.o(.bss+0x98):menuscreen_.cp: multiple definition of `gold'
c:\dev-c++\projects\excalibur.o(.bss+0x98):excalibur.cpp: first defined here
c:\dev-c++\projects\menuscreen_.o(.bss+0xa0):menuscreen_.cp: multiple definition of `magic'
c:\dev-c++\projects\excalibur.o(.bss+0xa0):excalibur.cpp: first defined here
c:\dev-c++\projects\menuscreen_.o(.bss+0xa4):menuscreen_.cp: multiple definition of `attack'
c:\dev-c++\projects\excalibur.o(.bss+0xa4):excalibur.cpp: first defined here
c:\dev-c++\projects\menuscreen_.o(.bss+0xa8):menuscreen_.cp: multiple definition of `aim'
c:\dev-c++\projects\excalibur.o(.bss+0xa8):excalibur.cpp: first defined here
c:\dev-c++\projects\menuscreen_.o(.bss+0xac):menuscreen_.cp: multiple definition of `defence'
c:\dev-c++\projects\excalibur.o(.bss+0xac):excalibur.cpp: first defined here
c:\dev-c++\projects\menuscreen_.o(.bss+0x4):menuscreen_.cp: multiple definition of `weapon'
c:\dev-c++\projects\excalibur.o(.bss+0x4):excalibur.cpp: first defined here
c:\dev-c++\projects\menuscreen_.o(.bss+0x8):menuscreen_.cp: multiple definition of `shield'
c:\dev-c++\projects\excalibur.o(.bss+0x8):excalibur.cpp: first defined here
c:\dev-c++\projects\menuscreen_.o(.bss+0xc):menuscreen_.cp: multiple definition of `armor'
c:\dev-c++\projects\excalibur.o(.bss+0xc):excalibur.cpp: first defined here
c:\dev-c++\projects\menuscreen_.o(.bss+0x1c):menuscreen_.cp: multiple definition of `item1'
c:\dev-c++\projects\excalibur.o(.bss+0x1c):excalibur.cpp: first defined here
c:\dev-c++\projects\menuscreen_.o(.bss+0x20):menuscreen_.cp: multiple definition of `item2'
c:\dev-c++\projects\excalibur.o(.bss+0x20):excalibur.cpp: first defined here
c:\dev-c++\projects\menuscreen_.o(.bss+0x24):menuscreen_.cp: multiple definition of `item3'
c:\dev-c++\projects\excalibur.o(.bss+0x24):excalibur.cpp: first defined here
c:\dev-c++\projects\menuscreen_.o(.bss+0x28):menuscreen_.cp: multiple definition of `item4'
c:\dev-c++\projects\excalibur.o(.bss+0x28):excalibur.cpp: first defined here
c:\dev-c++\projects\menuscreen_.o(.bss+0x2c):menuscreen_.cp: multiple definition of `item5'
c:\dev-c++\projects\excalibur.o(.bss+0x2c):excalibur.cpp: first defined here
c:\dev-c++\projects\menuscreen_.o(.bss+0x30):menuscreen_.cp: multiple definition of `item6'
c:\dev-c++\projects\excalibur.o(.bss+0x30):excalibur.cpp: first defined here
c:\dev-c++\projects\menuscreen_.o(.bss+0x34):menuscreen_.cp: multiple definition of `item7'
c:\dev-c++\projects\excalibur.o(.bss+0x34):excalibur.cpp: first defined here
c:\dev-c++\projects\menuscreen_.o(.bss+0x38):menuscreen_.cp: multiple definition of `item8'
c:\dev-c++\projects\excalibur.o(.bss+0x38):excalibur.cpp: first defined here
c:\dev-c++\projects\menuscreen_.o(.bss+0x3c):menuscreen_.cp: multiple definition of `item9'
c:\dev-c++\projects\excalibur.o(.bss+0x3c):excalibur.cpp: first defined here
c:\dev-c++\projects\menuscreen_.o(.bss+0x40):menuscreen_.cp: multiple definition of `item10'
c:\dev-c++\projects\excalibur.o(.bss+0x40):excalibur.cpp: first defined here
c:\dev-c++\projects\menuscreen_.o(.bss+0x44):menuscreen_.cp: multiple definition of `item11'
c:\dev-c++\projects\excalibur.o(.bss+0x44):excalibur.cpp: first defined here
c:\dev-c++\projects\menuscreen_.o(.bss+0x48):menuscreen_.cp: multiple definition of `item12'
c:\dev-c++\projects\excalibur.o(.bss+0x48):excalibur.cpp: first defined here
c:\dev-c++\projects\menuscreen_.o(.bss+0x4c):menuscreen_.cp: multiple definition of `item13'
c:\dev-c++\projects\excalibur.o(.bss+0x4c):excalibur.cpp: first defined here
c:\dev-c++\projects\menuscreen_.o(.bss+0x50):menuscreen_.cp: multiple definition of `item14'
c:\dev-c++\projects\excalibur.o(.bss+0x50):excalibur.cpp: first defined here
c:\dev-c++\projects\menuscreen_.o(.bss+0x54):menuscreen_.cp: multiple definition of `item15'
c:\dev-c++\projects\excalibur.o(.bss+0x54):excalibur.cpp: first defined here
c:\dev-c++\projects\menuscreen_.o(.bss+0x58):menuscreen_.cp: multiple definition of `item16'
c:\dev-c++\projects\excalibur.o(.bss+0x58):excalibur.cpp: first defined here
c:\dev-c++\projects\menuscreen_.o(.bss+0x5c):menuscreen_.cp: multiple definition of `item17'
c:\dev-c++\projects\excalibur.o(.bss+0x5c):excalibur.cpp: first defined here
c:\dev-c++\projects\menuscreen_.o(.bss+0x60):menuscreen_.cp: multiple definition of `item18'
c:\dev-c++\projects\excalibur.o(.bss+0x60):excalibur.cpp: first defined here
c:\dev-c++\projects\menuscreen_.o(.bss+0x64):menuscreen_.cp: multiple definition of `item19'
c:\dev-c++\projects\excalibur.o(.bss+0x64):excalibur.cpp: first defined here
c:\dev-c++\projects\menuscreen_.o(.bss+0x68):menuscreen_.cp: multiple definition of `item20'
c:\dev-c++\projects\excalibur.o(.bss+0x68):excalibur.cpp: first defined here
c:\dev-c++\projects\menuscreen_.o(.bss+0x6c):menuscreen_.cp: multiple definition of `item21'
c:\dev-c++\projects\excalibur.o(.bss+0x6c):excalibur.cpp: first defined here
c:\dev-c++\projects\menuscreen_.o(.bss+0x70):menuscreen_.cp: multiple definition of `item22'
c:\dev-c++\projects\excalibur.o(.bss+0x70):excalibur.cpp: first defined here
c:\dev-c++\projects\menuscreen_.o(.bss+0x74):menuscreen_.cp: multiple definition of `item23'
c:\dev-c++\projects\excalibur.o(.bss+0x74):excalibur.cpp: first defined here
c:\dev-c++\projects\menuscreen_.o(.bss+0x78):menuscreen_.cp: multiple definition of `item24'
c:\dev-c++\projects\excalibur.o(.bss+0x78):excalibur.cpp: first defined here
c:\dev-c++\projects\menuscreen_.o(.bss+0x7c):menuscreen_.cp: multiple definition of `item25'
c:\dev-c++\projects\excalibur.o(.bss+0x7c):excalibur.cpp: first defined here
c:\dev-c++\projects\menuscreen_.o(.bss+0x80):menuscreen_.cp: multiple definition of `item26'
c:\dev-c++\projects\excalibur.o(.bss+0x80):excalibur.cpp: first defined here
c:\dev-c++\projects\menuscreen_.o(.bss+0x84):menuscreen_.cp: multiple definition of `item27'
c:\dev-c++\projects\excalibur.o(.bss+0x84):excalibur.cpp: first defined here
c:\dev-c++\projects\menuscreen_.o(.bss+0x88):menuscreen_.cp: multiple definition of `item28'
c:\dev-c++\projects\excalibur.o(.bss+0x88):excalibur.cpp: first defined here
c:\dev-c++\projects\menuscreen_.o(.bss+0x8c):menuscreen_.cp: multiple definition of `item29'
c:\dev-c++\projects\excalibur.o(.bss+0x8c):excalibur.cpp: first defined here
c:\dev-c++\projects\menuscreen_.o(.bss+0x90):menuscreen_.cp: multiple definition of `item30'
c:\dev-c++\projects\excalibur.o(.bss+0x90):excalibur.cpp: first defined here
c:\dev-c++\projects\menuscreen_.o(.bss+0x0):menuscreen_.cp: multiple definition of `empty'
c:\dev-c++\projects\excalibur.o(.bss+0x0):excalibur.cpp: first defined here
c:\dev-c++\projects\menuscreen_.o(.bss+0x10):menuscreen_.cp: multiple definition of `szWord'
c:\dev-c++\projects\excalibur.o(.bss+0x10):excalibur.cpp: first defined here
c:\dev-c++\projects\menuscreen_.o(.bss+0x14):menuscreen_.cp: multiple definition of `szLine'
c:\dev-c++\projects\excalibur.o(.bss+0x14):excalibur.cpp: first defined here
c:\dev-c++\projects\menuscreen_.o(.bss+0xb0):menuscreen_.cp: multiple definition of `ynvari'
c:\dev-c++\projects\excalibur.o(.bss+0xb0):excalibur.cpp: first defined here
ive tried everything i could think of! i even searched the net and and looked at other sources.
(note the varibales are prenamed in excalibur.cpp)
Why did you do it like that? Why do you declare those variables in excalibur.cpp?
your such a idiot j/k
you use extern to use the variables from other source files. it basically tells the linker, "you can use this variable, but it's not declared here".
So whatcha do is declare it like normal in say, excaliber.cpp. Example:
int test;
then in main.cpp write this
extern int test;
then main.cpp can use test. and it is the SAME one as well.
do you have many friends?Quote:
Originally posted by frenchfry164
your such a idiot j/k
actualy i dont, thats why i like programming. or were u asking the other guy? thanks for ur help the guy who posted before doob
thats not he entire excalibnur.cpp file i didnt wanna ahve like another 600 line uneaded.Quote:
Originally posted by d00b
Why did you do it like that? Why do you declare those variables in excalibur.cpp?
Ah..
Lol :) the code there is kinda messy :)
you need to work on your coding style. I like to indent all my lines one space after each open brace, then stop doing it when I get to a closing brace. It's how most people do it. There are some other stuff, like switches, and declaring functions, that I do different from other people.
Yea... and If you paste it on the forum do not forget to use the tags :)
although sometimes it doesn't paste right, and you gotta go in and fix it. I hate it when that happens
well I have my own way of wrting code. i indent stuff that has to do with a certain thing. like if one set of couts made a ascii art banner it could be indented twice and couts that are there and may never look at again are not indented. and i do code braskets the popular warQuote:
Originally posted by frenchfry164
you need to work on your coding style. I like to indent all my lines one space after each open brace, then stop doing it when I get to a closing brace. It's how most people do it. There are some other stuff, like switches, and declaring functions, that I do different from other people.
and i aslo line up the open close braskets incase of this situationCode:{
/// code
}
i woulnt run into like THAT but in a case i am using braksets ina s et of braskets.Code:{
{
//code
}
}
Everybody does :)Quote:
Originally posted by frenchfry164
although sometimes it doesn't paste right, and you gotta go in and fix it. I hate it when that happens
Why don't you re-read my post. I never once stated that it was a library file. I was just showing a simple header.Quote:
Originally posted by Okiesmokie
uhh that is not a library file, that is a header file.
In visual C++ in the new project menu, you choose Static Linked Library and write your code, hit the build button and voila you have a library.
Name the file menuscreen_.h instead of .cpp
name the function something else than main, like void menuscreen()
include menuscreen_.h in excalibur.cpp and call menuscreen(), don't call the file, call the function.
no it was supposed to be a cpp file, i meant like attaching files, not header(include files).
Sams Teachyourself C++ In 21 Days is a good book to read. Maybe you'll get something from there. Besides, it's free on the net :)