errors with code

Im having problems with errors for my code for a class.Im supposed to test the functions defined in the header in the main program.When i compile i get these errors
1 E:\location.cpp In file included from location.cpp
12 E:\location.h `istream' was not declared in this scope
12 E:\location.h `istream' was not declared in this scope
12 E:\location.h expected primary-expression before ',' token
12 E:\location.h expected primary-expression before '&' token
12 E:\location.h expected primary-expression before ')' token
12 E:\location.h a function call cannot appear in a constant-expression
12 E:\location.h ISO C++ forbids declaration of `type name' with no type
12 E:\location.h confused by earlier errors, bailing out

heres my code:

header:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#ifndef location_H
#define location_H
class location
{
    friend:  
       istream &operator>>(istream& , location&);
       ostream &operator<<(ostream& , location&);
      
      private:
              int row;
              int colm;
              
      public:
             location (int=0,int=0);
             void assign(int, int);
             location operator =(location);
             bool Location operator ==(location);
             int inc(int, int);
             int Row();
             int Col();
}
#endif 


header implementation:
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
#include "location.h"

location::location(int r,int c);
{
           row=r;
           colm=c;
}
void location::assign(int r,int c)
{
           row=r;
           colm=c;
}
location location::operator = (location y)
{
         row = y.row;
         colm = y.colm;
         return y;
}
bool Location::operator (==,location z)
{
     return((row==z.row)&&(colm==z.colm))
}
int location::Row()
{
    return row;
}
int location::Colm()
{
    return colm;
}
int inc(int r, int c)
{
    row= r+1;
    colm= c+2;
}
istream &operator>>(istream &input , location &x)
{
         char a;
         input<<a
               <<x.row
               <<a
               <<x.colm
               <<a;
         return input;
} 
ostream &operator<<(ostream &output , location &x)
{
         output<<'('
               <<x.row
               <<','
               <<x.colm
               <<')';
         return output;
}


main program:
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
#include <iostream>

using namespace std;

int main()
{
    
    location x,y(1),z(1,2)
    
    cout<<"testing constructor"<<endl;
    cout<<"x="<<x<<endl;
    cout<<"y="<<y<<endl;
    cout<<"z="<<z<<endl;
    
    cout<<"testing assign function"<<endl
    x.assign(3.7);
    y.assign(9.3);
    cout<<"Enter a location for z (r,c)";
    cin>>z.assign;
    
    cout<<"x="<<x.assign<<endl;
    cout<<"y="<<y.assign<<endl;
    cout<<"z="<<z.assign<<endl;
    
    cout<<"testing == function"<<endl;
    if (x==y)
       cout<<"these points are equal";
    else
       cout<<"these points are not equal";
    
    cout<<"testing = function"<<endl;
    if (y=x)
       cout<<"these points are equal";
    else
       cout<<"these points are not equal";
    
    cout<<"testing row function"<<endl;   
    cout<<"x row="<<x.Row; 
    cout<<"y row="<<y.Row;
    cout<<"z row="<<z.Row;       
    
    cout<<"testing colm function"<<endl;   
    cout<<"x column="<<x.Colm; 
    cout<<"y column="<<y.Colm;
    cout<<"z column="<<z.Colm;  
    
    cout<<"testing inc function"<<endl;
    cout<<"x="<<x.inc;
    cout<<"y="<<y.inc;
    cout<<"z="<<z.inc;
    
    return 0;
}  


Most of the header and the functions in the implementation are accurate since they're given by my professor.If someone can help me out i would appreciate it.
You didn't #include<iostream> in your header, notice that you need to add std:: then
doesnt work,still get the same errors
more specifcally, this is what i have now header only

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#ifndef location_H
#define location_H
#include <iostream>
class location
{
    friend:  
       istream &operator>>(istream& , location&);
       ostream &operator<<(ostream& , location&);
      
      private:
              int row;
              int colm;
              
      public:
             location (int=0,int=0);
             void assign(int, int);
             location operator =(location);
             bool Location operator ==(location);
             int inc(int, int);
             int Row();
             int Col();
}
#endif 
std::istream
std::ostream
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#ifndef location_H
#define location_H
#include <iostream>
class location
{
    friend: 
     std::istream &operator>>(istream& , location&);
     std::ostream &operator<<(ostream& , location&);
      
      private:
              int row;
              int colm;
              
      public:
             location (int=0,int=0);
             void assign(int, int);
             location operator =(location);
             bool Location operator ==(location);
             int inc(int, int);
             int Row();
             int Col();
}
#endif 


still the same errors
Note:most of my other code is the same.
I still see two missing std::
putting the std:: did help with two of the errors.but im still getting some of the same errors

specifically:
1 F:\location.cpp In file included from location.cpp
13 F:\location.h expected primary-expression before '&' token
13 F:\location.h expected primary-expression before '&' token
13 F:\location.h expected primary-expression before ',' token
13 F:\location.h expected primary-expression before '&' token
13 F:\location.h expected primary-expression before ')' token
13 F:\location.h a function call cannot appear in a constant-expression
13 F:\location.h ISO C++ forbids declaration of `type name' with no type
13 F:\location.h confused by earlier errors, bailing out


