Dec 14, 2016 at 11:08pm UTC
Hello,
I am getting error:'std' in 'class Distance' does not name a type... and if I use "using namespace std;" in both header and cpp file it will put out error:ostream in class Distance does not name a type.
In .cpp line 87 there error is happening
Is there any fix for this?
Header file:
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
#include <iostream>
#include <iomanip>
#include <sstream>
#include <cstdlib>
#include <ostream>
#ifndef DISTANCE_H_INCLUDED
#define DISTANCE_H_INCLUDED
class Distance{
private :
int judze;
int jardi;
int peda;
int colla;
public :
Distance();
Distance(int , int , int , int );
Distance(int );
int get_judze() const ;
int get_jardi() const ;
int get_peda() const ;
int get_colla() const ;
void set_judze(int );
void set_jardi(int );
void set_peda(int );
void set_colla(int );
void izvadit() const ;
friend std::ostream& operator <<( std::ostream &os, const Distance& d);
};
#endif // DISTANCE_H_INCLUDED
And .cpp code:
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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
#include <iostream>
#include <iomanip>
#include <sstream>
#include <cstdlib>
#include <ostream>
#include "distance.h"
Distance::Distance() : judze(0), jardi(0), peda(0), colla(0){}
Distance::Distance(int c){
if (c<0){
peda=0;
jardi=0;
judze=0;
colla=0;
}
else {
colla=0;
peda=0;
jardi=0;
judze=0;
while (colla>12){
peda++;
colla-=12;
}
while (peda>3){
jardi++;
peda-=3;
}
while (jardi>1760)
judze++;
jardi-=1760;
}
}
Distance::Distance(int ju, int ja, int p, int c){
judze = ju;
jardi = ja;
peda = p;
colla = c;
}
int Distance::get_colla() const {
return colla ;
}
int Distance::get_jardi()const {
return jardi;
}
int Distance::get_judze()const {
return judze;
}
int Distance::get_peda()const {
return peda;
}
void Distance::set_judze(int a){
judze = a;
}
void Distance::set_jardi(int b){
judze = b;
}
void Distance::set_peda(int c){
peda = c;
}
void Distance::set_colla(int d){
colla = d;
}
void Distance::izvadit() const {
std::cout << std::setw(2) << colla << std::endl;
std::cout << std::setw(2) << jardi << std::endl;
std::cout << std::setw(2) << judze << std::endl;
std::cout << std::setw(2) << peda << std::endl;
}
Distance::std::ostream& operator <<( std::ostream &os, const Distance& d){
if (get_judze == 0){
if (get_jardi == 0){
if (get_peda == 0){
os << d.get_colla;
}
else {
os << d.get_peda << "’" << d.get_colla << "”" ;
}
}
else {
os << d.get_jardi << "y" << d.get_peda << "’" << d.get_colla << "”" ;
}
}
else {
os << d.get_judze << "m" << d.get_jardi << "y" << d.get_peda << "’" << d.get_colla << "”" ;
}
return os;
}
Last edited on Dec 14, 2016 at 11:09pm UTC
Dec 14, 2016 at 11:19pm UTC
Remove Distance:: from line 87.
Distance::std::ostream& operator <<( std::ostream &os, const Distance& d){
Last edited on Dec 14, 2016 at 11:20pm UTC
Dec 14, 2016 at 11:27pm UTC
If I do so, I get all these error's:
86 error: 'get_judze' was not declared in this scope|
87 error: 'get_jardi' was not declared in this scope|
98 error: 'get_peda' was not declared in this scope|
|89| error: cannot bind 'std::ostream {aka std::basic_ostream<char>}' lvalue to 'std::basic_ostream<char>&&'|
|96| error: cannot bind 'std::ostream {aka std::basic_ostream<char>}' lvalue to 'std::basic_ostream<char>&&'|
|101| error: cannot bind 'std::ostream {aka std::basic_ostream<char>}' lvalue to 'std::basic_ostream<char>&&'|
Dec 14, 2016 at 11:36pm UTC
It's because you forgot to use the Distance object d when calling the functions. You also need to put parentheses after the function names.
if (d. get_judze() == 0){
Dec 15, 2016 at 12:55am UTC
That's right. Function in total should be:
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
std::ostream& operator <<( std::ostream &os, const Distance& d)
{
if (d.get_judze() == 0)
{
if (d.get_jardi() == 0)
{
if (d.get_peda() == 0)
{
os << d.get_colla();
}
else
{
os << d.get_peda() << "’" << d.get_colla() << "”" ;
}
}
else
{
os << d.get_jardi() << "y" << d.get_peda() << "’" << d.get_colla() << "”" ;
}
}
else
{
os << d.get_judze() << "m" << d.get_jardi() << "y" << d.get_peda() << "’" << d.get_colla() << "”" ;
}
return os;
}
Last edited on Dec 15, 2016 at 12:58am UTC