Undeclared Identifiers?

Hello everyone!

I am making a read/write to file program that I must include the usage of classes. Unfortunately I cannot get it to work as my main function appears to not be seeing my class member functions. I'm not sure why this is, but I was wondering if anyone could help me.

Errors occur at lines 56, 62 and 82. The compiler states that they are undeclared identifiers, yet they are declared just right above?

The code is as follows:

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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

class ReadWrite
{
public:
    void Read(istream& infile);
    void Write(ostream& infile);
};

void ReadWrite::Read(istream& infile)
{
    string line;
    while(getline(infile,line))
    {
        cout << line << endl;
    }
}

void ReadWrite::Write(ostream& infile)
{
    string input;
    while(input != "save"){
        getline(cin,input);
        if(input == "save")
        {
            break;
        }
        infile << input << endl;
    }
}

int main()
{
    string userinput;
    
    cout << endl << "Hi, this program will search for your specified file that you may read or write to. You may also create a new file when asked. Also note you can exit the program at any time by typing 'quit'" << endl << endl;
    
    while(userinput != "quit")
    {
        cout << "What is the name of your file?" << endl;
        cin >> userinput;
        
        fstream file(userinput, ios::app);
        
        if(!file.is_open())
        {
            cout << "Would you like to read or write to this file?" << endl;
            cin >> userinput;
            
            if(userinput == "read")
            {
                Read(file);
            }
            if(userinput == "write")
            {
                cout << "You may now write, enter 'save' when you wish to exit the writing process." << endl;
                
                Write(file);
            }
        }
        else
        {
            cout << "File does not exist, would you like to write a new one? (Yes/No)" << endl;
            
            cin >> userinput;
            
            if(userinput == "Yes")
            {
                cout << "Please enter the name of your file, include .txt extension." << endl;
                cin >> userinput;
                
                fstream newFile(userinput);
                
                newFile.open(userinput, ios::out);
                
                cout << "You may now write to your new file, enter 'save' when you wish to exit the writing process." << endl;
                
                Write(newFile);
                
            }
            if(userinput == "No")
            {
                cout << "Would you like to search for a new file? If Yes, you can enter a new file name, if No the program will exit." << endl;
                if (userinput == "Yes")
                {
                    continue;
                }
                if(userinput == "No")
                {
                    userinput = "quit";
                }
                else{
                    cout << "You've entered an incorrect response. Please try again." << endl;
                }
            }
        }
    }
    return 0;
}
Last edited on
You are calling functions called Read and Write, but you have only defined functions called ReadWrite::Read and ReadWrite::Write.

Class member functions are different than normal functions. Go check the tutorial on classes here for more information.
Hi @Merriak

You have to create
an instance of your
class i.e.

1
2
3
4
5
6
7
8
//Class      //instance
ReadWrite InputOutput;

               //Instance    //method
               InputOutput.Write(parameter);

               //Instance    //method
               InputOutput.Read(parameter);


also you can use member
functions -methods- without
an instance but i do not know
if you need it, first try making
an instance.
Ah yes Eyenrique! That does indeed fix it! Thank you... I could of swore last time I programmed in C++ that I didn't need an instance. In fact I look at old programs I made and I didn't have to use an instance in some cases and the program worked. So why must I need an instance here?

The rust is piled up high now!
You are welcome!

To be honest i
do not know old
C++ standard i
learned that you
have to create an instance
when you want an user-defined type
but you can access to
member functions and
data members without
instances using a certain
syntax;


Eyenrique-MacBook-Pro:Desktop Eyenrique$ ./ClassSyntax 
NEXT SERVICE
30 MILES


NEXT SERVICE
30 MILES

NeXT SERVICE
30 MILES


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
//ClassSyntax.cpp
//##

#include <iostream>
#include <string>

using namespace std;


class Sign{
        public:
                    
                static  void showMessage();
                static int const MILES=30;
};//end class sign


void Sign::showMessage(){
        cout<<"NEXT SERVICE\n"<<MILES<<" MILES"<<endl;
}//end method showMessage

int main(){

//using instance
Sign Signal;

        Signal.showMessage();
            
        cout<<'\n'<<endl;

//without instance
        Sign::showMessage();
        cout<<"\nNeXT SERVICE\n"<<Sign::MILES<<" MILES"<<endl;
            
return 0; //indicates success
}//end of main 
Topic archived. No new replies allowed.