Keeps saying the function I'm trying to call is not declared in scope.

Hello. I'm trying to use some old code for a new project I'm working on, and I'm trying to test if it works first before tweaking it. For some reason, although this code worked when I submitted it (it was for class) it doesn't work now. It keeps saying that readData is not declared in this scope. I would really appreciate help in fixing this problem. Thank you very much!

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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
  #include <iostream>
#include <stdlib.h>
#include <fstream>
#include <bits/stdc++.h>
using namespace std;

typedef string ElementType;


class Node
{
private:
  ElementType first, mid, last, phone;
  Node *next;

  friend class List;

  //Constructors
public:
  Node(ElementType f, ElementType mi, ElementType sur, ElementType ph){
    first = f;
    mid = mi;
    last = sur;
    phone = ph;
    next = NULL;
  };


  Node* getNext(){
    return next;
  }

  //Mutator function
  void printNode(){
    cout << first << " " << mid << " " << last << " " << phone;
  }

  //Accessor
  bool sameAs(Node *q){
    return (first == q->first && mid == q->mid && last == q->last && phone == q->phone);
  }

};
typedef Node* NodePointer;

//This class can create a list. 

class List {
private:
  Node* first;

public:
  List(){
    first = NULL;
  }

  NodePointer getFirst(){
    return first;
  };

  void insert(ElementType fir, ElementType midd, ElementType las, ElementType phon, NodePointer pos){
    NodePointer pN;
    pN = new Node(fir, midd, las, phon);


    NodePointer last = first;


    if (first == NULL){
      first = pN;
      return;
    }
    else{
      while(last->next != NULL){
	last = last->next;
      }
      last->next = pN;
    }
  }


//This function reads data (contacts) from a file and puts each line into a list.

void readData(List &L) {
  ElementType fi, mi, la, ph, ph1, ph2;
  ifstream fin;
  fin.open("dadexcel.txt");

  while(!fin.eof()){
fin >> fi >> mi >> la >> ph >> ph1 >> ph2;
ph = ph + " " + ph1 + " " + ph2;
L.insert(fi, mi, la, ph, L.getFirst());
  }
}

    //This function prints out the nodes in a list

    void printContactList(){
      NodePointer pN;
      for(pN = first; pN != NULL; pN = pN->next){
	pN->printNode();
	cout << endl;
      }
    }



};


    //Main function
    int main()
  {
      List R;
   readData(R);
      R.printContactList();

      return 0;

    }

Last edited on
Look very carefully at where you end your class (on line 108) - if it's not a member function then readData needs to be outside that.

Of course, you could, if you wanted, refactor your code so that readData was a member function ...
readData is inside the class.
so you either need
R.readData(R); //this is silly: you don't need to pass yourself to your own function, design issue.
or
the function needs to be outside the class. Which seems to be correct as you insert into L as an object and everything looks like an external design. Was this the intent? the issue could be how you indent, it is very hard to see what brackets align and the only easy way to spot the issue is the ; on the class bracket.
Topic archived. No new replies allowed.