简体   繁体   中英

C++ running structure in Visual Studio

I have two files in the same project, which are f1.cpp and f2.cpp used to solve the same problem "connectivity problem" in algorithm. In visual studio I put them into the sources files.Another file in the source files is pch.cpp . Also the project name is f1 .

The code of f1.cpp is,

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

the f2.cpp is,

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

Although I am pretty sure the two codes, if run independently, is no error. But as they appear together in the sources files, when I ran the f2.cpp, there will be error as

C2065 'cin': undeclared identifier connectivity problem
C2065 'cout': undeclared identifier connectivity problem
C2065 'endl': undeclared identifier connectivity problem

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 ? 

Just like I pointed out before if you run multiple source files together, the linker will produce two main() methods, and that will cause a compiler error since you can have only one main() ...

Remember that the program can only point to one main() function...

But, you can use #define s in your code:

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

Just point #define F_PROJ or #define S_PROJ respectively...

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