C++ running structure in Visual Studio
up vote
0
down vote
favorite
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 ?
c++ visual-studio
|
show 3 more comments
up vote
0
down vote
favorite
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 ?
c++ visual-studio
If you run them together, the linker will produce twomain()
methods, and that will cause a compiler error since you can have only onemain()
...
– Ruks
Nov 10 at 3:16
Can I run them separately in the same project ?
– exteralvictor
Nov 10 at 3:17
Remove the file ... so I need to open a new project to run them separately ?
– exteralvictor
Nov 10 at 3:20
@exteralvictor si, senor!
– Swordfish
Nov 10 at 3:21
@exteralvictor Or you just have to use defines in your code...
– Ruks
Nov 10 at 3:22
|
show 3 more comments
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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 ?
c++ 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 ?
c++ visual-studio
c++ visual-studio
edited Nov 10 at 3:14
asked Nov 10 at 3:09
exteralvictor
185114
185114
If you run them together, the linker will produce twomain()
methods, and that will cause a compiler error since you can have only onemain()
...
– Ruks
Nov 10 at 3:16
Can I run them separately in the same project ?
– exteralvictor
Nov 10 at 3:17
Remove the file ... so I need to open a new project to run them separately ?
– exteralvictor
Nov 10 at 3:20
@exteralvictor si, senor!
– Swordfish
Nov 10 at 3:21
@exteralvictor Or you just have to use defines in your code...
– Ruks
Nov 10 at 3:22
|
show 3 more comments
If you run them together, the linker will produce twomain()
methods, and that will cause a compiler error since you can have only onemain()
...
– Ruks
Nov 10 at 3:16
Can I run them separately in the same project ?
– exteralvictor
Nov 10 at 3:17
Remove the file ... so I need to open a new project to run them separately ?
– exteralvictor
Nov 10 at 3:20
@exteralvictor si, senor!
– Swordfish
Nov 10 at 3:21
@exteralvictor Or you just have to use defines in your code...
– Ruks
Nov 10 at 3:22
If you run them together, the linker will produce two
main()
methods, and that will cause a compiler error since you can have only one main()
...– Ruks
Nov 10 at 3:16
If you run them together, the linker will produce two
main()
methods, and that will cause a compiler error since you can have only one main()
...– Ruks
Nov 10 at 3:16
Can I run them separately in the same project ?
– exteralvictor
Nov 10 at 3:17
Can I run them separately in the same project ?
– exteralvictor
Nov 10 at 3:17
Remove the file ... so I need to open a new project to run them separately ?
– exteralvictor
Nov 10 at 3:20
Remove the file ... so I need to open a new project to run them separately ?
– exteralvictor
Nov 10 at 3:20
@exteralvictor si, senor!
– Swordfish
Nov 10 at 3:21
@exteralvictor si, senor!
– Swordfish
Nov 10 at 3:21
@exteralvictor Or you just have to use defines in your code...
– Ruks
Nov 10 at 3:22
@exteralvictor Or you just have to use defines in your code...
– Ruks
Nov 10 at 3:22
|
show 3 more comments
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
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...
It would be a lot cleaner to put them into separate functions and pick one based on a command line argument or something along those lines.
– Squidy
Nov 10 at 3:27
Great that would be a thought.
– exteralvictor
Nov 10 at 3:28
@Squidy Yes, thought so ...
– Ruks
Nov 10 at 3:29
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
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...
It would be a lot cleaner to put them into separate functions and pick one based on a command line argument or something along those lines.
– Squidy
Nov 10 at 3:27
Great that would be a thought.
– exteralvictor
Nov 10 at 3:28
@Squidy Yes, thought so ...
– Ruks
Nov 10 at 3:29
add a comment |
up vote
1
down vote
accepted
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...
It would be a lot cleaner to put them into separate functions and pick one based on a command line argument or something along those lines.
– Squidy
Nov 10 at 3:27
Great that would be a thought.
– exteralvictor
Nov 10 at 3:28
@Squidy Yes, thought so ...
– Ruks
Nov 10 at 3:29
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
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...
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...
edited Nov 10 at 3:28
answered Nov 10 at 3:25
Ruks
658111
658111
It would be a lot cleaner to put them into separate functions and pick one based on a command line argument or something along those lines.
– Squidy
Nov 10 at 3:27
Great that would be a thought.
– exteralvictor
Nov 10 at 3:28
@Squidy Yes, thought so ...
– Ruks
Nov 10 at 3:29
add a comment |
It would be a lot cleaner to put them into separate functions and pick one based on a command line argument or something along those lines.
– Squidy
Nov 10 at 3:27
Great that would be a thought.
– exteralvictor
Nov 10 at 3:28
@Squidy Yes, thought so ...
– Ruks
Nov 10 at 3:29
It would be a lot cleaner to put them into separate functions and pick one based on a command line argument or something along those lines.
– Squidy
Nov 10 at 3:27
It would be a lot cleaner to put them into separate functions and pick one based on a command line argument or something along those lines.
– Squidy
Nov 10 at 3:27
Great that would be a thought.
– exteralvictor
Nov 10 at 3:28
Great that would be a thought.
– exteralvictor
Nov 10 at 3:28
@Squidy Yes, thought so ...
– Ruks
Nov 10 at 3:29
@Squidy Yes, thought so ...
– Ruks
Nov 10 at 3:29
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53235696%2fc-running-structure-in-visual-studio%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
If you run them together, the linker will produce two
main()
methods, and that will cause a compiler error since you can have only onemain()
...– Ruks
Nov 10 at 3:16
Can I run them separately in the same project ?
– exteralvictor
Nov 10 at 3:17
Remove the file ... so I need to open a new project to run them separately ?
– exteralvictor
Nov 10 at 3:20
@exteralvictor si, senor!
– Swordfish
Nov 10 at 3:21
@exteralvictor Or you just have to use defines in your code...
– Ruks
Nov 10 at 3:22