Link to home
Create AccountLog in
Avatar of IssacJones
IssacJones

asked on

"Already defined" problem

Hiya

I'm hoping that this is a fairly trivial question???

I have a header file in which I have added a namespace declaration e.g.

#ifndef __MYAREA_H__
#define __MYAREA_H__

#pragma once

namespace MyArea{

          int countingThings;

};

#define __MYAREA_H__

Now, I have added this header to the header of my Document, and in the Document source I have written

MyArea::coutingThings = 5;

but when I compile this I get

LNK2005 int MyArea::countingThings already defined in (loads of object files)

I'm guessing this is a really silly mistake but I can't see what I've done wrong.

Can anybody help?
ASKER CERTIFIED SOLUTION
Avatar of Zoppo
Zoppo
Flag of Germany image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Avatar of IssacJones
IssacJones

ASKER

I have split the points because both your answers came at the same time. As Zoppo was the first in the list he gets the majority but I liked the way of intializing that infinity suggested. I hope this is acceptable. Thanks to both of you.
mmmmm

although I've closed this one, and I've been able to initialize the variables by using

#include "myarea.h"
 
int MyArea::countingThings = 0;

what I cannot still do is use MyArea::countingThings in the code. That is, I can't do something like
 
int x = MyArea::countingThings + 17;

in the functions of the cpp file.

Can you tell me what I'm doing wrong?
Hi IssacJones,

in which way doesn't it work? Compiler/linker error or runtime problems? Is 'myarea.h' included in the file where you want to use it?

In general this should work ...

Best regards,

ZOPPO
Hi Zoppo

I get

error LNK2001: unresolved external symbol "int MyArea::countingThings"
>> error LNK2001: unresolved external symbol "int MyArea::countingThings"

Did you add the .cpp file that contains the line :

        int MyArea::countingThings = 0;

to your project ? Was it correctly compiled and linked to your program ?
Ah, right, sorry, I tried to compile it using

int x = MyArea::countingThings + 17;

but without initializing it at the top of the cpp file i.e.

int MyArea::countingThings = 0;

when I do both it compiles.

You live and learn eh!

As an afterthought, suppose I wanted to have global variables x,y,z in the MyArea namespace but only global to one file (well, the cpp and the header) is there an easier way to do the above?

Thanks again for your help.
A variable declared at file scope always has external linkage (ie. can be used from the other compilation units), unless it has been declared with the static storage class specifier, in which case the variable has internal linkage (ie. can only be used in the compilation unit where the variable was defined).