Jump to content

redefinition of variables?!

- - - - -

  • Please log in to reply
6 replies to this topic

#1
alirezan

alirezan

    Learning Programmer

  • Members
  • PipPipPip
  • 62 posts
Hi

I have 4 simple files. Here they are:

base.cpp

Quote

#include <stdio.h>
#include <iostream>

#include "derived.h"

base::base(void)
{
}


base::~base(void)
{
}

int base::dispatch (int eventid, int stateid)
{
return 0;
}

int main (void)
{
derived d;
derived *dp = &d;

dp->dispatch (1,2);
getchar();
return 1;
}

base.h

Quote

#ifndef BASE_H_
#define BASE_H_



enum { EVENT_STARTUP, // E-00
EVENT_GPS, // E-01 - GPS coordinates
EVENT_NOGPS, // E-02 - No GPS coordinates or fix
EVENT_FDOOR_OPENED, // E-03 - Front door opened
EVENT_RDOOR_OPENED, // E-04 - Rear door opened
EVENT_FDOOR_CLOSED, // E-05 - Front door closed
EVENT_RDOOR_CLOSED, // E-06 - Rear door closed
EVENT_STOP_REQUESTED, // E-07
EVENT_LOGIN, // E-08
EVENT_LOGOUT, // E-09
EVENT_DISABLED, // E-10
EVENT_ENABLED, // E-11
EVENT_PA_DEACTIVATED, // E-12
EVENT_PA_ACTIVATED, // E-13
EVENT_ROUTE_UPDATED, // E-14
MAX_EVENTS // Maximum number events. WARNING: This has to be the last enum item
};

enum { STATE_DISABLED, // ST-00
STATE_NOT_IN_SERVICE, // ST-01
STATE_ENTRY_TRIGGER, // ST-02
STATE_EXIT_TRIGGER, // ST-03
STATE_OPERATING, // ST-04
STATE_ARRIVED, // ST-05
MAX_STATES // maximum number of states. WARNING: This has to be the last enum item
};


/* Managers */
//const char *kNextstop = "Nextstop";
//const char *kSignage = "Signage";
//const char *kAudio = "Audio";
//const char *kMdt = "Mdt";
//const char *kEvent = "Event";
//const char *kAnnunciation = "Annunciation";
//const char *kDevice = "Device";

/* Annunciation action codes */
const unsigned char kFuncAnnDisplay = 0x01; // Display text message on sign
const unsigned char kFuncAnnClearSign = 0x02; // Clear sign
const unsigned char kFuncAnnPlay = 0x03; // play sound on audio
/* Event action codes */
const unsigned char kFuncEventNewGps = 1; // Event action: new GPS coordinates
const unsigned char kFuncEventLostGps = 2; // Event action: lost GPS coordinates
const unsigned char kFuncEventFrontDoorOpen = 3; // Event action: front door opened
const unsigned char kFuncEventRearDoorOpen = 4; // Event action: rear door opened
const unsigned char kFuncEventFrontDoorClose = 5; // Event action: front door closed
const unsigned char kFuncEventRearDoorClose = 6; // Event action: rear door closed
const unsigned char kFuncEventStopRequest = 7; // Event action: stop requested
const unsigned char kFuncEventPaDeactivated = 12; // Event action: PA system deactivated
const unsigned char kFuncEventPaActivated = 13; // Event action: PA system activated
const unsigned char kFuncEventMiddleDoorOpen = 15; // Event action: middle door opened
const unsigned char kFuncEventMiddleDoorClose = 16; // Event action: middle door closed
const char *kFrontDoorOpen = "front door opened";
const char *kRearDoorOpen = "rear door opened";
const char *kMiddleDoorOpen = "middle door opened";
const char *kFrontDoorClose = "front door closed";
const char *kRearDoorClose = "rear door closed";
const char *kMiddleDoorClose = "middle door closed";
const char *kStopRequest = "stop requested";
const char *kNewGps = "new GPS coordinates";
const char *kLostGps = "lost GPS coordinates";
const char *kPaActivated = "PA system activated";
const char *kPaDeactivated = "PA system deactivated";
/* MDT action codes */
const unsigned char kFuncMdtOnLine = 1; // MDT action: on line
const unsigned char kFuncMdtOffLine = 2; // MDT action: off line
const unsigned char kFuncMdtLogIn = 8; // MDT action: log in
const unsigned char kFuncMdtLogOut = 9; // MDT action: log out
const char *kOnLine = "on line";
const char *kOffLine = "off line";
const char *kLogIn = "log in";
const char *kLogOut = "log out";

