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
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 ***
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.)
// 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
class Foo
{
private:
int value;
};
class Bar
{
public:
Bar() = default;
private:
int value;
};
class Gaz
{
public:
Gaz();
private:
int value;
};
Gaz::Gaz()
{}