473,248 Members | 1,557 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,248 software developers and data experts.

Help with Global structs in headers and multiple definitions

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

Jul 23 '05 #1
2 4872
GB
Panda2 wrote:
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


Note that you are getting a linker error, not a compiler error. It is
happening because you are giving each of your two translation units
(main.cpp and stuff.cpp) a copy of the "numbers" object, and it has
external linkage in each. You need to define only the type in the header
file, and define the variable on one of the cpp files.

Gregg
Jul 23 '05 #2
Panda2 wrote:
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 I 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..
You need to put the *definition* of numbers to stuff.cpp and
put an extern *declaration* in globals.h
You can *declare* and object as many times as you wish but
you can *define* it only once.
cat globals.h #ifndef GUARD_GLOBALS_H
#define GUARD_GLOBALS_H 1

#include <cstdlib>
#include <iostream>
// #include <windows.h>
#include <fstream>
#include <string>
#include <time.h>

struct Test_m {
float data;
};

const
size_t size = 100;
extern
Test_m numbers[100]; // declaration

float* CalcEMA(float EMA[], const Test_m input[],
float fPerc, int nType, size_t nTData);

#endif//GUARD_GLOBALS_H
cat stuff.cpp #include "globals.h"

Test_m numbers[size]; // definition

float* CalcEMA(float EMA[], const Test_m input[],
const float fPerc, const int nType, const size_t nTData) {
// Calculates exponential moving averages
if (nType == 1) {
EMA[0] = input[0].data;
}
for (size_t n = 1; n < nTData; ++n) {
if (nType == 1) {
EMA[n] = (((input[n].data - EMA[n-1])*fPerc) + EMA[n-1]);
}
}
return EMA;
}
cat main.cpp #include "globals.h"

int main(int argc, char *argv[]) {
const
size_t n = 20;
float EMA[size];
for (size_t a = 0; a < n; ++a) {
numbers[a].data = a;
}

CalcEMA(EMA, numbers, 0.2, 1, n);

for(size_t x = 0; x < n; ++x) {
std::cout << EMA[x] << std::endl;
}

system("PAUSE");
return EXIT_SUCCESS;
}
g++ -Wall -ansi -pedantic -o main main.cpp stuff.cpp
./main

0
0.2
0.56
1.048
1.6384
2.31072
3.04858
3.83886
4.67109
5.53687
6.4295
7.3436
8.27488
9.2199
10.1759
11.1407
12.1126
13.0901
14.0721
15.0576
sh: line 1: PAUSE: command not found
Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: A | last post by:
Hi, Consider this code: //Header File - Foo.h int i = 0; // non-static global variable class Foo{ ...
12
by: xxx | last post by:
I'm having a little trouble understanding why a derivative class cannot access a protected member of the base class in the following code: #include <stdio.h> class CBase { protected: int...
8
by: Jan-Henrik Grobe | last post by:
Hallo, normally I am not the one who calls a Newsgroup if something does not work. But now I am frustrated. I did write a program with a lot classes. I use some gloabl variables which are in a...
7
by: Method Man | last post by:
Can someone explain the scope/linkage differences between the following 4 global declarations and when one should be used (theoretically) over the rest? sample.h --------- #ifndef SAMPLE_H...
3
by: Keith M | last post by:
Hi, I am a bit of a newcomer to C# but have experience with (unmanaged) C++. Now, I have a 3rd party dll and headers. This dll is a C++ style dll that exports classes and structs. I am...
1
by: Rahul | last post by:
Hi Everybody I have some problem in my script. please help me. This is script file. I have one *.inq file. I want run this script in XML files. But this script errors shows . If u want i am...
8
by: yossi.kreinin | last post by:
Hi! When are multiple definitions of global variables with the same name considered legal in C, and how is it different from C++? It appears that in terms of assembly language, some C...
10
by: subramanian100in | last post by:
Suppose I declare a global variable int g; in two different files say a.c which has main() and b.c When I compile them to build an executable under gcc in Redhat Linux with the command ...
1
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.