class base
{
public:
base(void);
virtual int dispatch (int /*eventid*/, int /*stateid*/);
virtual ~base(void);
};


#endif

derived.cpp

Quote

#include "derived.h"
#include <stdio.h>
#include <iostream>

derived::derived(void)
{
}

int derived::dispatch (int eventid, int stateid)
{
printf ("Dispatch, %d, %d" ,eventid, stateid);
return 0;
}

derived::~derived(void)
{
}

when I compile it says:

Quote

g++ -o tst base.cpp derived.cpp
/tmp/ccP4cQU0.o:(.data+0x0): multiple definition of `kFrontDoorOpen'
/tmp/cck1KJZa.o:(.data+0x0): first defined here
/tmp/ccP4cQU0.o:(.data+0x4): multiple definition of `kRearDoorOpen'
/tmp/cck1KJZa.o:(.data+0x4): first defined here
/tmp/ccP4cQU0.o:(.data+0x8): multiple definition of `kMiddleDoorOpen'
/tmp/cck1KJZa.o:(.data+0x8): first defined here
/tmp/ccP4cQU0.o:(.data+0xc): multiple definition of `kFrontDoorClose'
/tmp/cck1KJZa.o:(.data+0xc): first defined here
/tmp/ccP4cQU0.o:(.data+0x10): multiple definition of `kRearDoorClose'
/tmp/cck1KJZa.o:(.data+0x10): first defined here
/tmp/ccP4cQU0.o:(.data+0x14): multiple definition of `kMiddleDoorClose'
/tmp/cck1KJZa.o:(.data+0x14): first defined here
/tmp/ccP4cQU0.o:(.data+0x18): multiple definition of `kStopRequest'
/tmp/cck1KJZa.o:(.data+0x18): first defined here
/tmp/ccP4cQU0.o:(.data+0x1c): multiple definition of `kNewGps'
/tmp/cck1KJZa.o:(.data+0x1c): first defined here
/tmp/ccP4cQU0.o:(.data+0x20): multiple definition of `kLostGps'
/tmp/cck1KJZa.o:(.data+0x20): first defined here
/tmp/ccP4cQU0.o:(.data+0x24): multiple definition of `kPaActivated'
/tmp/cck1KJZa.o:(.data+0x24): first defined here
/tmp/ccP4cQU0.o:(.data+0x28): multiple definition of `kPaDeactivated'
/tmp/cck1KJZa.o:(.data+0x28): first defined here
/tmp/ccP4cQU0.o:(.data+0x2c): multiple definition of `kOnLine'
/tmp/cck1KJZa.o:(.data+0x2c): first defined here
/tmp/ccP4cQU0.o:(.data+0x30): multiple definition of `kOffLine'
/tmp/cck1KJZa.o:(.data+0x30): first defined here
/tmp/ccP4cQU0.o:(.data+0x34): multiple definition of `kLogIn'
/tmp/cck1KJZa.o:(.data+0x34): first defined here
/tmp/ccP4cQU0.o:(.data+0x38): multiple definition of `kLogOut'
/tmp/cck1KJZa.o:(.data+0x38): first defined here
collect2: ld returned 1 exit status



how am i redefining things? I am confused and can't try to figure out what i'm doing wrong! Any help would be greately appreciated!

Thanks

#2
irancplusplus

irancplusplus

    Learning Programmer

  • Members
  • PipPipPip
  • 65 posts
did you forget derive.h!
I wrote this ebook! Will you translate it into English for free!?:confused: PM me!

#3
alirezan

alirezan

    Learning Programmer

  • Members
  • PipPipPip
  • 62 posts

irancplusplus said:

did you forget derive.h!

Ah! you're right! I'm sorry!

Here it is:

derived.h

Quote


#ifndef DERIVED_H
#define DERIVED_H

class derived : public base
{
public:
derived();
int dispatch (int eventid, int stateid);
virtual ~derived();
};


#endif


#4
irancplusplus

irancplusplus

    Learning Programmer

  • Members
  • PipPipPip
  • 65 posts
add
#include "base.h"
in derived.h
and add static in front of all const definitions:

#ifndef BASE_H_

#define BASE_H_




enum { EVENT_STARTUP, // E-00

EVENT_GPS, // E-01 - GPS coordinates

EVENT_NOGPS, // E-02 - No GPS coordinates or fix

EVENT_FDOOR_OPENED, // E-03 - Front door opened

EVENT_RDOOR_OPENED, // E-04 - Rear door opened

EVENT_FDOOR_CLOSED, // E-05 - Front door closed

EVENT_RDOOR_CLOSED, // E-06 - Rear door closed

EVENT_STOP_REQUESTED, // E-07

EVENT_LOGIN, // E-08

EVENT_LOGOUT, // E-09

EVENT_DISABLED, // E-10

EVENT_ENABLED, // E-11

EVENT_PA_DEACTIVATED, // E-12

EVENT_PA_ACTIVATED, // E-13

EVENT_ROUTE_UPDATED, // E-14

MAX_EVENTS // Maximum number events. WARNING: This has to be the last enum item

};


enum { STATE_DISABLED, // ST-00

STATE_NOT_IN_SERVICE, // ST-01

STATE_ENTRY_TRIGGER, // ST-02

STATE_EXIT_TRIGGER, // ST-03

STATE_OPERATING, // ST-04

STATE_ARRIVED, // ST-05

MAX_STATES // maximum number of states. WARNING: This has to be the last enum item

};



/* Managers */

//const char *kNextstop = "Nextstop";

//const char *kSignage = "Signage";

//const char *kAudio = "Audio";

//const char *kMdt = "Mdt";

//const char *kEvent = "Event";

//const char *kAnnunciation = "Annunciation";

//const char *kDevice = "Device";


/* Annunciation action codes */

static const unsigned char kFuncAnnDisplay = 0x01; // Display text message on sign

static const unsigned char kFuncAnnClearSign = 0x02; // Clear sign

static const unsigned char kFuncAnnPlay = 0x03; // play sound on audio

/* Event action codes */

static const unsigned char kFuncEventNewGps = 1; // Event action: new GPS coordinates

static const unsigned char kFuncEventLostGps = 2; // Event action: lost GPS coordinates

static const unsigned char kFuncEventFrontDoorOpen = 3; // Event action: front door opened

static const unsigned char kFuncEventRearDoorOpen = 4; // Event action: rear door opened

static const unsigned char kFuncEventFrontDoorClose = 5; // Event action: front door closed

static const unsigned char kFuncEventRearDoorClose = 6; // Event action: rear door closed

static const unsigned char kFuncEventStopRequest = 7; // Event action: stop requested

static const unsigned char kFuncEventPaDeactivated = 12; // Event action: PA system deactivated

static const unsigned char kFuncEventPaActivated = 13; // Event action: PA system activated

static const unsigned char kFuncEventMiddleDoorOpen = 15; // Event action: middle door opened

static const unsigned char kFuncEventMiddleDoorClose = 16; // Event action: middle door closed

static const char *kFrontDoorOpen = "front door opened";

static const char *kRearDoorOpen = "rear door opened";

static const char *kMiddleDoorOpen = "middle door opened";

static const char *kFrontDoorClose = "front door closed";

static const char *kRearDoorClose = "rear door closed";

static const char *kMiddleDoorClose = "middle door closed";

static const char *kStopRequest = "stop requested";

static const char *kNewGps = "new GPS coordinates";

static const char *kLostGps = "lost GPS coordinates";

static const char *kPaActivated = "PA system activated";

static const char *kPaDeactivated = "PA system deactivated";

/* MDT action codes */

static const unsigned char kFuncMdtOnLine = 1; // MDT action: on line

static const unsigned char kFuncMdtOffLine = 2; // MDT action: off line

static const unsigned char kFuncMdtLogIn = 8; // MDT action: log in

static const unsigned char kFuncMdtLogOut = 9; // MDT action: log out

static const char *kOnLine = "on line";

static const char *kOffLine = "off line";

static const char *kLogIn = "log in";

static const char *kLogOut = "log out";


class base

{

public:

	base(void);

	virtual int dispatch (int /*eventid*/, int /*stateid*/);

	virtual ~base(void);

};



#endif

now your program can be run. but I don't know what it is doing.
I wrote this ebook! Will you translate it into English for free!?:confused: PM me!

#5
alirezan

alirezan

    Learning Programmer

  • Members
  • PipPipPip
  • 62 posts
awesome! Thanks alot. It's not gonna do anything yet because it's not complete!
Thanks alot

#6
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
Putting static in front of global variables means you can't call them outside of scope of that file. I doubt that's OP's intention since all of them are in header file.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#7
irancplusplus

irancplusplus

    Learning Programmer

  • Members
  • PipPipPip
  • 65 posts
there are 2 other ways to solve the problem:
1) using unnamed namespace in header file.
2) transferring definitions to base.cpp (and adding extern instead of static)!
the last solution is most appropriate.
I wrote this ebook! Will you translate it into English for free!?:confused: PM me!




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users