CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 26
  1. #1
    Join Date
    Nov 2014
    Posts
    13

    C++ circular buffer

    I'm having some issues with my code. For the produce function i am getting an error saying 'no instance of overload function produce() matches the argument list' and also for the lines buffer[head].data = message; buffer[head].time = current_time i get an error saying 'expression must have pointer to object type.

    In the code i'm not sure if i passed the variabes to the function correctly. I'm new to C++ and trying to learn as much as i can. I have attached the code

    Any help would be appreciated.

    Kind Regards

    code produce.txt

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: C++ circular buffer

    Please post your code rather than providing a link. Many members don't open unknown links. When posting code, please use code tags. Go Advanced, select the formatted code and click '#'.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: C++ circular buffer

    Produce is defined as accepting three ints as arguments. You're passing a struct as the first.

    Consume and Show are defined with no arguments. You're passing three.

    buffer_head is undefined.

    In Produce, buffer is defined as an int&. You're trying to access it as if it were an array.

  4. #4
    Join Date
    Nov 2014
    Posts
    13

    Re: C++ circular buffer

    Quote Originally Posted by GCDEF View Post
    Produce is defined as accepting three ints as arguments. You're passing a struct as the first.

    Consume and Show are defined with no arguments. You're passing three.

    buffer_head is undefined.

    In Produce, buffer is defined as an int&. You're trying to access it as if it were an array.
    Thanks for the reply

    I have changed the first argument to struct, thanks for pointing that out i overlooked it. The reason i left Consume and Show blank was because i was going to do it once i got produce to work. Sorry buffer head should have been head, as defined by unsigned long. After the changes i still get an error for the final 2 lines, it says 'no operator [] matches these operands'. And the error for the produce in the main still exists.



    Code:
    #include <iostream>
    #include <time.h>
    using namespace std;
    
    struct Message {
    	unsigned char data; // single byte of information to be sent
    	time_t time; 
    };
    
    #define B_L 10
    
    //function prototype
    void Produce(Message, int, int);
    void Consume(Message, int, int);
    void Show(Message, int, int);
    
    int main()
    {
    	Message buffer[B_L]; // the message buffer
    	unsigned long tail = 0; // position of the tail in the message buffer –
    	// the next message will be consumed from here
    	unsigned long length = 0; // number of messages in the buffer
    	char UserInput;
    
    	while (1) {
    
    		// show the menu of options
    		cout << endl;
    		cout << "Buffer Management Menu" << endl;
    		cout << "----------------------" << endl;
    		cout << "1. Produce a new message for the buffer" << endl;
    		cout << "2. Consume a message from the buffer" << endl;
    		cout << "3. View the contents of the buffer" << endl;
    		cout << "4. Exit from the program" << endl << endl;
    
    		// get the user's choice
    		cout << "Enter your option: ";
    		cin >> UserInput;
    		cout << endl;
    
    		// act on the user's input
    		switch (UserInput) {
    		case '1':
    			Produce(buffer, tail, length);
    			break;
    
    		case '2':
    			Consume(buffer, tail, length);
    			break;
    
    		case '3':
    			Show(buffer, tail, length);
    			break;
    			
    		case '4':
    			return 0;
    		default:
    			cout << "Invalid entry" << endl << endl;
    			break;
    		}
    	}
    }
    
    
    
    	void Produce(Message buffer, int &tail, int &length) //int will be stored into circular queue
    	{
    		time_t current_time;
    		unsigned long head;
    		char message;
    		
    		head = (tail + length) % 10; //Find the element of the buffer in which to store the message	
    			
    		cout << "Enter your message: "; // get the value of the data for the message from the user
    		cin >> message;
    		cout << endl;
    		current_time = time(NULL);
    
    		if ((head == 0 && tail == length - 1) || (head == tail + 1))// Check for buffer overflow 
    		{
    			printf("Error! Queue is full\n");
    			return;
    		}
    		else {
    			length = length + 1; // if no buffer overflow has occurred, adjust the buffer length
    			buffer[head].data = message;
    			buffer[head].time = current_time;// get the value of the time for the message
    		}
    	}

  5. #5
    Join Date
    Nov 2014
    Posts
    13

    Re: C++ circular buffer

    sorry it should be

    void Produce(Message[B_L], unsigned long, unsigned long);

    and the error in the main is gone.

  6. #6
    Join Date
    Nov 2014
    Posts
    13

    Re: C++ circular buffer

    and i also changed theones in the produce function and the errors are gone. I just need to run it now and hope that it works.

  7. #7
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: C++ circular buffer

    Code:
    Message buffer[B_L]; // the message buffer
    ...
    void Produce(Message buffer, int &tail, int &length)
    ...
    Produce(buffer, tail, length);
    buffer is an array of type Message. However the first argument of Produce is just a variables of type Message but you are trying to pass buffer which is an array of Message.

    if you want to pass buffer as an array then the function definition should be
    Code:
    void Produce(Message buffer[B_L], int &tail, int &length)
    You also need to change the function declarations to match your function definitions.

    Also tail and length are defined as unsigned long, but they are being passed to the function as reference int - so the function definitions need changing to use unsigned long& rather than int&.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  8. #8
    Join Date
    Nov 2014
    Posts
    13

    Re: C++ circular buffer

    Quote Originally Posted by 2kaud View Post
    Code:
    Message buffer[B_L]; // the message buffer
    ...
    void Produce(Message buffer, int &tail, int &length)
    ...
    Produce(buffer, tail, length);
    buffer is an array of type Message. However the first argument of Produce is just a variables of type Message but you are trying to pass buffer which is an array of Message.

    if you want to pass buffer as an array then the function definition should be
    Code:
    void Produce(Message buffer[B_L], int &tail, int &length)
    You also need to change the function declarations to match your function definitions.

    Also tail and length are defined as unsigned long, but they are being passed to the function as reference int - so the function definitions need changing to use unsigned long& rather than int&.
    Okay so i put in a cout to see what the array ooks like and ran it. When i input a message i.e 2 i get an address 082241 then when i input another it says queue is full. I'm not sure where I'm going wrong.

    Code:
    #include <iostream>
    #include <time.h>
    using namespace std;
    
    struct Message {
    	unsigned char data; // single byte of information to be sent
    	time_t time; 
    };
    
    #define B_L 10
    
    //function prototype
    void Produce(Message[10], unsigned long, unsigned long);
    void Consume(Message[10], unsigned long, unsigned long);
    void Show(Message[10], unsigned long, unsigned long);
    
    int main()
    {
    	Message buffer[B_L]; // the message buffer
    	unsigned long tail = 0; // position of the tail in the message buffer –
    	// the next message will be consumed from here
    	unsigned long length = 0; // number of messages in the buffer
    	char UserInput;
    
    	while (1) {
    
    		// show the menu of options
    		cout << endl;
    		cout << "Buffer Management Menu" << endl;
    		cout << "----------------------" << endl;
    		cout << "1. Produce a new message for the buffer" << endl;
    		cout << "2. Consume a message from the buffer" << endl;
    		cout << "3. View the contents of the buffer" << endl;
    		cout << "4. Exit from the program" << endl << endl;
    
    		// get the user's choice
    		cout << "Enter your option: ";
    		cin >> UserInput;
    		cout << endl;
    
    		// act on the user's input
    		switch (UserInput) {
    		case '1':
    			Produce(buffer, tail, length);
    			break;
    
    		case '2':
    			Consume(buffer, tail, length);
    			break;
    
    		case '3':
    			Show(buffer, tail, length);
    			break;
    			
    		case '4':
    			return 0;
    		default:
    			cout << "Invalid entry" << endl << endl;
    			break;
    		}
    	}
    }
    
    
    
    	void Produce(Message[10] buffer, unsigned long &tail, unsigned long &length) //int will be stored into circular queue
    	{
    		time_t current_time;
    		unsigned long head;
    		char message;
    		
    		head = (tail + length) % 10; //Find the element of the buffer in which to store the message	
    			
    		cout << "Enter your message: "; // get the value of the data for the message from the user
    		cin >> message;
    		cout << endl;
    		current_time = time(NULL);
    
    		if ((head == 0 && tail == length - 1) || (head == tail + 1))// Check for buffer overflow 
    		{
    			printf("Error! Queue is full\n");
    			return;
    		}
    		else {
    			length = length + 1; // if no buffer overflow has occurred, adjust the buffer length
    			buffer[head].data = message;
    			buffer[head].time = current_time;// get the value of the time for the message
    
    			for (int i = 0; i < B_L; i++)
    		{
    			cout << i << '  ' << buffer[i].data ;
    		}
    		}
    	}

  9. #9
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: C++ circular buffer

    That code doesn't compile, but it sounds like it's time to learn to use the debugger.

  10. #10
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: C++ circular buffer

    Code:
    void Produce(Message[10], unsigned long, unsigned long);
    This declaration doesn't match the definition

    Code:
    void Produce(Message[10], unsigned long&, unsigned long&);
    Also instead of 10 use B_L which has already been defined. Also note that in c++ it is more usual to use a const int rather than a #define
    Code:
    const int B_L = 10;
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  11. #11
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: C++ circular buffer

    I think your problem is here
    if ((head == 0 && tail == length - 1) || (head == tail + 1))// Check for buffer overflow

    Shouldn't it be

    if ((head == 0 && tail == B_L- 1) || (head == tail + 1))// Check for buffer overflow

  12. #12
    Join Date
    Nov 2014
    Posts
    13

    Re: C++ circular buffer

    Quote Originally Posted by 2kaud View Post
    Code:
    void Produce(Message[10], unsigned long, unsigned long);
    This declaration doesn't match the definition

    Code:
    void Produce(Message[10], unsigned long&, unsigned long&);
    Also instead of 10 use B_L which has already been defined. Also note that in c++ it is more usual to use a const int rather than a #define
    Code:
    const int B_L = 10;
    Right i did the declaration and definition as:

    void Produce(Message[10], unsigned long, unsigned long);

    my program works but i'm still quite confused because i thought doing so might cause each function to create each of their own variable, all unrelated to another.

    Can someone explain this part to me?

    Thanks

  13. #13
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: C++ circular buffer

    Passing parameters by value means that changes to these variables within the function are local to the function and are not reflected in the arguments used when the function was called. Passing parameters by ref (ie using &) means that changes to these variables within the function are reflected in the arguments used when the function was called.
    Code:
    void Produce(Message[10], unsigned long, unsigned long);
    The unsigned long parameters (tail and length) are passed by value so changes to these in Produce() are not reflected back in the tail and length variables defined in main(). As such I won't expect the program to work as expected because for every time round the loop in main() you are using the same value of tail and length. As noted previously, I would expect the declaration and definition of Produce to be
    Code:
    void Produce(Message[10], unsigned long&, unsigned long&);
    Code:
    void Produce(Message buffer[10], unsigned long &tail, unsigned long &length)
    so that changes to tail and length in Produce() are reflected back to the variables in main() so that the updated variables are used for the next call to Produce().
    Last edited by 2kaud; November 27th, 2014 at 04:10 PM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  14. #14
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: C++ circular buffer

    When i input a message i.e 2 i get an address 082241 then when i input another it says queue is full. I'm not sure where I'm going wrong.
    Code:
    cout << i << '  ' << buffer[i].data ;
    This should be
    Code:
    cout << i << "  " << buffer[i].data ;
    to display a string. Use ' only when using a single char.

    then when i input another it says queue is full.
    Your test for queue full is not correct. The second time tail is still 0 as nothing has been consumed, length is 1 as 1 message has been produced, head is 1 as the next position to insert the message so head does equal tail plus 1!
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  15. #15
    Join Date
    Nov 2014
    Posts
    13

    Re: C++ circular buffer

    Quote Originally Posted by 2kaud View Post
    Passing parameters by value means that changes to these variables within the function are local to the function and are not reflected in the arguments used when the function was called. Passing parameters by ref (ie using &) means that changes to these variables within the function are reflected in the arguments used when the function was called.
    Code:
    void Produce(Message[10], unsigned long, unsigned long);
    The unsigned long parameters (tail and length) are passed by value so changes to these in Produce() are not reflected back in the tail and length variables defined in main(). As such I won't expect the program to work as expected because for every time round the loop in main() you are using the same value of tail and length. As noted previously, I would expect the declaration and definition of Produce to be
    Code:
    void Produce(Message[10], unsigned long&, unsigned long&);
    Code:
    void Produce(Message buffer[10], unsigned long &tail, unsigned long &length)
    so that changes to tail and length in Produce() are reflected back to the variables in main() so that the updated variables are used for the next call to Produce().

    That makes sense thanks. I think my array isn't being referenced properly. How do I go about making the function able to refer to the array?

    i tried message&[B_L] and Message &buffer[10] but it says array of reference is not allowed.

Page 1 of 2 12 LastLast

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