im making my frist multiple file compilation, and my first .h file of my own.
problem is, i dont know where to put struct grades {definition}. no matter which file i put it in, the compiler complains
exportimportstructmain.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
|
#include <iostream>
#include <fstream>
#include "exportimportstructdec.h"
using namespace std;
struct grades
{
int grade1;
int grade2;
int grade3;
int grade4;
}mygradesi,mygradeso,mygradesa;
//declare pointers to structures for file io:
grades * mygradesptro=&mygradeso;
grades * mygradesptra=&mygradesa;
grades * mygradesptri;
int main ()
{
int choice;
char opfile[]="binary.bin";
char opmessage[]="\nplease enter 4 grades separated by pressing enter\n";
char menu[]="\ndo u want to \n1, read from the file, or \n2, write over the file or \n3, append the file or \n4, exit the program?\n";
//give the user options:
do
{
cout<<menu;
cin>>choice;
//option 1, read from the file:
if (choice==1)
{
readfilefunc(opfile,mygradesi,mygradesptri);
}
//option 2, write over the file:
if (choice==2)
{
wrtfilefunc(opfile,opmessage,mygradeso,mygradesptro);
}
//option 3, append the file:
if (choice==3)
{
appfilefunc(opfile,opmessage,mygradesa,mygradesptra);
}
//option 4, exit:
}while (choice!=4);
return 0;
}
| |
exportimportstructdef.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
|
#include <iostream>
#include <fstream>
#include "exportimportstructdec.h"
using namespace std;
struct grades
{
char grade1[10];
int grade2;
int grade3;
int grade4;
}mygradesi,mygradeso,mygradesa;
//declare pointers to structures for file io:
grades * mygradesptro=&mygradeso;
grades * mygradesptra=&mygradesa;
grades * mygradesptri;
//the purpose of this program is to send structures into a file, and then read them from the same file
//structure to be written:
void readfilefunc(char filename1[],grades structi,grades * structptri)
{
//check the size of the file and allocate appropriate memory:
long begin,end;
ifstream filecheck (filename1);
if (filecheck.is_open())
{
begin = filecheck.tellg();
filecheck.seekg (0, ios::end);
end = filecheck.tellg();
filecheck.close();
int filesize=(end-begin)/(sizeof(structi));
structptri= new grades [filesize];
}
else cout << "Unable to open file for size check";
//read the file into memory:
int structarraysize=0;
ifstream filei;
filei.open (filename1, ios::in|ios::binary);
if (filei.is_open())
{
while ((filei.peek()!=EOF))
{
filei.read(reinterpret_cast<char*>(&structptri[structarraysize]), sizeof(grades));
structarraysize++;
}
//display memory on screen:
printmemory(structarraysize,structptri);
}
else cout << "Unable to open file for input";
}
void wrtfilefunc(char wrtfilename[],char wrtmessage[],grades structo,grades * structptro)
{
ofstream fileo;
fileo.open (wrtfilename, ios::out|ios::binary);
cout << wrtmessage;
scanstruct(structptro);
if (fileo.is_open())
{
fileo.write (reinterpret_cast<char*>(structptro), sizeof(structo));
fileo.close();
}
else cout << "\nUnable to open file for output\n";
}
void appfilefunc(char appfilename[],char appmessage[],grades structa,grades * structptra)
{
ofstream filea;
filea.open (appfilename, ios::app|ios::binary);
cout << appmessage;
scanstruct(structptra);
if (filea.is_open())
{
filea.write (reinterpret_cast<char*>(structptra), sizeof(structa));
filea.close();
}
else cout << "\nUnable to open file for output\n";
}
void scanstruct(grades * structptrscan)
{
cin >> (*structptrscan).grade1 >> (*structptrscan).grade2 >> (*structptrscan).grade3 >> (*structptrscan).grade4;
}
void printmemory(int specificstructarraysize,grades * structptri)
{
for(int i=0;i<specificstructarraysize;i++)
{
cout<<"\ngrade1\n"<< (structptri[i]).grade1<<"\ngrade2\n"<< (structptri[i]).grade2<<"\ngrade3\n"<< (structptri[i]).grade3<<"\ngrade4\n"<< (structptri[i]).grade4<<"\n";
}
}
| |
exportimportstructdec.h
1 2 3 4 5 6
|
struct grades;
void printmemory(int specificstructarraysize,grades * structptri);
void scanstruct(grades * structptrscan);
void readfilefunc(char filename1[],grades structi,grades * structptri);
void wrtfilefunc(char wrtfilename[],char wrtmessage[],grades structo,grades * structptro);
void appfilefunc(char wrtfilename[],char wrtmessage[],grades structo,grades * structptro);
| |
1>------ Build started: Project: exportstruct, Configuration: Debug Win32 ------
1>Linking...
1>exportimportstructmain.obj : error LNK2005: "struct grades * mygradesptri" (?mygradesptri@@3PAUgrades@@A) already defined in exportimportstructdef.obj
1>exportimportstructmain.obj : error LNK2005: "struct grades mygradeso" (?mygradeso@@3Ugrades@@A) already defined in exportimportstructdef.obj
1>exportimportstructmain.obj : error LNK2005: "struct grades mygradesa" (?mygradesa@@3Ugrades@@A) already defined in exportimportstructdef.obj
1>exportimportstructmain.obj : error LNK2005: "struct grades mygradesi" (?mygradesi@@3Ugrades@@A) already defined in exportimportstructdef.obj
1>exportimportstructmain.obj : error LNK2005: "struct grades * mygradesptro" (?mygradesptro@@3PAUgrades@@A) already defined in exportimportstructdef.obj
1>exportimportstructmain.obj : error LNK2005: "struct grades * mygradesptra" (?mygradesptra@@3PAUgrades@@A) already defined in exportimportstructdef.obj
1>C:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Projects\exportstruct\Debug\exportstruct.exe : fatal error LNK1169: one or more multiply defined symbols found
1>Build log was saved at "file://c:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Projects\exportstruct\exportstruct\Debug\BuildLog.htm"
1>exportstruct - 7 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========