473,387 Members | 1,534 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,387 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 4881
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.