CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 2010
    Posts
    26

    What is wrong in this program?

    What i am trying to do here is to pass an argument from int main() to num2 then using the same value pass the argument to num1 but the compiler is acting like the arguments passed to num1 does not belong to the class' private member. What's the problem and what is the problem's name? I want to find it in the net or textbook.

    Code:
    #include<iostream>
    #include <string>
    
    using namespace std;
    class num1
    {
    int a;
    int b;
    
    public:
    
    num1(int h,int g):a(h),b(g){};
    
    int addnum()
    {
    return a+b;
    }
    
    };
    
    class num2
    {
    num1 c( int x, int y);
    
    public:
    
    num2 (int a, int b)
    {
    x=a;
    y=b;
    
    }
    
    int funct()
    {return c.addnum();}
    };
    
    int main()
    {
    num2 f(1,2);
    cout<<f.funct();
    return 0;
    }
    Last edited by hayloiuy; July 23rd, 2010 at 11:53 PM. Reason: Not specific

  2. #2
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    Re: What is wrong in this program?

    1. You have the following:

    Code:
    class num2
    {
       num1 c( int x, int y);
    That does not declare an object of type num1 ... In fact, I'm not
    sure what it does. You just want:

    Code:
    class num2
    {
       num1 c;

    2. You also have:

    Code:
    public:
    
    num2 (int a, int b)
    {
       x=a;
       y=b;
    }
    class num2 does not have member variables "x" and "y" (num1 does,
    but not num2).

    3. You want to do the following:

    Code:
    class num2
    {
        num1 c; //( int x, int y);
    
    public:
    
        num2 (int a, int b) : c(a,b) 
        {
        //  x=a;
        //  y=b;
        }
    
        int funct()
        {
            return c.addnum();
        }
    };

  3. #3
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: What is wrong in this program?

    Quote Originally Posted by Philip Nicoletti View Post
    1. You have the following:

    Code:
    class num2
    {
       num1 c( int x, int y);
    That does not declare an object of type num1 ... In fact, I'm not
    sure what it does.
    This declares a function called c, that takes two int as argument, and returns a num1. This is why it compiles, but the program fails later.

    To answer the original question though, there is no real name to the problem. You did several mistakes that interfered with each other. I think Philip Nicoletti was able to catch them all.

    The most important is to remember to declare funct as a member function inside your class, or it won't have access to c. That, or pass a num1 argument to funct.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  4. #4
    Join Date
    Jan 2009
    Posts
    69

    Re: What is wrong in this program?

    Philip and monarch covered most of the actual code issues, so I'm going to bring a bit of a sledge hammer down about the way the code was written (don't worry, it's a constructive hammer, we've all written code like that once upon a time ) So this is all with positive intent.

    Your code is a little hard to read and inconsistent.

    First thing, indents indents indents! Indents!

    Code:
    class num2
    {
        num1 c( int x, int y);
    
    public:
    
        num2 (int a, int b)
        {
            x=a;
            y=b;
    
        }
    
        int funct()
        {return c.addnum();}
    };
    is much easier to read than what you had before. General rule, indent after every { and come back one after every }.

    Variable names, class names, and function names could be a little more tangible. I know this is just a test class, but you see already where x,y,a,b,f,c,h and g get pretty confusing and easy to lose track of who has what pretty quickly. I know you're just starting out, so it's easy to sling one-letter-variables around and name functions "function". Even just MINOR tweaks like:

    Code:
    class num1
    {
        int a;
        int b;
    
    public:
    
        num1(int valueA, int valueB) : a(valueA), b(valueB) {};
    
        int addnum()
        {
            return a+b;
        }
    
    };
    is slightly cleaner in my opinion. I'll admit that's not really the best example, but it gets the idea across a little.

  5. #5
    Join Date
    Apr 2010
    Posts
    26

    Re: What is wrong in this program?

    Quote Originally Posted by Philip Nicoletti View Post
    1. You have the following:

    Code:
    class num2
    {
       num1 c( int x, int y);
    That does not declare an object of type num1 ... In fact, I'm not
    sure what it does. You just want:

    Code:
    class num2
    {
       num1 c;

    2. You also have:

    Code:
    public:
    
    num2 (int a, int b)
    {
       x=a;
       y=b;
    }
    class num2 does not have member variables "x" and "y" (num1 does,
    but not num2).

    3. You want to do the following:

    Code:
    class num2
    {
        num1 c; //( int x, int y);
    
    public:
    
        num2 (int a, int b) : c(a,b) 
        {
        //  x=a;
        //  y=b;
        }
    
        int funct()
        {
            return c.addnum();
        }
    };
    Thanks Phillip!! That answered my question!! To everyone else i sincerely thank you for your further explanation and sorry about the indentation. I thought it was neat for me. I assure you guys there would be no such thing ever again. From what i understand from you guys explained that:

    1) in
    Code:
     num1(int x, int y)
    the x and y is pretty much "private" to num1 only. I can't initialize them in class num2 unless i use constructor initializer.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured