Thread: "class" problem

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    4

    "class" problem

    Hi everyone,
    here is my question
    I wrote a class as follows
    I want to output the numbers if sh(n)==2or3or5or7
    the problem is that error appears
    C2440 cannot convert from 'int' to 'sh'

    I know that the sh() in class sh cannot return numbers
    but I don't know how to fix this problem.

    thanks for helping.

    Code:
    class sh:public Item{
    	int n;
    public:
    	int out();
    	int th(int x){ 	
    		int sum=0;
    		for(int i=10;n>0;){
    		sum+=x%10;
    		x=x/10;
    		} return x;
    	}
    	sh(Item *src,int x):Item(src){           
    		while(x>10){
    		x=th(x);
    		} 
    	}
    };
    int sh::out() {
    	int n=source->out();
    	source=new Filter(source,n);
    	if(sh(n)==2||3||5||7) return n; 
    }

  2. #2
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    you would need a constructor for sh that takes an int (is th a type-o in your code above?). Then for the == comparison, you need to overload the == operator. Check out the FAQ for that.

  3. #3
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Quote Originally Posted by tinkywinky
    Hi everyone,


    Code:
    	if(sh(n)==2||3||5||7) return n; 
    }
    ok first you cannot do if x == 2 || 3 || whatever
    you must do
    Code:
    int x = 0;
    // some code
    if (x == 2 || x == 3 || x == 4 // and so on, you get the idea
    that's just your first problem!! read up on class syntax again.
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    4
    what is type-o?
    sorry I'm not familiar with class

    Is there any other way to solve the problem without
    overload the operator??
    I thought the problem is that sh(n) isn't returning numbers to
    sh:ut() so I cannot compare sh(n) with numbers
    but Im not sure...

    thanks a lot!

  5. #5
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    As ChaosEngine pointed out, there is quite a bit wrong with your code. Read the FAQ to learn more about classes, or try a procedural approach.

  6. #6
    Registered User
    Join Date
    Oct 2005
    Posts
    4
    Thank you guys!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM