繁体   English   中英

我无法在解决方案资源管理器Visual Studio Community 2015中运行多个文件

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

我是C ++的初学者,我愿意学习更多。 我正在使用一个名为learningcpp.com的网站,到目前为止,我已经学到了一些经验教训。

不幸的是,我上了这一节课: http : //www.learncpp.com/cpp-tutorial/18-programs-with-multiple-files/我的Visual Studio 2015不会在解决方案中编译2个文件。 该站点表示它将在未来更多地使用此方法,因此,我担心如果无法解决此问题,将无法继续进行。

我有2个档案。 其中一个名为add.cpp,另一个名为main.cpp。

图片

main.cpp包含以下代码:

#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;
}

我的add.cpp包含

#include "stdafx.h"

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

当我运行程序时,收到以下消息:

图片

有人知道解决办法吗? 我真的很想继续这个有趣的旅程,学习C ++。

这表示构建不成功,并且可以肯定的是,只要您不包含add.h ,实际上我认为您甚至都没有创建add.h因为我在main.cpp中看到了Add()原型,该原型不包含对它进行降级的操作。标头。

解决方案:

1-创建一个头文件Add.h如下所示:

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

2-文件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-转到MSVC:项目->添加现有项目,然后选择add.cpp

5-现在编译并生成并运行。

希望它对您有用。

您是否正在为编译错误

#include "stdafx.h"

您可能需要重新创建项目或删除stdafx.h的包含文件。

stdafx.h用于预编译的头文件。 要使用此功能,请在创建项目时单击下一步,然后单击“预编译头”复选框,而不要单击“空项目”复选框,然后取消选中“安全开发lifcycle(SDL)”。 请注意,这将为主源文件创建stuff.cpp。 您可以将源代码从main.cpp复制到stuff.cpp。 您需要添加“现有项目”以将add.cpp包含到项目中。

另一种选择是不使用预编译头,也不使用stdafx.h,后者是预编译头的一部分。 创建项目时,通常会清除“预编译头”,清除“安全开发lifcycle(SDL)”,然后设置“空项目”。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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