I am making a project in Visual Studio 2013 Preview which includes the code from the tutorials I am learning C++ from. The main file in the project contains a program which is able to launch the functions defined in another .cpp file in the same project. The code for it is below:
#include "stdafx.h"
#include <iostream>
#include "Comments.cpp"
#include "Structure of a Program.cpp"
int structure(void);
void comment(void);
int main()
{
using namespace std;
cout << "This project contains files related to Chapter 1 of Learn CPP
Tutorials\n" << endl;
cout << "Please type in the tutorial number to view its output:\n" <<
"1 - Structure of a Program\n" <<
"2 - Comments\n" <<
"3 - A First Look at Variables (and cin)\n" <<
"4 - A First Look at Functions\n" <<
"5 - A First Look at Operators\n" <<
"6 - Whitespace & Basic Formatting\n" <<
"7 - Forward Declarations\n" <<
"8 - Programs With Multiple Files\n" <<
"9 - Header Files\n" <<
"10 - A First Look at the Preprocessor\n" <<
"11 - How to Design your First Programs\n" << endl;
int x;
cin >> x;
if (x == 1)
{
structure();
}
if (x == 2)
{
comment();
}
cout << "Thank you for using this program. Good bye." << endl;
return 0;
}
The problem I am having is that when I build the program, there is always an error saying that the functions I am launching are already defined even though they are not so. Basically, I need help in how to launch functions which are located in a different .cpp file but are in the same project.
Thanks
Never include .cpp files!
Typically, including a .cpp file will result in recompilation of code that should not or may not be compiled several times (hence the link errors). On the other hand, in .h files you put declarations and other stuff that may be compiled several times, they will be compiled roughly once for every include of that .h file.
In this case, since you have declared structure() and comment() you probably don't need to replace the .cpp includes with anything.