my current code:

header
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#ifndef location_H
#define location_H
#include <iostream>
class location
{
    friend: 
     std::istream &operator>>(std::istream& , location&);
     std::ostream &operator<<(std::ostream& , location&);
      
      private:
              int row;
              int colm;
              
      public:
             location (int=0,int=0);
             void assign(int, int);
             location operator =(location);
             bool Location operator ==(location);
             int inc(int, int);
             int Row();
             int Col();
}
#endif 


implementation
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
#include "location.h"


location::location(int r,int c);
{
           row=r;
           colm=c;
}
void location::assign(int r,int c)
{
           row=r;
           colm=c;
}
location location::operator = (location y)
{
         row = y.row;
         colm = y.colm;
         return y;
}
bool Location::operator (==,location z)
{
     return((row==z.row)&&(colm==z.colm))
}
int location::Row()
{
    return row;
}
int location::Colm()
{
    return colm;
}
int inc(int r, int c)
{
    row= r+1;
    colm= c+2;
}
std::istream &operator>>(std::istream &input , location &x)
{
         char a;
         input<<a
               <<x.row
               <<a
               <<x.colm
               <<a;
         return input;
} 
std::ostream &operator<<(std::ostream &output ,  location &x)
{
         output<<'('
               <<x.row
               <<','
               <<x.colm
               <<')';
         return output;
}


note:the main program code is still the same.If anyone can help me with what im still doing wrong,id appreciate it.
There are some errors which I didn't noticed:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 #ifndef location_H
#define location_H
#include <iostream>
class location
{
    //friend:  <- Not like this
     friend /*but like this*/ std::istream &operator>>(std::istream& , location&);
     friend /*here the same*/ std::ostream &operator<<(std::ostream& , location&);
      
      private:
              int row;
              int colm;
              
      public:
             location (int=0,int=0);
             void assign(int, int);
             location operator =(location);
             bool Location operator ==(location); // bool Location?
             int inc(int, int);
             int Row();
             int Col();
}; // you forgot the semicolon
#endif  


In source file:
1
2
3
4
bool /*the class name is location -lowercase- not Location*/Location::operator == (location z)//Messed up parenthesis:  (==,location z)
{
     return((row==z.row)&&(colm==z.colm)); // missing semicolon
}


I hope I got everything...
Last edited on
closed account (z05DSL3A)
in the implementation location::location(int r,int c)//; should not be here

edit:
Looking at it there are lots of minor syntax errors.

header
1
2
location& operator =(const location&);
bool operator ==(const location&) const;


implementation
1
2
3
4
5
6
7
8
9
10
11
location& location::operator = (const location& y)
{
         row = y.row;
         colm = y.colm;
         return *this;
}

bool location::operator == (const location& z) const
{
     return((row==z.row)&&(colm==z.colm));
}
Last edited on
well, thanks to you guys im making progress.alot of errors are disappearing. Im getting these now:
34 F:\location.cpp `row' undeclared (first use this function)
35 F:\location.cpp `colm' undeclared (first use this function)
41 F:\location.cpp no match for 'operator<<' in 'input << a'

current code:

implementation:
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
#include "location.h"


location::location(int r,int c)
{
           row=r;
           colm=c;
}
void location::assign(int r,int c)
{
           row=r;
           colm=c;
}
location location::operator = (const location& y)
{
         row = y.row;
         colm = y.colm;
         return y;
}
bool  location::operator == (const location& z)const
{
     return((row==z.row)&&(colm==z.colm));
}
int location::Row()
{
    return row;
}
int location::Colm()
{
    return colm;
}
int inc(int a, int b)
{
    row=+a;
    colm=+b;
}
std::istream &operator>>(std::istream &input , location &x)
{
         char a;
         input<<a
               <<x.row
               <<a
               <<x.colm
               <<a;
         return input;
} 
std::ostream &operator<<(std::ostream &output ,  location &x)
{
         output<<'('
               <<x.row
               <<','
               <<x.colm
               <<')';
         return output;
}


header
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#ifndef location_H
#define location_H
#include <iostream>
class location
{
  
   friend std::istream &operator>>(std::istream& , location&);
   friend std::ostream &operator<<(std::ostream& , location&);
      
      private:
              int row;
              int colm;
              
      public:
             location (int=0,int=0);
             void assign(int, int);
             location operator =(const location&);
             bool operator ==(const location&)const;
             int inc(int, int);
             int Row();
             int Colm();
};
#endif 


