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 ?









share|improve this question























  • 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










  • 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














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 ?









share|improve this question























  • 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










  • 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












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 ?









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 3:14

























asked Nov 10 at 3:09









exteralvictor

185114




185114











  • 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










  • 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











  • 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












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 #defines 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...






share|improve this answer






















  • 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











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















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

























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 #defines 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...






share|improve this answer






















  • 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















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 #defines 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...






share|improve this answer






















  • 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













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 #defines 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...






share|improve this answer














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 #defines 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...







share|improve this answer














share|improve this answer



share|improve this answer








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

















  • 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


















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Darth Vader #20

How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

Ondo