0

Greetings oh mighty coders,

I am a beginner and in a bit of trouble here.

There is my baseclass (sensor.h):

class sensor
{
private:
   int sensor_id;
   string sensor_name;
   string sensor_type;
   float reading;

public:

   sensor();
   sensor(int, char*, char*);
   ~sensor();

/* Few extra methods here */

};

... and I want to create 4 other classes that inherit from my baseclass sensor (temperaturesensor, humiditysensor... and so on).

 #include "sensor.h"


class temperaturesensor:public sensor
{
public:
   Temperatursensor(int, char*,char*);
   ~Temperatursensor();

/* Few extra methods here */

};

Thing is: Every single one of these classes has to be in its own .cpp/.h file and then be included and used in my main.cpp.

using namespace std;
#include <xyz.h>
/* Other libaries here */
          ....
#include "temperaturesensor.h"
#include "humiditysensor.h"

int main()
{
    sensor* station[2];


    station [0] = new temperaturesensor(x,y,z);
    station [1] = new humiditysensor(x,y,z);
}

If I include one of them it's no biggie. However: If I use multiple ones I get an redefinition error.

error C2011: 'sensor': 'class' typeredefinition c:\users\name\desktop\project\sensor.h 14

error c2011: 'temperaturesensor' : 'class' typeredefinition 

What can I do to workaround this? Note that I am not allowed to use #pragma once

Sorry for my stupidity and thanks in advance!

Cnewbie
  • 23
  • 2
  • 3
    You can use standard *include guards* in place of the non-portable `#pragma once`. – Bathsheba Jun 29 '17 at 15:52
  • Welcome to Stack Overflow. Please take the time to read [The Tour](http://stackoverflow.com/tour) and refer to the material from the [Help Center](http://stackoverflow.com/help/asking) what and how you can ask here. – πάντα ῥεῖ Jun 29 '17 at 15:53
  • 1
    You do have [*include guards*](https://en.wikipedia.org/wiki/Include_guard) or [`#pragma once`](https://en.wikipedia.org/wiki/Pragma_once) in your header files? – Some programmer dude Jun 29 '17 at 15:53
  • 1
    Do you remember, that array is numarate from 0? – 21koizyd Jun 29 '17 at 15:53
  • And can you please edit your question *show* the errors? Copy-paste them (as text). – Some programmer dude Jun 29 '17 at 15:53
  • Since you are using `std::string` in your class, use `std::string` in your constructor's parameters. The compiler can convert `char *` to `std::string`, so you can call the constructor with character literals. – Thomas Matthews Jun 29 '17 at 15:56
  • Btw, `station[2]` is an out-of-bounds access and hence undefined behaviour. What you presumably meant was to use indices `0` and `1`, not `1` and `2`. – Walter Jun 29 '17 at 15:58

3 Answers3

2

You must use:

#ifndef FILE_H
#define FILE_H

.. normal code here

#endif

or

#pragma once

but too, I think, that sensor schould be abstract class and you schould use virtual destructor. One more think is that array is numerate from 0.

21koizyd
  • 1,843
  • 12
  • 25
  • Good catch for indexing of the array! However, I do not see the need for an abstract base class. – AGN Gazer Jun 29 '17 at 16:01
  • 1
    Don't create symbols with a leading underscore followed by an upper-case letter, [those are reserved in all scopes](http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier). – Some programmer dude Jun 29 '17 at 16:01
  • @AGNGazer I see that class abstract sensor and specialization to particular sensors – 21koizyd Jun 29 '17 at 16:04
0

you forgot to use the include guards in your header class, this is redefining your base class everytime you use it.

so, just do a

#pragma once

or a normal include guard

#ifndef YOURFILENAME_H
#define YOURFILENAME_H

.. normal code here

#endif

Then you will not have the multiple definition error.

Tomaz Canabrava
  • 2,320
  • 15
  • 20
0

The definition of the class sensor is coming from both "temperaturesensor.h" and "humiditysensor.h". Use guards https://en.wikipedia.org/wiki/Include_guard or #pragma once: https://en.wikipedia.org/wiki/Pragma_once

AGN Gazer
  • 8,025
  • 2
  • 27
  • 45