Thread: for/while Warnings

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    8

    for/while Warnings

    I found a data structures tutorial online that I followed through a little bit, and I wrote my own version of the linked list class it talks about. It works fine, but every time I compile it gives me a warning for each for/while loop I have: "Functions containing while are not expanded inline." It doesn't affect how the program actually works, but I'm just wondering what these warnings are about.

    Here's my linked list class along with a basic main function to test it, just in case it might help.

    Code:
    #include <iostream>
    
    using namespace std;
    
    class linkedlist {
    	
    private:
    	// A node structure; each data value is a node
    	struct node {
    		int data;
    		node *link;
    	} *p;
    	
    public:
    	// Constructor
    	linkedlist () {
    		p = NULL; // Primary node
    	}
    	// Desctructor
    	~linkedlist () {
    		// Destroy the list
    		node *q;
    		while (p != NULL) {
    			q = p;
    			p = q->link;
    			delete q;
    		}
    	}
    	// Append a new data value
    	// @param val the data value to append
    	void append (int val) {
    		// If the list is empty
    		if (p == NULL) {
    			p = new node;
    			p->data = val;
    			p->link = NULL;
    		}
    		// If the list is not empty
    		else {
    			node *q, *t;
    			q = p;
    			while (q->link != NULL)
    				q = q->link;
    			t = new node;
    			t->data = val;
    			t->link = NULL;
    			q->link = t;
    		}
    	}
    	// Adds an item to the front of the list
    	// @param val the item to add
    	// @return true if it was added successfully
    	bool add_as_first (int val) {
    		node *q;
    		q = new node;
    		q->data = val;
    		q->link = p;
    		p = q;
    		return true;
    	}
    	// Adds an item after a certain position
    	// @param j the position to add
    	// @param val the item to add
    	// @return true if it was added successfully
    	bool add_after (int j, int val) {
    		node *q, *t;
    		q = p;
    		for (int i = 0; i<j; i++) {
    			q = q->link;
    			if (q == NULL)
    				return false;
    		}
    		t = new node;
    		t->data = val;
    		t->link = q->link;
    		q->link = t;
    		return true;
    	}
    	// Deletes the first instance of val
    	// @param val the value to find and delete
    	// @return true if the value was found and deleted
    	bool del (int val) {
    		// t is the node pior, q is the current node
    		node *q, *t;
    		q = p;
    		t = NULL;
    		while (q->data != val) {
    			if (q->link == NULL)
    				return false;
    			t = q;
    			q = q->link;
    		}
    		// If the data was p, the first value in the list
    		if (t == NULL) {
    			p = q->link;
    			delete q;
    		}
    		// If the data found was not the first in the list
    		else {
    			node *r;
    			r = q->link;
    			t->link = r;
    			delete q;
    		}
    		return true;
    	}
    	// Display the list
    	void display () {
    		node *q;
    		cout << "[";
    		q = p;
    		while (q->link != NULL) {
    			cout << q->data << ", ";
    			q = q->link;
    		}
    		cout << q->data;
    		cout << "]" << endl;
    	}
    	// Get the size of the list
    	// @return the number of nodes in the list
    	int size () {
    		int c = 0;
    		node *q;
    		for (q = p; q != NULL; q = q->link)
    			c++;
    		return c;
    	}
    };
    
    int main () {
    	linkedlist list;
    	list.append(1);
    	list.append(3);
    	list.append(4);
    	list.add_after(0, 2);
    	list.display();
    	cout << "Size: " << list.size() << endl;
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    4
    You should declare and define your methods which contain more than one line of code at different places, which should solve the compiler warning.

    meaning:

    Code:
    class MyClass
    {
       private:
       ...
       public:
       void MyMethod():
    };
    
    void MyClass::MyMethod()
    {
        ...   // Definition
    }

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    All functions actually defined in the class are presumed to have "inline" in front of them. The compiler is telling you that, even though you want these functions to be inline, they won't be.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    8
    I knew you could define those functions outside of the class definition, I just didn't realize that that was the problem.

    Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Advice on removing valid (but minor) compiler warnings
    By PaulBlay in forum C Programming
    Replies: 12
    Last Post: 04-20-2009, 12:16 PM
  2. Warnings when using vector of vector
    By Boksha in forum C++ Programming
    Replies: 5
    Last Post: 03-29-2008, 01:54 PM
  3. Compilers and warnings
    By rogster001 in forum C Programming
    Replies: 6
    Last Post: 03-26-2008, 05:16 AM
  4. Replies: 9
    Last Post: 03-14-2008, 09:55 AM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM