【发布时间】:2023-03-12 08:30:01
【问题描述】:
我最近下载了 Microsoft Visual C++ 2010 Express 来尝试学习 C++,但我遇到了一个问题。我以前用 Java 使用过 Eclipse,Microsoft Visual C++ 似乎与它相似。
所以我的问题是我创建了一个名为 Project 的项目,并且项目中有两个文件(HelloWorld.cpp 和 PowersOfTwo.cpp)。 HelloWorld.cpp 的代码如下:
/*
Hello World File
*/
#include <iostream>
using namespace std;
int main()
{
cout<< "Hello, World" << endl;
return 0;
}
PowersOfTwo.cpp 代码如下:
/*
This program generates the powers of two
until the number that the user requested
*/
#include <iostream>
using namespace std;
int raiseToPower(int n, int k);
int main()
{
int limit;
cout << "This program lists the powers of two. " << endl;
cout << "Enter exponent limit: ";
cin >> limit;
for (int i = 0; i <= limit; i++)
{
cout << "2 to the " << i << " = " << raiseToPower(2, i) << endl;
}
return 0;
}
/* Function for raiseToPower */
int raiseToPower(int n, int k)
{
int result = 1;
for (int i = 0; i < k; i++)
{
result *= n;
}
return result;
}
基本上,我尝试在不调试 PowersOfTwo.cpp 文件的情况下开始,但最终出现一个致命错误,指出 _main 已在 HelloWorld.obj 中定义。这是否意味着我不能在同一个项目中拥有两个带有 main 方法的文件(与 eclipse 不同,当我可以拥有两个带有 main 方法的文件时)。这是否也意味着我每次都必须创建一个新项目才能让不相关的程序运行?
【问题讨论】:
-
哪个
main将用于启动程序? -
然后删除另一个。
-
你只能有一个主要功能。这是一个规则。
-
所以我只需要删除 HelloWorld.cpp 文件
-
@chris Eclipse 让您设置开始项目而不是源文件。每个项目可能(并且将会)拥有自己的
main。同样适用于 Visual Studio。
标签: c++ visual-c++