Using a "main.cpp" file to run several other .cpp files
Author |
Message |
Aange10
|
Posted: Thu Feb 05, 2015 6:18 pm Post subject: Using a "main.cpp" file to run several other .cpp files |
|
|
Hello! I'm an undergraduate at university and I've elected to take C++. I am using Microsoft Visual Studio 12.0 to handle my C++ projects. Thank you in advance for you assistance.
My class uses an online IDE for the majority of all assigned homework. However, today my instructor assigned problems 1-10 out of our textbooks to do at home with our visual studio IDE.
To begin my homework I followed a simple tutorial to make my first C++ project. I named the file Challenge1.cpp, and I successfully answered challenge number one. Next, on the left hand side of my screen there is a window named Solution Explorer to which I moused over a folder titled Source files and I right clicked to Add, and then add new item. I created another CPP file with the name Challenge2.cpp and I proceeded to answer question number two.
As you can imagine, the compiler errored because I have tried to have two main() functions in a single project. I understand that executables require there to be one main() function.
I have now created a third file main.cpp in hopes that this one file can hold my one main() function, and I can change the name of the functions held in the other files.
Before I state my problem, let me review that I have created three files in my project. Challenge1.cpp, Challenge2.cpp, main.cpp
Now, my problem is as follows:
Because I need to reserve the function name main() for the main.cpp file, I have changed the names of the functions in the other two files. What used to be int main (){} in Challenge1.cpp and Challenge2.cpp is now void challenge1(){} and void challenge2(){} respectively. (Of course, these functions are not empty as they contain the answers I've written for problems number one and two.) The issue I am now having is that I do not know how to call these first two procedures, stored in Challenge1.cpp and Challenge2.cpp, from a third file main.cpp,
I have tried the following.
code: |
#include <Challenge1.cpp>
void main(){
"Challenge1.cpp"::challenge1();
}
|
Strangely enough, I receive an error "C1083: Cannot open include file: '"Challenge1.cpp"': No such file or directory". This is strange because I have typed the file name correctly.
Also, I'm hypothesizing that the line
code: |
"Challenge1.cpp"::challenge1();
|
would not correctly run the procedure even if the file was opened. What should I write here?
If anybody could help me run my challenge files from this main file I would greatly appreciate it. If not I will see a tutor on campus. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Raknarg
|
Posted: Thu Feb 05, 2015 7:09 pm Post subject: RE:Using a "main.cpp" file to run several other .cpp files |
|
|
#include <Challenge1.cpp>
I think this is your problem. This is different from
#include "Challenge1.cpp"
They tell thecompiler to look in different places. IIRC using the quotation marks means the compiler will look through your current directory to find files. Basically every time you define something you want to include in a file, you would use quotation marks.
Anyone want to expand on that? My C++ knowledge isn't too expansive |
|
|
|
|
|
Tony
|
Posted: Thu Feb 05, 2015 8:14 pm Post subject: RE:Using a "main.cpp" file to run several other .cpp files |
|
|
There's a lot of issues here, but it might be quite simple... do you need one executable to handle both challenge1 and challenge2? Typically each challenge would get build as its own executable.
Quote:
#include <Challenge1.cpp>
Typically only header files (*.h) are included, not cpp files. Those are linked during compilation.
Quote:
"Challenge1.cpp"::challenge1();
Here "Challenge1.cpp" is a string literal. It could just as well be "Hello World". |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
|
Raknarg
|
Posted: Thu Feb 05, 2015 8:16 pm Post subject: RE:Using a "main.cpp" file to run several other .cpp files |
|
|
I didn't even notice that, is it possible to include ccp files? |
|
|
|
|
|
Insectoid
|
Posted: Thu Feb 05, 2015 8:23 pm Post subject: RE:Using a "main.cpp" file to run several other .cpp files |
|
|
Yes, but it's typically unnecessary. Linking is either done by the IDE or, if you're compiling from the terminal, using the -L command (if I remember right). |
|
|
|
|
|
Aange10
|
Posted: Fri Feb 06, 2015 2:52 pm Post subject: Re: Using a "main.cpp" file to run several other .cpp files |
|
|
I send my thanks to each of you that responded to my question. With the assistance of a friend I have come to a solution. I'll be sure to come back if I ever need more help! |
|
|
|
|
|
|
|