C++ simple program errors in declaring variables

This is a discussion on C++ simple program errors in declaring variables within the C++ Programming forums, part of the General Programming Boards category; Hey, i tried to write a simple C++ porgram about classes but i have some problems...take a look at my ...

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    230

    C++ simple program errors in declaring variables

    Hey, i tried to write a simple C++ porgram about classes but i have some problems...take a look at my code:

    Code:
    #include <iostream>
    
    using namespace std;
    
    class Data{
        private:
            int height;
            int age;
            char name;
        public:
            int printAll();
    };
    
    void Data::printAll(info.height){
        cout << "info.height: " << info.height << "\n";
    }
    
    int main(void){
        Data info;
            cout << "Height:";
            cin >> info.height;
            info.printAll(info.height);
    
    return 0;
    }
    output errors:
    Code:
    class_program.cpp:14: error: variable or field ‘printAll’ declared void
    class_program.cpp:14: error: ‘info’ was not declared in this scope
    class_program.cpp: In function ‘int main()’:
    class_program.cpp:7: error: ‘int Data::height’ is private
    class_program.cpp:21: error: within this context
    class_program.cpp:7: error: ‘int Data::height’ is private
    class_program.cpp:22: error: within this context
    class_program.cpp:22: error: no matching function for call to ‘Data::printAll(int&)’
    class_program.cpp:11: note: candidates are: int Data::printAll()
    Can anyone help me...? Thank you very much!

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,655
    int printAll();
    void Data::printAll(info.height)

    See the difference?

  3. #3
    Registered User
    Join Date
    Aug 2010
    Posts
    230
    can i declare the printAll (inside class) as void ?

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,014
    >>void Data::printAll(info.height){
    >>info.printAll(info.height);
    What do you think this does? Why do you think it's right?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Sep 2010
    Posts
    31
    Code:
    int printAll();
    
    void Data::printAll(info.height){
        cout << "info.height: " << info.height << "\n";
    }
    The debugger has actually told you everything you would need to solve these problems. If you don't know how to read debugger messages, that's pretty much necessary for becoming a programmer.

    Notice how printAll() has been given two different return types (<int> and <void>), and two different sets of parameters. In order to define a function that has already been prototyped, the return type and parameters have to be the same. Otherwise the compiler considers them two different functions.

    The parameter you've given it in the definition is unusable. Parameters need to have the type specified. As well, when you call a class method using the dot operator (ie, info.printAll(args)), the function automatically knows everything about the calling object (you'll get this better when you learn about keyword this). For the moment, here is a silly example to help visualize:

    Code:
    You create a class PERSON
    This class contains variables like numberOfLegs, height, and walkingSpeed
    The class also contains a method: void walk()
    
    You then create an object of class PERSON, namely Bob.
    
    When you tell Bob to walk ( ie: bob.walk() ), you don't need to tell Bob how many legs he has, how tall he is,
    or how fast he normally walks;
    All PERSONs already know these things!

  6. #6
    Registered User
    Join Date
    Sep 2010
    Posts
    41
    it is the basic concept of class that u can not access data member, declared in private access specifier, from other than public. only member function can access the data member
    as clear from the error that "int height is private".
    u r using the arguement "info.height" why? if u have nothing to do with the arguement then why u use them actually u r missing the concept of "public" and private access specifier i think u should have a look at them
    always use a member function to assign some value to data member

  7. #7
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    >>void Data::printAll(info.height){
    >>info.printAll(info.height);
    What do you think this does? Why do you think it's right?
    I, too, am curious about this. Elysia's post alone sums up the issue nicely.

  8. #8
    Registered User
    Join Date
    Sep 2010
    Posts
    41
    Code:
     
    try this and study classes again



    EDIT: Answer removed by moderator.
    Last edited by VirtualAce; 10-09-2010 at 01:55 PM.

  9. #9
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    Stop posting answers. Prefer to steer the OP in the correct direction rather than post answers. This is more than likely for homework and we do not post outright answers to homework b/c we are not a cheat site nor do we wish to become one.

    I have removed the answer.

  10. #10
    Registered User
    Join Date
    Sep 2010
    Posts
    41
    ok bubba i will be carefull next time

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program giving errors
    By andy bee in forum C Programming
    Replies: 5
    Last Post: 08-11-2010, 11:38 PM
  2. im a noob at c++, do you think so?
    By belRasho in forum C++ Programming
    Replies: 6
    Last Post: 04-26-2010, 12:02 AM
  3. Simple window program
    By baniakjr in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2006, 03:46 PM
  4. Help with simple program
    By nik2007 in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2006, 09:54 AM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21