overloaded iosteram operators

Nov 2, 2018 at 1:45pm
Hi guys,

I am trying to make a basic program to help me understand overloaded operators a bit better. I want to be able to use the input and output stream operators on objects, for example

Percent p1;
cin >> p1; // this would put the int value got from input stream into a member variable in the p1 object

cout << p1: // this would print out the int member variable of p1 to output stream

When I run the code below, I get 2 errors:
C2059 systax error: 'using namespace' lab6Main.cpp line 4
C2059 systax error: 'using namespace' percent.cpp line 4

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
  // main:
 #include "pch.h"
#include <iostream>
#include "Percent.h"
using namespace std;

int main()
{
	Percent p1;
	cin >> p1;
	cout << p1;
	return 0;
}

// Percent.h

 #ifndef PERCENT_H
#define PERCENT_H
#pragma once
#include <iostream>
using namespace std;


class Percent
{
public:
	
	Percent();

	friend istream& operator >>(istream& inputStream,
		Percent& aPercent);
	friend ostream& operator <<(ostream& outputStream,
		const Percent& aPercent);
	//There will be other members and friends.
private:
	int value;
}

#endif

 //Percent.cpp
 #include "pch.h"
#include <iostream>
#include "Percent.h"
using namespace std;

Percent::Percent()
{

}

istream& operator >>(istream& inputStream,
	Percent& aPercent)
{
	inputStream >> aPercent.value;
	return inputStream;
}

ostream& operator <<(ostream& outputStream,
	const Percent& aPercent)
{
	outputStream << aPercent.value;
	return outputStream;
}
Last edited on Nov 2, 2018 at 1:47pm
Nov 2, 2018 at 1:49pm
There's a faint possibility that the error is actually in Percent.h (which gets included in the other two files).

You need a semi-colon at the end of your class definition for class Percent.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Percent
{
public:
	
	Percent();

	friend istream& operator >>(istream& inputStream,
		Percent& aPercent);
	friend ostream& operator <<(ostream& outputStream,
		const Percent& aPercent);
	//There will be other members and friends.
private:
	int value;
}  ;    // <===== HERE *** 
Last edited on Nov 2, 2018 at 1:52pm
Nov 2, 2018 at 1:52pm
Thanks mate! Small syntax problems are so annoying

I appreciate the quick reply!
Nov 2, 2018 at 1:55pm
One more quick question, why is this semicolon needed at the end of .h files and not other files like .cpp files?
Last edited on Nov 2, 2018 at 1:56pm
Nov 2, 2018 at 1:58pm
It is at the end of the class definition. It is irrelevant whether it is in a header file (except that the error will propagate into other files when the header is include'd).

(You edited your earlier post, so that when I referred to a line number, that changed with your edit. So I had to include the code I was referring to instead.)
Last edited on Nov 2, 2018 at 1:59pm
Nov 2, 2018 at 1:59pm
Understood! Thanks for the help
Nov 2, 2018 at 2:29pm
Some notes:
1
2
3
4
5
6
7
8
9
10
11
// main:
#include <iostream>
#include "Percent.h"
// using namespace std;

int main()
{
	Percent p1;
	std::cin >> p1;
	std::cout << p1;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// Percent.h
#ifndef PERCENT_H
#define PERCENT_H
// #pragma once // can you trust a man, who has belt and suspenders?
// #include <iostream> // not needed
#include <iosfwd> // sufficient
// using namespace std; // dangerous in header

class Percent
{
public:
	Percent() = default; // compiler version is ok

	friend std::istream& operator>> (std::istream& inputStream,
		Percent& aPercent);
	friend std::ostream& operator<< (std::ostream& outputStream,
		const Percent& aPercent);
private:
	int value;
};
#endif 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//Percent.cpp
#include <iostream>
#include "Percent.h"
// we don't need entire namespace
using std::istream;
using std::ostream;

istream& operator >>(istream& inputStream,
	Percent& aPercent)
{
	inputStream >> aPercent.value;
	return inputStream;
}

ostream& operator <<(ostream& outputStream,
	const Percent& aPercent)
{
	outputStream << aPercent.value;
	return outputStream;
}
Nov 2, 2018 at 2:44pm
@keskiverto Thanks buddy!
Nov 2, 2018 at 2:55pm
Sidenote:
Foo, Bar, and Gaz are effectively identical.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Foo
{
private:
  int value;
};

class Bar
{
public:
  Bar() = default;
private:
  int value;
};

class Gaz
{
public:
  Gaz();
private:
  int value;
};

Gaz::Gaz()
{}


These Foo and Bar are not identical:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Foo
{
public:
  Foo(int);
private:
  int value;
};

class Bar
{
public:
  Bar() = default;
  Bar(int);
private:
  int value;
};

Foo::Foo(int x)
: value(x)
{}

Bar::Bar(int x)
: value(x)
{}
Nov 2, 2018 at 9:21pm
Could you explain your post a bit more please? I'm not quite sure how this relates
Last edited on Nov 2, 2018 at 9:40pm
Topic archived. No new replies allowed.