Class Variables question about operand errors

Hello all,
I am in need of some assistance for this assignment
I am having a problem fixing this to compile correctly. I am getting errors with line 30,31,32 of main involving the operands (They are underlined signaling errors). I am not sure why I am getting these errors? What do I need to do to correct the errors. Thanks for your time in advance!

Here are the errors:
Error 1 error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'void' (or there is no acceptable conversion)

Error 2 error C3867: 'Serial::getSerialNumber': function call missing argument list; use '&Serial::getSerialNumber' to create a pointer to member

Error 3 error C3867: 'Serial::getSerialNumber': function call missing argument list; use '&Serial::getSerialNumber' to create a pointer to member

4 IntelliSense: no operator "<<" matches these operands
operand types are: std::basic_ostream<char, std::char_traits<char>> << void

5 IntelliSense: no operator "<<" matches these operands
operand types are: std::basic_ostream<char, std::char_traits<char>> << void

6 IntelliSense: no operator "<<" matches these operands
operand types are: std::basic_ostream<char, std::char_traits<char>> << void
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
  //Programming Assignment 3:  Class Variable
//1. Create a class named Serial that includes a data member that holds a "serial number" for each object created from the class. That is, the first object created will be numbered 1, the second 2, and so on.
//2. A class variable should be used to contain the current serial number that is to be applied to the next created object.
//3. Add a member function that permits an object to report its own serial number. 
//4. Write your class in a file named Serial.h.
// ***Output***: Write a main() program that creates three objects and displays the serial numbers assigned to each object using the following code :
//cout << "object 1 serial number is " << object1.getSerialNumber() << endl;
//cout << "object 2 serial number is " << object2.getSerialNumber() << endl; 
//cout << "object 3 serial number is " << object3.getSerialNumber() << endl;

#include "Serial.h"
#include <iostream>
using namespace std;
int Serial::counter = 0;

int main() 
{
	Serial object1; //creating first object for class Serial
	Serial object2; // creating another object for class Serial
	Serial object3; // creating third object for class Serial

	cout << "object 1 serial number is " << object1.getSerialNumber() << endl; //calling them from method getSerialNumber
	cout << "object 2 serial number is " << object2.getSerialNumber() << endl; 
	cout << "object 3 serial number is " << object3.getSerialNumber() << endl;
	system("pause");
	return 0;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;

class Serial 
{
public:
	static int counter;
	int number;

//constructor
public:Serial() 
{
	number = ++counter;
}

	   void getSerialNumber()
	   {
		   cout << "I am object number " << number << endl;
		}
};
Well, there is no line 30, 31 and 32. But I assume you mean the cout statements in main.

cout << "object 1 serial number is " << object1.getSerialNumber() << endl;

your function getSerialNumber doesn't return a number. It only prints out something. Which means you can't use it like this. Not sure what you're trying to do.

You should rename the function unless you're going to be returning the actual number, it's confusing otherwise.

I would suggest changing the function to this -

1
2
3
4
 int getSerialNumber()
    {
        return number;
    }


Edit: If you dont want to change the function, do what @Thomas1965 suggests.

Edit 2: Fixed the function to be integer from void.
Last edited on
The operator << is to output sth. Since getSerialNumber() does not return a value you can't call it like this.

This will work:
1
2
3
cout << "\nobject 1 serial number is "; object1.getSerialNumber(); 
cout << "\nobject 2 serial number is "; object2.getSerialNumber(); 
cout << "\nobject 3 serial number is "; object3.getSerialNumber();

You should rename the function unless you're going to be returning the actual number, it's confusing otherwise.
I would suggest changing the function to this -
1
2
3
4
5
 
void getSerialNumber()
    {
        return number;
    }




Thanks for your reply! I get an error when doing this. I can understand how this would be a much better option though. The error is involving number. As far as I understand it does match the function despite what it says?

This is the error:

1 IntelliSense: return value type does not match the function type

Thanks for your reply Thomas1965! I appreciate the reasoning why I got the error with the operands. I can't change the code to your suggestion though. I have to keep it the same format so I am more than willing to change other things to keep the output code the same!
Oh sorry about that! The function should be changed to integer not void :p my bad! I'll edit my older post and fix it there too.
1
2
3
4
int getSerialNumber() // notice int and not void
    {
        return number;
    }
Last edited on
Now that works like a charm! I had it so close. I thought the error was referring to "number" not being the correct "type" as it was an int type so I didn't know it was referring to the function type. I will have to remember this. I get it now though. Thanks very much for all your help! Marking this as solved!
Topic archived. No new replies allowed.