0

I have the main class "arm", it has many attributes like long or elbow, but it has as an attribute one object of the class "hand". Hand has different methods and attributes so it´s important to make it as a different class.

My question is how to use an object as an attribute of another class on c++? (i´m using Microsoft Visual Studio)

the class hand is

on hand.h

class hand
{
 private:
      int number_of_fingers;
 public:
      void handleSomthing();
}

and the class arm is

on arm.h

#include "hand.h"
class arm
{
 private: 
      float long;
      int elbow;
      hand right;
}

But when I include "hand.h" on arm an error occurs saying

error C2011: 'hand': new definition of type 'class'

If I try to use hand.h and create and use objects on the main program it works, so the problem is not on the class hand I think.

I´m trying to learn OOP and I´ll really appreciate your help

songyuanyao
  • 147,421
  • 15
  • 261
  • 354

2 Answers2

0

The class declarations needs a semicolon at the end.

class hand { private: int number_of_fingers; public: void handleSomthing(); };

#include "hand.h"
class arm { private: float long; int elbow; hand right; };
songyuanyao
  • 147,421
  • 15
  • 261
  • 354
0

arm.h

#include "hand.h"

class arm 
{ 
private: 
    float length; 
    int elbow; 
    hand right; 
};

hand.h

class hand 
{
private:
    int number_of_fingers;
public:
    void handleSomthing(); 
};

You cannot name a variable name long:

float long

long is a type, you should name it lenght

float length
Francois Girard
  • 310
  • 3
  • 13
  • Thanks apparently the mistake was that I wrote #include "hand.h" on the main program (main.cpp) and on the top of arm.h. So when i created an object from the class arm I "included" twice the header hand.h The correct way is include hand.h only on the file where arm is written. Thanks for answering Jenn – Jenn Sierra Apr 05 '16 at 18:05
  • You have to put compiler flag on top of your header file so you can include it multiple times. See [here](http://stackoverflow.com/questions/1653958/why-are-ifndef-and-define-used-in-c-header-files) – Francois Girard Apr 06 '16 at 19:52