problems with header files

I am trying to learn the concept behind calling functions using a header file, but whenever i try to compile, i get an error on line 6 of



heres the code for everything:
1
2
3
4
5
6
7
8
9
10
11
12
//main.cpp
using namespace std;
#include <iostream>
#include "importantbusiness.h"


int main()
{
    cout << importantbusiness(4) << endl;
    return 0;
}


1
2
3
4
5
6
7
8
9
10
11
12
//importantbusiness.cpp
using namespace std;
#include <iostream>
#include "importantbusiness.h"


int importantbusiness(int importantfiles)
{
    return importantfiles;
}



1
2
3
4
5
6
7
8
9
10
//importantbusiness.h
#include <iostream>

#ifndef IMPORTANTBUSINESS_H_INCLUDED
#define IMPORTANTBUSINESS_H_INCLUDED

#include "important business source.cpp"

#endif // IMPORTANTBUSINESS_H_INCLUDED


I am absolutely clueless as to why i keep getting this error. any suggestions would be greatly appreciated. (edit:) I rewrote the code in order to simplify my problem, and I finally got the program to run. i forcot to include "important business source.cpp" in the header. Thanks alot to everyone who lent an ear and their time.
Last edited on
Why do you have an int main() in both main.cpp and importantbusiness.cpp?? Shouldn't it only be in Main.cpp?
Last edited on
edited. sorry i accidentally copied over main.cpp twice.
hmm.. when I compile it seperately I get the same error. However, when I make one file out of it, It runs just fine...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//main.cpp
using namespace std;
#include <iostream>
#include <vector>

int Importantbusiness (int importantfiles)
{
    return importantfiles;
}

int Importantbusiness (int importantfiles);


int main()
{
    cout << "Important! We only have "<< Importantbusiness(4) << " important files left!"<< endl;
    cin.get();
    return 0;
}


I thought it might be because of a wrong ifndef and definition.. That's not the case however..

So until someone can find the real problem, I'd say you just put them together in one file..
Sorry, but I can't seem to solve it... Hope this much helps enough :)

Cheers!
Looks like you are missing a semicolon at the end of the prototype in the header file.
@Zhuge: I already tried that option for him, but that didn't work (for me)...
importantbusiness.h line 8: try adding a semicolon ; to the end of the line

@kaduuk: Shouldn't the function prototype be before the function declaration?
Last edited on
@ LB: whaha yeah just noticed that :P Usually I don't use prototypes (unless it's a huge function), and I quickly made it in one file... But you're right, it should be before :P And adding the semicolon doesn't work (for me..)

But I actually don't see why you'd want to separate this in 3 files.. Headers are used so you can recycle code right? But I don't see how you'd want to recycle this piece of code.. So my suggestion is that you just write it in one file
Last edited on
ok, well so far I added the semicolon to the prototype in the header file, but now when i compile I get "undefined reference to `Importantbusiness(int)'" for line 9 of main.cpp.

@kaduk
Thanks for your help, but I'm currently trying to learn how one would use two source files for a single project, so putting all the functions in one file would kinda defeat the purpose.

Well, from your code, I see that it is declared, but not defined, as the error suggests. You define it in importantbusiness.cpp but that file never gets used anywhere! How should it be defined in Main.cpp if you defined it somewhere else that doesn't even get included? ;)
Last edited on
Are you linking to importantbusiness.cpp?

What IDE are you using?
Topic archived. No new replies allowed.