Questions on Class and Function Calling

This is a discussion on Questions on Class and Function Calling within the C++ Programming forums, part of the General Programming Boards category; 1. Let's look at the following: #include <iostream.h> class a { private: int x; public: a() {x=0;} a(int z) {x=z;} ...

  1. #1
    Fud
    Guest

    Question Questions on Class and Function Calling

    1. Let's look at the following:

    #include <iostream.h>

    class a {
    private:
    int x;
    public:
    a() {x=0;}
    a(int z) {x=z;}
    };

    int main() {
    a test1; // first command
    a test2(3); // second command
    a test3(); // third command
    return (0);
    }

    I understand why the first and second command creates no error. But I dont understand why the third command always creates error? Is it normal? Do there exist any method to eliminate the problem? To me, the third command is virtually the same as the first command. But now the thrid creates the error but not the first one. So strange.

    2. Let's look at the following:

    #include <iostream.h>

    void a() { // do something}

    int main () {
    a; // first command
    a(); // second command
    a(void); // third command
    return (0);
    }

    Now, the first command creates error but not the second one. I want to know why. And what will happen to the third command?

    Thanks for answering.

  2. #2
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    1. Let's look at the following:
    ...
    Is it normal?
    Yes.

    Do there exist any method to eliminate the problem?
    No, because it's not legal C++. Take another look at it. It looks alot like a function declaration doesn't it, which gives the compiler headaches.

    2. Let's look at the following:
    ...
    Now, the first command creates error but not the second one. I want to know why.
    because the second one calls the function , whereas the first is just the function name.

    And what will happen to the third command?
    It'll fail to compile. void in this sense is used in declarations not in the calling of functions.

  3. #3
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    [quoteDo there exist any method to eliminate the problem?[/quote]

    On second thoughts if you really want to use this syntax to call the default construct you can use new -


    a* test3=new a();

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 09:18 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-30-2005, 12:21 AM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM

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