1

So I have this header file:

#include <iostream>
#include <string>

class Furniture
{
    float width, height, depth;
    std::string name;

public:
    // Constructor
    Furniture(std::string name);
    void ReadDimensions();
    virtual void Print();
};

And this .cc file to define the functions declared above:

#include "Furniture.h"

Furniture::Furniture(std::string name)
{
    this->name = name;
}

void Furniture::ReadDimensions()
{
    // Read width
    std::cout << "Enter width: ";
    std::cin >> width;
    // Read height
    std::cout << "Enter height: ";
    std::cin >> height;
    // Read depth
    std::cout << "Enter depth: ";
    std::cin >> depth;

    if (width <= 0 || height <= 0 || depth <=0)
            std::cout << "You entered invalidd values\n";
}

And when I try to compile my main file which includes two child classes written in their own files, it gives me an error which reads

"Furniture.h:4: error: redefinition of ‘class Furniture’

Furniture.h:5: error: previous definition of ‘class Furniture’"

But as far as I know I declared the class correctly and didn't redeclare it in the definition. Why is it giving me this error and what can I do to fix it?

Roger Lipscombe
  • 89,048
  • 55
  • 235
  • 380
Henry Gridley
  • 43
  • 3
  • 9

1 Answers1

7

Try adding following code in your .h file. This will prevent redefinition.

#ifndef __FURNITURE_H__
#define __FURNITURE_H__

#include <iostream>
#include <string>

class Furniture
{
    float width, height, depth;
    std::string name;

public:
    // Constructor
    Furniture(std::string name);
    void ReadDimensions();
    virtual void Print();
};

#endif
Sudipta Kumar Sahoo
  • 1,049
  • 8
  • 16