Thread: problem

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    24

    problem

    Code:
    #include<iostream.h>
    #include<conio.h>
    
    
    class test
    {
    private:
    float a,b,c;
    int q,w;
    public:
    int test(int q, int w=4)
    {
    return(q*w);
    }
    float test(int a,int b,int c)
    {
    return ((a+b+c)/3);
    }
    };
    void main()
    {
    clrscr();
    test o,p,l;
    
    
    o=test(2,3);
    p=test(2);
    l=test(1,2,4);
    getch();
    }

    compiling error
    cant convert int to * test
    and also ::test

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I was able to fix most issues by writing main() this way.
    Code:
    #include <iostream>
    
    int main()
    {
      //clrscr();
      test o, p, l;
      float result;
    
    
      result = o.Test (2, 3);
      std::cout << "o.test(2, 3) result = " << result << std::endl;
      result = p.Test (2);
      std::cout << "p.test(2) result = " << result << std::endl;
      result = l.Test (1, 2, 4);
      std::cout << "l.test(1, 2, 4) result = " << result << std::endl;
      //getch();
    }
    On the errors I fixed: I started using iostream instead of iostream.h, because iostream.h is not a standard header and was the chief reason I could not compile your code. Then I replaced void main() with int main() as that is the standard definition. The only other thing I can say is: Do not write a method with the same name as the class. The compiler will treat such a function as a constructor, and return values are not allowed for constructors. I changed the name of the member functions accordingly just to get a build going.

    If you continue to have problems, please post complete errors.
    compiling error
    cant convert int to * test
    and also ::test
    This means NOTHING.

  3. #3
    Registered User
    Join Date
    Jun 2013
    Posts
    66
    It looks to me like a complete misunderstanding about classes and how they work. I would suggest re-reading the chapter about classes and constructors in your textbook with an eye toward correct syntax.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem passing argument into function, basic problem
    By tsdad in forum C++ Programming
    Replies: 7
    Last Post: 05-22-2013, 12:09 PM
  2. Replies: 2
    Last Post: 01-06-2013, 07:49 AM
  3. Replies: 1
    Last Post: 12-07-2012, 10:00 AM
  4. Replies: 4
    Last Post: 10-16-2008, 07:30 PM
  5. syntax linked list problem & struct problem
    By beely in forum C Programming
    Replies: 5
    Last Post: 11-11-2002, 09:14 AM