Sign in to follow this  
ISDCaptain01

how to deal with multiple source files

Recommended Posts

Im having a bit of trouble with multiple source files

 

Say I declare a function prototype in my header file

 

header.h

#pragma once
#include <iostream>

int foo;

void Function(int);

source.cpp

#include "header.h"


void function()
{
   foo = 5;
}

main.cpp

#include "header.h"


int main()
{

   function();
   return 0;
}

for some reason visual c++ gives me trouble when I do this and my code refuses to compile. Why is this?

Share this post


Link to post
Share on other sites

Your header file declares a function called "Function" that takes an int.

 

You main.cpp and source.cpp use a function called "function" (note the difference in case) that doesn't take any parameters.

 

Also, your global int "foo" should be declared (i.e. "int foo;") in one of your .cpp files and externed in header.h (i.e. "extern int foo;").

Share this post


Link to post
Share on other sites
You have two functions, both with differing case and differing parameters:

void Function(int);
void function();

You make an attempt to call "function" in main.cpp, but header.h doesn't expose the proper prototype for "void function"; instead, header.h provides a prototype (without a definition) for "void Function(int)". You will also need to move "int foo;" to source.cpp, and expose a prototype for it "extern int foo;" in header.h.


See Organizing Code Files in C and C++ for more information.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this