P
Panda2
I'm getting multple definition errors for the struct in the following,
and I don't know how to get around it. I've tried using extern (which
I don't really understand) but it didn't help (maybe used it wrong).
I'm compiling with Dev-Cpp and the code works when the "stuff.cpp" is
included directly in main. I'd appreciate it if someone can explain
how I can use a gloablly defined struct over several files..
ps: I know theres some goofy, crappy, and awful use of c++.. Forgive
me, I'm very bad at this...
File 1: main.cpp
/////////////////////////////
#include "globals.h"
using namespace std;
int main(int argc, char *argv[])
{
float EMA[100];
for(int a=0; a<20; a++){numbers[a].data=a;}
CalcEMA (numbers, EMA, 0.2, 1, 20);
for(int x=0; x<20; x++){cout << EMA[x] << "\n";}
system("PAUSE");
return EXIT_SUCCESS;
}
File 2: stuff.cpp
/////////////////////////////////
#include "globals.h"
VOID CalcEMA (Test_m input[], float EMA[], float fPerc, int nType, int
nTData)
{
/* Calculates exponential moving averages */
if(nType == 1){EMA[0]= input[0].data;}
for(int n=1; n<nTData; n++)
{
if(nType == 1){EMA[n]=( ( (input[n].data - EMA[n-1]) * fPerc) +
EMA[n-1] );}
}
return;
}
File 3: globals.h
////////////////////////////////////
#ifndef _GLOBALS_H_
#define _GLOBALS_H_
#include <cstdlib>
#include <iostream>
#include <windows.h>
#include <fstream>
#include <string>
#include <time.h>
struct Test_m {
float data;
}numbers[100];
VOID CalcEMA (Test_m input[], float EMA[], float fPerc, int nType, int
nTData);
#endif
/////////////////////////////////
Compiler output:
Compiler: Default compiler
Building Makefile: "C:\Dev-Cpp\TestFiles\Makefile.win"
Executing make...
make.exe -f "C:\Dev-Cpp\TestFiles\Makefile.win" all
g++.exe main.o stuff.o -o "Project1.exe" -L"C:/Dev-Cpp/lib"
stuff.o(.bss+0x0):stuff.cpp: multiple definition of `numbers'
main.o(.bss+0x0):main.cpp: first defined here
make.exe: *** [Project1.exe] Error 1
Execution terminated
and I don't know how to get around it. I've tried using extern (which
I don't really understand) but it didn't help (maybe used it wrong).
I'm compiling with Dev-Cpp and the code works when the "stuff.cpp" is
included directly in main. I'd appreciate it if someone can explain
how I can use a gloablly defined struct over several files..
ps: I know theres some goofy, crappy, and awful use of c++.. Forgive
me, I'm very bad at this...
File 1: main.cpp
/////////////////////////////
#include "globals.h"
using namespace std;
int main(int argc, char *argv[])
{
float EMA[100];
for(int a=0; a<20; a++){numbers[a].data=a;}
CalcEMA (numbers, EMA, 0.2, 1, 20);
for(int x=0; x<20; x++){cout << EMA[x] << "\n";}
system("PAUSE");
return EXIT_SUCCESS;
}
File 2: stuff.cpp
/////////////////////////////////
#include "globals.h"
VOID CalcEMA (Test_m input[], float EMA[], float fPerc, int nType, int
nTData)
{
/* Calculates exponential moving averages */
if(nType == 1){EMA[0]= input[0].data;}
for(int n=1; n<nTData; n++)
{
if(nType == 1){EMA[n]=( ( (input[n].data - EMA[n-1]) * fPerc) +
EMA[n-1] );}
}
return;
}
File 3: globals.h
////////////////////////////////////
#ifndef _GLOBALS_H_
#define _GLOBALS_H_
#include <cstdlib>
#include <iostream>
#include <windows.h>
#include <fstream>
#include <string>
#include <time.h>
struct Test_m {
float data;
}numbers[100];
VOID CalcEMA (Test_m input[], float EMA[], float fPerc, int nType, int
nTData);
#endif
/////////////////////////////////
Compiler output:
Compiler: Default compiler
Building Makefile: "C:\Dev-Cpp\TestFiles\Makefile.win"
Executing make...
make.exe -f "C:\Dev-Cpp\TestFiles\Makefile.win" all
g++.exe main.o stuff.o -o "Project1.exe" -L"C:/Dev-Cpp/lib"
stuff.o(.bss+0x0):stuff.cpp: multiple definition of `numbers'
main.o(.bss+0x0):main.cpp: first defined here
make.exe: *** [Project1.exe] Error 1
Execution terminated