Thread: problème declaration entre deux class

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    87

    class defiition problem

    Bonjour,
    (je suis débutante dans la programmation orienté objet)
    j'ai une class A où j'ai déclaré une variable
    Code:
    
      class A { private: int x; }
    j'ai déclaré une autre class B dans le projet mais dans un autre fichier

    quand j'utilise x il me dit que qu'il n'est pas déclaré

    comment je déclare x pour qu'il être connus par B?
    Last edited by dreamvig; 05-09-2012 at 01:26 AM.

  2. #2
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    dreamvig, ici on parle seulement anglais!
    You have also posted in the wrong forum, this should go into the C++ forum. Translation:


    Hello
    (I'm new to object oriented programming)
    I have a class a where I declare a variable
    Code:
    class A { private: int x; }
    I have declared another class B in the project but in a different file

    when I use x it tells me that it isn't declared.

    How can I declare x so that it is visible to B?
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  3. #3
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    The answer is in the declaration of x. You have set it specifically to be private, hence it is only accessible from within A. To set or read x, you should either make it public (goes against encapsulation though) or create accessor functions that act on x.
    You will also have to instantiate class A somewhere to be able to access x (unless you make x static).
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  4. #4
    Registered User
    Join Date
    Apr 2012
    Posts
    87
    thank you
    i forget that i am in english forum

  5. #5
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Une traduction magnifique QuantumPete!
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Should it be legalized - Part Deux
    By medievalelks in forum A Brief History of Cprogramming.com
    Replies: 124
    Last Post: 06-11-2008, 06:17 AM
  2. Class Declaration
    By enjoy in forum C++ Programming
    Replies: 4
    Last Post: 03-10-2006, 10:59 AM
  3. Replies: 7
    Last Post: 05-26-2005, 10:48 AM
  4. Replies: 2
    Last Post: 04-06-2005, 07:25 AM
  5. Class Declaration?
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 07-27-2002, 09:09 PM