note:main program is the same

I really appreciate the help so far,if you guys can help with these id appreciate it.
You forgot 'location::' at line 32 and have << instead of >> on lines 40-44
closed account (z05DSL3A)
1
2
3
4
5
6
int inc(int a, int b)
{
    row=+a;
    colm=+b;
//Missing a return value
}
Im having some error trouble now with my main program.
specifcally:
16 F:\testlocation.cpp no matching function for call to `location::assign(double)'
17 F:\testlocation.cpp no matching function for call to `location::assign(double)'
32 F:\testlocation.cpp could not convert `(&y)->location::operator=(((const location&)((const location*)(&x))))' to `bool'

note:in order to cut down in amount of lines the next errors cover the respective section

38 F:\testlocation.cpp no match for 'operator<<' in 'std::operator<< [with _Traits = std::char_traits<char>](((std::basic_ostream<char, std::char_traits<char> >&)(&std::cout)), ((const char*)"x row=")) << x.location::Row'

43 F:\testlocation.cpp no match for 'operator<<' in 'std::operator<< [with _Traits = std::char_traits<char>](((std::basic_ostream<char, std::char_traits<char> >&)(&std::cout)), ((const char*)"x column=")) << x.location::Colm'

48 F:\testlocation.cpp no match for 'operator<<' in 'std::operator<< [with _Traits = std::char_traits<char>](((std::basic_ostream<char, std::char_traits<char> >&)(&std::cout)), ((const char*)"x=")) << x.location::inc'


main 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
#include <iostream>
#include "location.h"


int main()
{
    
    location x,y(1),z(1,2);
    
    std::cout<<"testing constructor"<<std::endl;
    std::cout<<"x="<<x<<std::endl;
    std::cout<<"y="<<y<<std::endl;
    std::cout<<"z="<<z<<std::endl;
    
    std::cout<<"testing assign function"<<std::endl;
    x.assign(3.7);
    y.assign(9.3);
    std::cout<<"Enter a location for z (r,c)";
    std::cin>>z;
    
    std::cout<<"x="<<x<<std::endl;
    std::cout<<"y="<<y<<std::endl;
    std::cout<<"z="<<z<<std::endl;
    
    std::cout<<"testing == function"<<std::endl;
    if (x==y)
       std::cout<<"these points are equal";
    else
       std::cout<<"these points are not equal";
    
    std::cout<<"testing = function"<<std::endl;
    if (y=x)
       std::cout<<"these points are equal";
    else
       std::cout<<"these points are not equal";
    
    std::cout<<"testing row function"<<std::endl;   
    std::cout<<"x row="<<x.Row; 
    std::cout<<"y row="<<y.Row;
    std::cout<<"z row="<<z.Row;       
    
    std::cout<<"testing colm function"<<std::endl;   
    std::cout<<"x column="<<x.Colm; 
    std::cout<<"y column="<<y.Colm;
    std::cout<<"z column="<<z.Colm;  
    
    std::cout<<"testing inc function"<<std::endl;
    std::cout<<"x="<<x.inc;
    std::cout<<"y="<<y.inc;
    std::cout<<"z="<<z.inc;
    
    return 0;
}  


implementation:
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
[#include "location.h"


location::location(int r,int c)
{
           row=r;
           colm=c;
}
void location::assign(int r,int c)
{
           row=r;
           colm=c;
}
location location::operator = (const location& y)
{
         row = y.row;
         colm = y.colm;
         return y;
}
bool  location::operator == (const location& z)const
{
     return((row==z.row)&&(colm==z.colm));
}
int location::Row()
{
    return row;
}
int location::Colm()
{
    return colm;
}
int location::inc(int a, int b)
{
    a=1;
    b=2;
    row=+a;
    colm=+b;
    
}
std::istream &operator>>(std::istream &input , location &x)
{
         char a;
         input>>a
               >>x.row
               >>a
               >>x.colm
               >>a;
         return input;
} 
std::ostream &operator<<(std::ostream &output ,  location &x)
{
         output<<'('
               <<x.row
               <<','
               <<x.colm
               <<')';
         return output;
}


header
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class location
{
  
   friend std::istream &operator>>(std::istream& , location&);
   friend std::ostream &operator<<(std::ostream& , location&);
      
      private:
              int row;
              int colm;
              
      public:
             location (int=0,int=0);
             void assign(int, int);
             location operator =(const location&);
             bool operator ==(const location&)const;
             int inc(int, int);
             int Row();
             int Colm();
};
#endif 


if anyone could help, id appreciate it.
Last edited on
I believe that you can find what is wrong on your own, look the declaration of 'assign' and then check lines 16-17 of your main file.
Read line 26 and 32 of the main file and compare what you see to what the last error message says.
Topic archived. No new replies allowed.