Two compile errors I can't figure out

When attempting to compile my code, I get a total of 6 errors. I have been working on 2 of them, and cannot figure them out.

error: id.cpp:26: error: âage_â was not declared in this scope
function:
void SetAge (int a)
{
age_ = a;
}

The prototype for this function is in file id.h, under class ID and age_ is a private variable in class ID. The class also has a private variable name_, but I'm not receiving any errors in functions which use it.

error: id.cpp:73: error: expected primary-expression before â.â token
function:
std::ostream& operator << (std::ostream & os, ID)
{
os << ID.Cstr ();
return os;
}

The prototype for this one is also in id.h, but outside the scope of the class ID.

I appreciate any help you guys can give me.

Thanks,
Kash
You need to explicitly tell the compiler that SetAge is a member method of class ID like that:

1
2
3
4
void ID::SetAge (int a)
{
age_ = a;
}


The second problem is that you want to pass a parameter of type ID to the function operator<<, so just as you do with any other parameter, you need to provide a name for it and use this name in the function body:

1
2
3
4
5
std::ostream& operator << (std::ostream & os, ID param)
{
os << param.Cstr ();
return os;
}
Thanks for the quick response. I really appreciate it.

One question that arises from the correction of the opertor << function. Here's the new error message I get:

id.cpp:73: error: âclass IDâ has no member named âCstrâ

The basics of the function was provided by the instructor, is Cstr not a standard part of the function?

Thanks again,
Kash
Last edited on
To be able to call Cstr() on an ID object, ID must have a member function with that name. If it doesn't have it, you can create it.
Thanks for the help. After a few hours on it, since I knew I wasn't supposed to be adding any more functions to the program, I realized I didn't even need the Cstr(). Here's what I've got so far:

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
#include <id.h>                                                                            
#include <cstring>                                                                         
                                                                                           
void ID::SetName (const char * c)                                                          
{                                                                                          
  size_t length  = strlen (c);                                                             
                                                                                           
  name_          = new char [1 + length]; // we need an extra space to store the           
                                          // null charachter                               
                                                                                           
  name_ [length] = '\0';                  // adding the null character                     
                                                                                           
  strcpy (name_, c);                      // copies from c to name_ till null is           
                                          // reached                                       
}                                                                                          
                                                                                           
void SetAge (int a)                                                                        
{                                                                                          
  age_ = a;                                                                                
}                                                                                          
                                                                                           
const char * ID::GetName () const                                                          
{                                                                                          
  return name_;                                                                            
}                                                                                          

int ID::GetAge () const                                                                    
{                                                                                          
  return age_;                                                                             
}                                                                                          
                                                                                           
ID::ID () : name_ (0), age_ (-1)                                                           
{                                                                                          
  name_     = new char [2]; // we need a two character array, one for sharp, one for       
                        // null                                                            
                                                                                           
  name_ [0] = '#';                                                                         
                                                                                           
  name_ [1] = '\0';                                                                        
}                                                                                          
                                       
ID::ID (const char * n, int a) : name_ (0), age_ (a)                                       
{                                                                                          
  size_t length strlen (a);                                                                
                                                                                           
  name_          = new char [1 + length];                                                  
                                                                                           
  name_ [0]      = '#', strcpy (name_, n);                                                 
                                                                                           
  name_ [length] = '\0';                                                                   
}                                                                                          
                                                                                           
ID::ID (const ID & id) : name_ (0), age_ (id.age_)                                         
{                                                                                          
  size_t length strlen (id.name_);                                                         
                                                                                           
  name_          = new char [1+ length];                                                   
                                                                                           
  name_ [0]      = '#', strcpy (name_, id.name_);                                          
                                                                                           
  name_ [length] = '\0';                                                                   
}                                                                                          
                                                                                           
std::ostream& operator << (std::ostream & os, ID)  
{                                                                                          
  os << ID.Cstr ();                                                                        
  return os;                                                                               
}


The error message I'm receiving now is:

 
id.cpp:78: error: expected initializer before âstdâ


The error is actually on line 64 of this post.

Could you guys point me in the right direction to fix this error, and also how to go about setting up my assignment operator= function?

Thanks again,

Kash
Last edited on
Topic archived. No new replies allowed.