Program error

This is a discussion on Program error within the C++ Programming forums, part of the General Programming Boards category; My program should read five integers from a file called sample.txt, then print out the integers, add them, etc. using ...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    74

    Program error

    My program should read five integers from a file called sample.txt, then print out the integers, add them, etc. using the overload functions. I seem to be having a problem with my isElement, insert and remove functions, but I'm not a strong coder so I just can't figure this out!! Here is my code, then my 3 errors follow. Any help you can give is appreciated!!

    Code:
    #include<iostream.h>
    #include<fstream.h>
    
    struct Node{
    	char digit;
    	Node *next;
    };
    
    class BigInt{
    	Node *Num;
    	int cap;
    	int top;
    public:
    
    	void insert ( int);
    	void remove (int);
    	int isEmpty () {return !top;};
    	int isElement (int);
    	int size() {return top;};
    	friend BigInt operator+(BigInt A, BigInt B);
    	friend BigInt operator-(BigInt A, BigInt B);
    	friend istream& operator>>(istream& in, BigInt &B);
    	friend ostream& operator<<(ostream& o, BigInt &B);
    };
    
    int BigInt::isElement (int target)
    {
    	for (int i=0; i < top; i++)
    		if (Num [i] == target)  //ERROR 1
    			return 1;
    
    		return 0;
    }
    
    void BigInt::insert (int target)
    {
    	if (isElement(target))
    		return;
    
    	if (top == cap) {
    		Node *temp = new Node [cap + 5];
    		for (int i=0; i<top; i++) temp [i] = Num->next [i];
    		cap =+5;
    		delete []Num->next;
    		Num->next = temp;
    	}
    	Num->next [top++] = Num.target; //ERROR 2
    }
    
    void BigInt::remove (int target)
    {
    	char i;
    
    	if (! isElement (target))
    		return;
    
    	for (i = 0; i < cap; i ++)
    		if (Num[i] == target) //ERROR 3
    			break;
    
    		while (i < cap -1){
    			Num [i] = Num [i+1];
    			i++;
    		}
    		top --;
    }
    
    BigInt operator+(BigInt A, BigInt B)
    {
        A.Num->digit += B.Num->digit;
        return A;
    }
    
    BigInt operator-(BigInt A, BigInt B)
    {
        A.Num->digit -= B.Num->digit;
        return A;
    }
    
    istream& operator>>(istream& in, BigInt &B)
    {
    	in >> B.Num->digit;
    	return in;
    }
    
    ostream& operator<<(ostream& o, BigInt &B)
    {
    	o << B.Num->digit;
    	return o;
    }
    
    
    
    void main()
    {
    	BigInt v1, v2, v3, v4, v5;
    
    	ifstream in;
    
    	in.open("sample.txt");
    
    	in >> v1 >> v2 >> v3 >> v4 >> v5;
    
    	cout << v1+v2 << endl;
    	cout << v3-v4 << endl;
    }
    errors (line location shown in code above):

    1. binary '==' : 'struct Node' does not define this operator or a conversion to a type acceptable to the predefined operator

    2. left of '.target' must have class/struct/union type

    3. binary '==' : 'struct Node' does not define this operator or a conversion to a type acceptable to the predefined operator

    Thanks!!

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >if (Num [i] == target) //ERROR 1
    Num is a Node instance, there's no valid conversion from a Node to an int, so the compiler complains about it.

    >Num->next [top++] = Num.target; //ERROR 2
    Num is a pointer, try using the arrow operator instead (->).

    >if (Num[i] == target) //ERROR 3
    Same as error 1.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    74
    Thanks Prelude!!

    The arrow operator on

    Code:
    >Num->next [top++] = Num.target; //ERROR 2
    Num is a pointer, try using the arrow operator instead (-> ).
    isn't working. Is this what you meant:

    Code:
    Num->next [top++] = Num->target;
    If so, getting error that target is not a member of node.
    I think I have the other errors worked out.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >isn't working. Is this what you meant:
    Yes, but I didn't really look at the expression. Now that I do, I have a pressing question: What exactly are you trying to accomplish with this:
    Code:
    Num->next [top++] = Num.target;
    What the left hand side is expecting is a simple Node, so the assignment is wrong, target also isn't a member of Node. Since you're dereferencing next and then moving the pointer forward a bit you probably won't get a memory location you want anyway.

    -Prelude
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    May 2002
    Posts
    74
    Ok. Maybe I'm going about this wrong. The purpose of the function is to insert the data. I copied this function from another program and then made changes to accomodate this program. But somewhere in translation I am getting confused, and considering I'm confused with Node's to begin with, I'm completely lost. How do I make this function work??

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >But somewhere in translation I am getting confused, and considering I'm confused with Node's to begin with, I'm completely lost.
    I suggest you start over with the code that you started with (assuming it works) and figure it out. Then once you know what is going on you can modify it to your needs. That would probably be more productive than trying to patch together what you have now.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 04:35 PM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 09:39 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM

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