Doing a quiz..what is wrong?

OK so I noticed a few things that are very confusing.. maybe someone can explain to me what is going on and why? Here are my files..


// quiz1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include "io.cpp"
using namespace std;

int readnumber();
int writenumber();

int main()
{
readnumber1();
readnumber2();
writenumber();
return 0;
}

------------------------------------

and the attached file..

------------------------------------

#include "stdafx.h"
#include <iostream>
using namespace std;
int x;
int y;
int readnumber1()
{
cout << "Enter a number to add. " << endl;

cin >> x;

return x;
}

int readnumber2()
{
cout << "Now enter another number to add. " << endl;
cin >> y;
return y;
}



void writenumber()
{
cout << "The answer is " << x+y << endl;
}

############################################################
############################################################

It is giving me this error..

1>learncppquiz.obj : error LNK2005: "int __cdecl readnumber1(void)" (?readnumber1@@YAHXZ) already defined in io.obj
1>learncppquiz.obj : error LNK2005: "int __cdecl readnumber2(void)" (?readnumber2@@YAHXZ) already defined in io.obj
1>learncppquiz.obj : error LNK2005: "void __cdecl writenumber(void)" (?writenumber@@YAXXZ) already defined in io.obj
1>learncppquiz.obj : error LNK2005: "int x" (?x@@3HA) already defined in io.obj
1>learncppquiz.obj : error LNK2005: "int y" (?y@@3HA) already defined in io.obj
1>c:\users\gern\documents\visual studio 2010\Projects\learncppquiz\Debug\learncppquiz.exe : fatal error LNK1169: one or more multiply defined symbols found
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========



################################################################################################################################################################

Here's the thing though as I play with it and I change where I plug x and y in on the code it tells me that they are *undeclared
Last edited on
As far as i can see there are no prototype declarations on:

readnumber1(); readnumber2();

I haven't worked with attached files before so i might be wrong.
Last edited on
If I am understanding it right (which I must not be lol) but the attached file is supposed to take care of that because they are declared there and then called in main?
Normally .cpp files should not be included. Instead create a header file containing the declarations of the functions and include that instead. You should not have to make any changes to the .cpp file.
As i said i haven't worked with attached files at all so i don't know when they are executed, but as i can see you have functions in there, so unless it is executing before main() in your program, i'm guessing there needs to be prototype declarations.
Topic archived. No new replies allowed.