【问题标题】:C++ running structure in Visual StudioVisual Studio 中的 C++ 运行结构
【发布时间】:2019-04-13 14:40:57
【问题描述】:

我在同一个项目中有两个文件,分别是f1.cppf2.cpp,用于解决算法中的同一个问题“连接问题”。在 Visual Studio 中,我将它们放入源文件中。源文件中的另一个文件是 pch.cpp。项目名称也是f1

f1.cpp的代码是,

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

using namespace std;

static const int N = 10000;

int main()
{
    int i, p, q, id[N];
    for (i = 0; i < N; i++) id[i] = i;
    while (cin >> p >> q) {
        int t = id[p];
        if (t == id[q]) continue;
        for (i = 0; i < N; i++)
            // this is for union 
            if (id[i] == t) id[i] = id[q];
        cout << " " << p << " " << q << endl;
    }
    std::cout << "Hello World!\n"; 
    return 0;
}

f2.cpp 是,

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

using namespace std;

static const int N = 10000;
int main() {
    int i, j, p, q, id[N], sz[N];
    for (i = 0; i < N; i++) { id[i] = i, sz[i] = 1; }
    while (cin >> p >> q) {
        for (i = p; i != id[i]; i = id[i]);
        for (j = q; j != id[j]; j = id[j]);
        if (i == j) continue;
        if (sz[i] < sz[j]) {
            id[i] = j; sz[j] += sz[i];
        }
        else {
            id[j] = i; sz[i] += sz[j];
        }
        cout << " " << p << " " << q << endl;
    }
}

虽然我很确定这两个代码,如果独立运行,是没有错误的。但是由于它们一起出现在源文件中,当我运行 f2.cpp 时,会出现错误,因为

C2065 'cin':未声明的标识符连接问题
C2065 'cout':未声明的标识符连接问题
C2065 'endl':未声明的标识符连接问题

My question is why the error like this happens ? 
Do I have to open a new project in visual studio for editing different solutions on same problems ? 

【问题讨论】:

  • 如果你一起运行它们,链接器会产生两个main()方法,这会导致编译器错误,因为你只能有一个main()...
  • 我可以在同一个项目中分别运行它们吗?
  • 删除文件...所以我需要打开一个新项目来单独运行它们?
  • @exteralvictor si,先生!
  • @exteralvictor 或者你只需​​要在你的代码中使用定义......

标签: c++ visual-studio


【解决方案1】:

就像我之前指出的,如果你同时运行多个源文件,链接器将产生两个 main() 方法,这将导致编译器错误,因为你只能有一个 main()...

记住程序只能指向一个main()函数...

但是,您可以在您的代码中使用#defines:

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

using namespace std;

static const int N = 10000;

// Running the first project...
#define F_PROJ

// Psuedo main for first project...
int first_main()
{
    int i, p, q, id[N];
    for (i = 0; i < N; i++) id[i] = i;
    while (cin >> p >> q) {
        int t = id[p];
        if (t == id[q]) continue;
        for (i = 0; i < N; i++)
            // this is for union 
            if (id[i] == t) id[i] = id[q];
        cout << " " << p << " " << q << endl;
    }
    std::cout << "Hello World!\n";
    return 0;
}

// Psuedo main for second project...
int second_main()
{
    int i, j, p, q, id[N], sz[N];
    for (i = 0; i < N; i++) { id[i] = i, sz[i] = 1; }
    while (cin >> p >> q) {
        for (i = p; i != id[i]; i = id[i]);
        for (j = q; j != id[j]; j = id[j]);
        if (i == j) continue;
        if (sz[i] < sz[j]) {
            id[i] = j; sz[j] += sz[i];
        }
        else {
            id[j] = i; sz[i] += sz[j];
        }
        cout << " " << p << " " << q << endl;
    }
    return 0;
}

int main()
{
#ifdef F_PROJ
    first_main();
#elif defined(S_PROJ)
    second_main();
#endif
}

只需分别指向#define F_PROJ#define S_PROJ...

【讨论】:

  • 将它们放入单独的函数中并根据命令行参数或类似的东西选择一个会更干净。
  • 好主意。
猜你喜欢
  • 2011-04-18
  • 2015-06-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-31
  • 1970-01-01
  • 1970-01-01
  • 2020-08-05
相关资源
最近更新 更多