简体   繁体   中英

I cannot run multiple files within solution explorer Visual Studio Community 2015

I'm a beginner at C++ and I am willing to learn a lot more. I am using this website called learncpp.com and I've hit a few lessons, so far so good.

Unfortunately, I came to this part of the lesson: http://www.learncpp.com/cpp-tutorial/18-programs-with-multiple-files/ and my Visual Studio 2015 won't compile 2 files in the solution. The site says that it will be using this method a lot more in the future so I am concerned that I won't be able to proceed if I don't get this solved.

I have 2 files. One of which is named add.cpp and the other is named main.cpp.

图片

The main.cpp contains the code:

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

int add(int x, int y);

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

And my add.cpp contains

#include "stdafx.h"

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

When I run the program, I get this message:

图片

Anybody know a fix? I really want to continue this fun journey into learning C++.

that is a sign of unsuccessful build and for sure as long as you didn't include add.h in fact I think you didn't even create add.h because I see Add() prototype in main.cpp which excludes decalring it in a header.

the solution:

1- create a header file Add.h which will look like:

// add.h
#ifndef ADD_H_
#define ADD_H_
int add(int x, int y);
#endif

2- file add.cpp:

// add.cpp
#include "add.h"
int add(int x, int y)
{
     return x + y;
}

3- main.cpp:

// main.cpp
#include "stdafx.h"
#include "add.h"
#include <iostream>

// int add(int x, int y); // there's no need to re-declare it here because you already declared it in header

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

4- go to MSVC: project->add existing item then select add.cpp

5- now compile and build and run.

I wish it will work for you.

Are you getting compile errors for

#include "stdafx.h"

You may need to recreate the project or remove the includes for stdafx.h.

stdafx.h is used for precompiled headers. To use this feature, when creating the project, after clicking on next, click on check box for "precompiled headers" and do not click on check box for "empty project", and uncheck "secure development lifcycle (SDL)". Note that this will create stuff.cpp for the main source file. You can copy the source from main.cpp into stuff.cpp. You'll need to add "existing item" to include add.cpp into the project.

The alternative is to not use precompiled headers, and not use stdafx.h, which is a part of the precompiled headers. When creating a project, I normally clear "precompiled headers", clear "secure development lifcycle (SDL)", and set "empty project".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM