From: Per W. <pw...@ia...> - 2005-04-13 06:19:51
|
Don't put code in include files. Just put declarations there and code in *.cpp files. So: subten.h: int subten(int num); subten.cpp: #include "subten.h" int subten(int num) { return num - 10; } main.cpp: #include <iostream> #include "subten.h" int main() { ... a = subten(a); ... } Then add the new source file subten.cpp to your project, to make sure that it gets compiled and linked into the application. Note also, avoid "using namespace std" and instead just "wake up" the individual items you need. And most importantly: don't do a big global "using namespace std" in a header file. Note that the scope of the using statement isn't limited to the include file but will "taint" the source file that references the include file too. Short note about include files. They are treated just as if all text in them had been available in the *.cpp file. That is why they are called include files :) /Per W On Wed, 13 Apr 2005, Austin Scholze wrote: > Hi. I'm new to programming, and don't know all the terminology, so please go easy on me. > > I'm trying to figure out how to create a include file so I can use it's functions in my programs, by just including it. > > Here's my simple test program that didn't work in the least ^^ > > __________________________________________________ > > the program I tried to link to: > > #include<iostream> > > using namespace std; > > > int subten(int num) > { > num -= 10; > return num; > } > > The program I tried to link to the above with: > > #include <iostream> > #include "subten" > using namespace std; > > main() > { > > int a = 20; > cout << a << endl; > a=subten(a); > cout << a; > > system("pause"); > } > > Ok, now please don't chew me out to bad for the mistakes that I made, and are common knowledge to everyone but me :p. thanks. > |