Noob Problem

Hi,


Ive been following along with a tutorial i found on the web, but this has got me really confused, I know it's probably something really easy, but its not that i'm asking for a quick solution but also why it's not working so that i understand it.

I'm using VS 2010,

This is my small program I'm trying to run with the use of multiple files and a custom header file.

1
2
3
4
5
6
7
8
9
10
11
12
#include "stdafx.h"
#include <iostream>

#include "add.h" // this brings in the declaration for add()
 

int main()
{
    using namespace std;
    cout << "The sum of 3 and 4 is " << add(3, 4) << endl;
    return 0;
}



Here is my other .cpp file which contains the function i want to call in the project.cpp

1
2
3
4
5
6
7
8

#include "stdafx.h"
#include <iostream>

int add(int x, int y)
{
    return x + y;
}



And here is my Header file,

1
2
3
4
5
6
7
8
9
#ifndef ADD_H
#define ADD_H
 
int add(int x, int y); // function prototype for add.h
{
	return x + y;
}
 
#endif 



I'm getting this error:

 
1>e:\33visualstudio\project\project\add.h(5): error C2447: '{' : missing function header (old-style formal list?)


I'm sure i'm doing mulitple things wrong, I really don't know what header it's looking for, the tutorial didn't specify, i read comments about this showing their code, and they saying it worked,

I copied theirs verbatim, and yet still no compiles.

Thank you for any help

Hi Legato

Remove the semicolon from line 4 in add.h the semicolon means it is a function declaration. A function definition just has code in braces.

There isn't a need to have your other .cpp file - if you had that in your build it would be a multiple definition - you would have defined the same function twice.

Hope all goes well.





Hi,

Thanks for the reply!

Am i supposed to compile the header file?


My original goal was to be able to store a forward declaration in another file (.cpp) and call on it from the Main.cpp (project.cpp as above).

I know i can define the function in my Project.cpp, but from the tutorial i was reading, it was saying that i could use another file to host the declaration, then call it from a function in the Project.cpp.

I'm a little confused, I was hoping maybe someone could let me in on the secrets hehe

Thanks for the help
The using namespace std; is better to put it in the start(not that otherwise is wrong but in the start is better)
in your .cpp file, #include "add.h" , and remove the body or definition of add() in add.h :

add.cpp
1
2
3
4
5
6
7
8
9
#include "stdafx.h"   // <== i think you won't need this 2
#include <iostream> // <== you won't need this header

#include "add.h"

int add(int x, int y) // define the function add()
{
    return x + y;
}


add.h
1
2
3
4
5
6
#ifndef ADD_H
#define ADD_H
 
int add(int x, int y); // function prototype for add.h

#endif  
Last edited on
Woohoo!!

it works!

Are header files usually just the function prototypes?

And how exactly does Project.cpp know to get the definition from add.cpp?

is that because of the header file?

wondering how that would work when it comes to creating an exe.
Topic archived. No new replies allowed.