including header + inheritance

Dear Friends
Hi

I have defined to classes : Parent and Child. I have some global variables in a header file named as "var.h". These variables are used in both Parent and child Classes. The source code of these classes are written below:

Parent.h
==============================================
#ifndef PARENT_H
#define PARENT_H

#pragma once

#include <stdio.h>

class Parent
{
public:
Parent(void);
void inc();
~Parent(void);
};

#endif
========================================

Parent.cpp
========================================
#include "var.h"
#include "Parent.h"

Parent::Parent(void)
{
}

Parent::~Parent(void)
{
}

void Parent::inc()
{
counter++;
}
==============================================

Child.h
=============================================
#ifndef CHILD_H
#define CHILD_H

#include "Parent.h"
//#include "var.h"
#include <stdio.h>

#pragma once

class Child : public Parent
{
public:
Child(void);
void inc();

public:
~Child(void);
};

#endif
===========================================

Child.cpp
===========================================
#include "var.h"
#include "Child.h"

Child::Child(void)
{
}

Child::~Child(void)
{
}

void Child::inc()
{
counter += 2;
}
===============================================

var.h
===============================================
#ifndef VAR_H
#define VAR_H

int counter;

#endif
==============================================

main.cpp
==============================================
#include <stdio.h>

void main() {
printf("hello");
}

After compiling, the compiler returns a fatal error as follows:

1>Parent.obj : error LNK2005: "int counter" (?counter@@3HA) already defined in Child.obj
1>C:\Documents and Settings\pishi\Desktop\test\Debug\test.exe : fatal error LNK1169: one or more multiply defined symbols found

It says the "counter" is defined multiple times. Please help me for solving this problem


Because it's defined in parent as well, so child will inherit that, and then you include it again. Add a preprocessor ifndef segment for including your headers. Should clear it up
A simple #ifndef will not solve the problem here

The problem is that global variables cannot be defined in multiple cpp files (which happens when you #include the same header in multiple cpp files).

A variable definition tells the compiler to actually make space for a variable. IE it actually gives the variable a "body". This is done in each source file as it's compiled. Then the linker tries to join all the compiled files together and it gets confused because it sees multiple variables with the same name but differen bodies.


It's a similar idea to defining a function with the same name in multiple cpp files. How can you expect the linker to know which one to use?



There are ways to make it work with the extern keyword, but don't do that here. The best solution is to just not use global variables. They're sloppy and restrictive anyway.

It seems like 'counter' should be in the scope of Parent anyway. So I would do this:

1
2
3
4
5
6
7
8
9
// parent.h

class Parent
{
//...
protected:
  static int counter;
//...
};

1
2
3
4
5
// parent.cpp

//...  put this globally - not inside any function
int Parent::counter = 0;
//... 


With this, you can remove the global counter (and var.h) completely. Now Parent and Child will both use the static Parent::counter instead.
Topic archived. No new replies allowed.